Rasters

GeoTypes uses rasterio for raster operations with a wrapper (the Raster class) around these operations to allow easier functionality. This includes: * Extracting values from the value at a given GeoPoint or LL, even if they are in a different CRS. * Extracting patches of a set size at a given GeoPoint or LL, even if they are in a different CRS. * Returning the bounds as a GeoArea.

These capabilities are integrated into the RasterRegistry class to make it easier to get data from multiple rasters.

from geotypes.rasters import Raster, RasterRegistry
r = Raster('example.tif')
r
Raster("Path=example.tif","crs=EPSG:4326",size(552,620,))

Extracting data from a raster

You can either extract points or patches of a set size from a raster. If the data is invalid (no data value or out of bounds) it will return None, including for the entire patch. If this is an issue when using patches, consider filling the raster first (i.e. using qgis).

# Get the bounds as a GeoArea
area = r.get_bounds_as_geoarea()
for _ in range(10):
    p = area.random_point_geo()
    print("Depth", r.get_value(p))

# Get some patches
for _ in range(10):
    p = area.random_point_geo()
    patch = r.extract_patch_geo(p, width=5,height=5)
    if patch is not None:
        print(f"Patch mean={patch.mean()}")
Depth [-31.409237]
Depth [-34.234123]
Depth [-3.4028235e+38]
Depth [-30.973043]
Depth [-3.4028235e+38]
Depth [-8.84387]
Depth [-36.756977]
Depth [-3.4028235e+38]
Depth [-35.469486]
Depth [-36.298344]
Patch mean=-35.81536865234375
Patch mean=-33.3203010559082
Patch mean=-23.544713973999023
Patch mean=-37.140865325927734
Patch mean=-8.296087265014648
Patch mean=-31.775911331176758
Patch mean=-37.30376052856445
Patch mean=-11.780535697937012

RasterRegistry

A raster registry is a convenient way to store and operate on collections of rasters. Typically, these rasters each hold features, i.e. raster 1 holds depth, raster 2 holds slope.

# to load a raster registry, you need to specify the filepaths and the size of the patches to be extracted. This can be [1,1] for a single patch.
import yaml
# this is a yaml file example to load the config. We are just using the same example bathymetry patch as reference.
yaml_str = """
bathy: 
    path: "example.tif"
    size: [1,1]
depth: 
    path: "example.tif"
    size: [1,1]
"""
raster_cfg = yaml.safe_load(yaml_str)
raster_registry = RasterRegistry(raster_cfg)

# This raster registry is passed into the feature extractor to provide access to the rasters
raster_registry.get_named_patches(p)
{'bathy': array([[-11.78369]], dtype=float32),
 'depth': array([[-11.78369]], dtype=float32)}