This module provides classes and functions to facilitate working with geographical data.

class Region[source]

Region(name:str, bbox:list, pixel_size:float)

Defines a geographical region with a name, a bounding box and the pixel size

Region.width[source]

Width of the region

Region.height[source]

Height of the region

Region.transform[source]

Rasterio Affine transform of the region

Region.shape[source]

Shape of the region (height, width)

Region.coords[source]

Region.coords(offset='ul')

Computes longitude and latitude arrays given a shape and a rasterio Affine transform

Region.export[source]

Region.export(file)

Exports region information to json file

Region.load[source]

Region.load(file)

Loads region information from json file

These are the 5 regions used in the article:

//R_CA.json
{"name": "CA", "bbox": [-125, 32, -113, 43], "pixel_size": 0.01}
//R_PT.json
{"name": "PT", "bbox": [-10, 36, -6, 44], "pixel_size": 0.01}
//R_BR.json
{"name": "BR", "bbox": [-58, -20, -44, -5], "pixel_size": 0.01}
//R_MZ.json
{"name": "MZ", "bbox": [30, -27, 41, -10], "pixel_size": 0.01}
//R_AU.json
{"name": "AU", "bbox": [113, -27, 154, -10], "pixel_size": 0.01}

Examples:

r = Region(name='PI', bbox=[-10, 36, 5, 44], pixel_size=0.01)
r
name: PI
bbox: BoundingBox(left=-10, bottom=36, right=5, top=44)
pixel_size: 0.01
r.shape
(800, 1500)
test_eq(r.shape, (800, 1500))
r.transform
Affine(0.01, 0.0, -10.0,
       0.0, -0.01, 44.0)
test_eq(r.transform, rasterio.Affine(0.01, 0.0, -10.0, 0.0, -0.01, 44.0))
lon, lat = r.coords()
lon
array([-10.  ,  -9.99,  -9.98, ...,   4.97,   4.98,   4.99])

Geo functions

open_shp[source]

open_shp(file)

Read shapefile

open_tif[source]

open_tif(file)

Read tiff

bounds_from_shapefile[source]

bounds_from_shapefile(shapefile)

Computes bounding box for shapefile

size_from_bounds[source]

size_from_bounds(bounds, resolution)

Computes width and height from bounds for a given pixel resolution

size_resolution_assert[source]

size_resolution_assert(size, resolution)

rasterize[source]

rasterize(x, value_key=None, region=None, merge_alg='replace')

Rasterize shapefile

downsample[source]

downsample(x, src_tfm=None, dst_tfm=None, dst_shape=None, src_crs={'init': 'EPSG:4326'}, dst_crs={'init': 'EPSG:4326'}, resampling='average')

Donwsample a numpy array x

is_intersection[source]

is_intersection(gdf1, gdf2)

Find the intersection between two geo pandas dataframes

polygon_from_bounds[source]

polygon_from_bounds(bounds, to_GeoDataFrame=False, crs={'init': 'EPSG:4326'})

Create a polygon object from bounds

crop[source]

crop(x, bounds=None, shape=None, crop=True)

Crop rasterio dataset for a region defined by bounds. x is a dataset or a list of datasets (rasterio.open). If list then merge with bounds is used. else mask is used to crop given bounds or any given shape.

bounds_from_coords[source]

bounds_from_coords(lon, lat)

Compute bounds list form lon lat coords