Coverage for stems/gis/geom.py : 81%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
""" Geometry related functions """
# ============================================================================= # Bounds / BoundingBox """Return the union of some BoundingBox(s)
Parameters ---------- bounds : BoundingBox Bounding boxes
Returns ------- BoundingBox The union of all input BoundingBox """ (list_like(b) and len(b) == 4) for b in bounds])
"""Return the intersection of some BoundingBox(s)
Parameters ---------- bounds : BoundingBox Bounding boxes
Returns ------- BoundingBox The intersection of all input BoundingBox """ (list_like(b) and len(b) == 4) for b in bounds])
""" Calculate union of bounds for some transform
Parameters ---------- bounds : Sequence[BoundingBox] Bounding boxes transform : Affine Affine transform to use
Returns ------- rasterio.coords.BoundingBox Union of BoundingBox affine.Affine Unified affine transform tuple[int, int] Shape of union (nrow, ncol) """ # Union of bounds
# Create transform
# Calculate width / height
# Adjust bounds to fit (given integer width / height)
""" Calculate the source window for some output bounds
Parameters ---------- src_bounds : rasterio.coords.BoundingBox Input bounds for the source raster image src_transform : affine.Affine Affine transform for the source raster image dst_bounds : rasterio.coords.BoundingBox Destination bounds
Returns ------- rasterio.windows.Window Window of source that contains ``dst_bounds`` rasterio.coords.BoundingBox The BoundingBox representing the destination window for the source """ # TODO: refactor to use bbox_union/intersect? # Calculate source window and bounds (x/y offsets and sizes)
src_transform)
# Round the sizes
""" Calculate the rounded source window for some other transform
Parameters ---------- src_bounds : rasterio.coords.BoundingBox Input bounds for the source raster image dst_transform : affine.Affine Destination Affine transform
Returns ------- rasterio.windows.Window Window of source that conforms (pixel size, upper left) to ``dst_tranform`` """
# ============================================================================= # Geometry """ Return True if a geometry is not valid / null
Parameters ---------- shapely.geometry.BaseGeometry Some shapely geometry (or something that supports __geo_interface__)
Returns ------- bool Null or not """ # asShape doesn't copy vs shape geom_ = shapely.geometry.asShape(geom) return getattr(geom_, '_geom', None) is None
# ============================================================================= # GEOJSON """ Fixes issues with dateline and meridian cutting
Parameters ---------- geojson : dict GeoJSON as a dict
Returns ------- dict Fixed up GeoJSON
TODO ---- * Q1: Can we do this ourself using shapely/maths? * Q2: Is solution to Q1 faster? Avoids dumps/loads """ from osgeo import gdal, ogr gdal.UseExceptions() ogr.UseExceptions()
def read_vsi(filename): vsifile = gdal.VSIFOpenL(filename,'r') gdal.VSIFSeekL(vsifile, 0, 2) vsileng = gdal.VSIFTellL(vsifile) gdal.VSIFSeekL(vsifile, 0, 0) return gdal.VSIFReadL(1, vsileng, vsifile)
# Fix up GeoJSON while writing to memory _ = gdal.VectorTranslate('/vsimem/out.json', json.dumps(geojson), format='GeoJSON', layerCreationOptions=_CO_RFC7946) _ = None # make sure to delete so it flushes
text = (read_vsi('/vsimem/out.json') .decode('utf-8') .replace("'", '"'))
return json.loads(text) |