I have a shapefile (will be called source-file hereafter), which I need to clip by a MultiPolygon shapefile so that I can have a clipped shapefile for each polygon.
I tried the GeoPandas, though I am able to clip the source file by individually clipping it by selecting the polygons separately from the MultiPolygon shapefile, when I try to loop over the polygons to automate the clipping process I get the following error:
Error: TypeError: 'mask' should be GeoDataFrame, GeoSeries or(Multi)Polygon, got <class 'tuple'>
My code:
import geopandas as gpd
source = ('source-shapefile.shp')
mask = ('mask_shapefile.shp')
sourcefile = gpd.read_file(source)
maskfile = gpd.read_file(mask)
for row in maskfile.iterrows():
gpd.clip(sourcefile, row)
print(type(row)
shows tuple<class 'tuple'>
exploded = maskfile.explode(index_col = 'ID')
thenfor row in exploded['geometry']: \ clipped = gpd.clip(masked, row)
however I am not able to save separate shapefiles for each polygon as it over writes the files in the output folder , I tried this line to save the filesclipped.to_file('polygon'+ {int('ID'):03}+'.shp')