Skip to content

Instantly share code, notes, and snippets.

@matthewhanson
Created March 31, 2021 01:31
Show Gist options
  • Save matthewhanson/6be66c97c828acd1d39d8cbb97a0981e to your computer and use it in GitHub Desktop.
Save matthewhanson/6be66c97c828acd1d39d8cbb97a0981e to your computer and use it in GitHub Desktop.
import rasterio
from rasterio import Affine as A
from rasterio.warp import transform_geom
def image_boundary(filename, scale=1, tolerance=0.005):
# open source data
src = rasterio.open(filename)
# generate datamask
arr = src.read(
1, out_shape=(int(src.shape[0] / scale), int(src.shape[1] / scale))
)
arr[numpy.where(arr != 0)] = 1
transform = src.transform * A.scale(scale)
for geom, val in rasterio.features.shapes(arr, transform=transform):
geometry = shape(
transform_geom(src.crs, "EPSG:4326", geom, precision=3)
)
if val == 1:
geometry = geometry.simplify(tolerance, preserve_topology=True)
return mapping(geometry)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment