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?