Skip to content

Instantly share code, notes, and snippets.

@matthewhanson
Created October 20, 2020 18:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhanson/9f57fe5535922f9745d0d88ab67751d5 to your computer and use it in GitHub Desktop.
Save matthewhanson/9f57fe5535922f9745d0d88ab67751d5 to your computer and use it in GitHub Desktop.
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