-
-
Save matthewhanson/6be66c97c828acd1d39d8cbb97a0981e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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