I'm trying to write a function to clip rasters by a multi-polygon shapefile or GeoJSON in Python using rasterio.
So far, I found a good article that I've implemented, but it doesn't seem to work on clipping all polygons within a multi-polygon layer. Perhaps it is in this function below that prepares the GeoDataFrame for use in rasterio, which seems to only take the first feature geometry?
def getFeatures(gdf):
"""Function to parse features from GeoDataFrame in such a manner that rasterio wants them"""
import json
return [json.loads(gdf.to_json())['features'][0]['geometry']]
How would I clip a raster using a multi-polygon GeoJSON or shapefile in Python using rasterio?
Best Answer
Is this what you are looking for? I used this example integrated with a
for loop
.Note that
rasterio.mask.mask(src, shape, crop=True)
needs to be changed to[shape]
due to shapes (iterable object) – The values must be a GeoJSON-like dict or an object that implements the Python geo interface protocol (such as a Shapely Polygon).