Skip to main content

Spatial operations extend fiona and rasterio

Project description

Geo-Sardine :fish:

python package codecov Maintainability

PyPI - Python Version PyPI Conda

Spatial operations extend fiona and rasterio. Collection of spatial operation which i occasionally use written in python:

  • Interpolation with IDW (Inverse Distance Weighting) Shepard
  • Drape vector to raster
  • Spatial join between two vector
  • Raster wrapper, for better experience. ie: math operation between two raster, resize and resample

:blue_book: documentation: https://sahitono.github.io/geosardine

Setup

install with pip pip install geosardine

or anaconda conda install -c sahitono geosardine

How to use it

Drape and spatial join

import geosardine as dine
import rasterio
import fiona

with rasterio.open("/home/user/data.tif") as raster, fiona.open("/home/user/data.shp") as vector:
    draped = dine.drape_geojson(vector, raster)
    joined = dine.spatial_join(vector, raster) 

IDW Interpolation

import numpy as np
import geosardine as dine
xy = np.array([
        [106.8358,  -6.585 ],
        [106.6039,  -6.7226],
        [106.7589,  -6.4053],
        [106.9674,  -6.7092],
        [106.7956,  -6.5988]
])
values = np.array([132., 127.,  37.,  90., 182.])

"""
if epsg not provided, it will assume that coordinate is in wgs84 geographic
Find your epsg here https://epsg.io/
"""
interpolated = dine.interpolate.idw(xy, values, spatial_res=(0.01,0.01), epsg=4326)

# Save interpolation result to tiff
interpolated.save('idw.tif')

# shapefile or geojson can be used too
interp_file = dine.interpolate.idw("points.shp", spatial_res=(0.01,0.01), column_name="value")
interp_file.save("idw.tif")

# The result array can be accessed like this
print(interpolated.array)
"""
[[ 88.63769859  86.24219616  83.60463194 ... 101.98185127 103.37001289
  104.54621272]
 [ 90.12053232  87.79279317  85.22030848 ... 103.77118852 105.01425289
  106.05302554]
 [ 91.82987695  89.60855271  87.14722258 ... 105.70090081 106.76928067
  107.64635337]
 ...
 [127.21214817 127.33208302 127.53878268 ...  97.80436475  94.96247196
   93.12113458]
 [127.11315081 127.18465002 127.33444124 ...  95.86455668  93.19212577
   91.51135399]
 [127.0435062  127.0827023  127.19214624 ...  94.80175756  92.30685734
   90.75707134]]
"""

Raster Wrapper

Geosardine include wrapper for raster data. The benefit are:

  1. math operation (addition, subtraction, division, multiplication) between rasters of different size, resolution and reference system. The data type result is equal to the first raster data type

    for example:

    raster1 = float32 and raster2 = int32
    raster3 = raster1 - raster2
    raster3 will be float32
    
  2. resample with opencv

  3. resize with opencv

  4. split into tiled

from geosardine import Raster


"""
minimum parameter needed to create raster are 
1. 2D numpy array, example: np.ones(18, dtype=np.float32).reshape(3, 3, 2)
2. spatial resolution, example:  0.4 or ( 0.4,  0.4)
3. left coordinate / x minimum
4. bottom coordinate / y minimum
"""
raster1 = Raster(np.ones(18, dtype=np.float32).reshape(3, 3, 2), resolution=0.4, x_min=120, y_max=0.7)

## resample
resampled = raster.resample((0.2,0.2))
## resize
resized = raster.resize(height=16, width=16)

## math operation between raster
raster_2 = raster + resampled
raster_2 = raster - resampled
raster_2 = raster * resampled
raster_2 = raster / resampled

## math operation raster to number
raster_3 = raster + 2
raster_3 = raster - 2
raster_3 = raster * 2
raster_3 = raster / 2

Geosardine CLI

You can use it through terminal or command prompt by calling dine

$ dine --help
Usage: dine [OPTIONS] COMMAND [ARGS]...

  GeoSardine CLI

Options:
  --help  Show this message and exit.

Commands:
  drape         Drape vector to raster to obtain height value
  info          Get supported format
  join-spatial  Join attribute by location
  idw           Create raster with Inverse Distance Weighting interpolation

License

License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

geosardine-0.12.0a0.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

geosardine-0.12.0a0-py3-none-any.whl (25.9 kB view details)

Uploaded Python 3

File details

Details for the file geosardine-0.12.0a0.tar.gz.

File metadata

  • Download URL: geosardine-0.12.0a0.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10

File hashes

Hashes for geosardine-0.12.0a0.tar.gz
Algorithm Hash digest
SHA256 ddac3c95763b878c5454f2717b800b62c2f7bae3a4a372cbba00404e6c7c0fcc
MD5 5c88f2058009a0b7d8472914cddf1faa
BLAKE2b-256 9d35d854e376e5ff434cb39b5de00477e68902f288b14a208ecf08f949b04441

See more details on using hashes here.

File details

Details for the file geosardine-0.12.0a0-py3-none-any.whl.

File metadata

  • Download URL: geosardine-0.12.0a0-py3-none-any.whl
  • Upload date:
  • Size: 25.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.8.6 Windows/10

File hashes

Hashes for geosardine-0.12.0a0-py3-none-any.whl
Algorithm Hash digest
SHA256 794123470629ea960d4d0992dd26d28d2b723c2dd47863421d7544571a1740ea
MD5 e202415f4368893ba9f4a66c71606703
BLAKE2b-256 2c615460e6993db0b177ebc3f727731946e36291fd66eeab48233a7b05974835

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page