I have many polygons that need to be cut by lines we have set up to split those polygons. Some polygons may not have any lines to split them, others will have only 1 line, while others may have several line splits.
Here are a couple example screenshots. First one, ploy 406 has no lines to split it, poly 25 has 3 lines that will split it, an finally in poly 27 has only 1 line to split it. All lines have been checked to make sure they completely cross all polys.
Im using python with geopandas and shapely libraries to handle this.
So far I have come up with a solution that appears to identify where each line intersects with a polygon and splits it with shapely.ops.split. But once it is complete the geometries are separated from the original geodataframe and Id like to maintain the attributes from the polygons.
# iterate over each cutline and find intersecting polygon
split_lots = []
for index, crow in df_cutlines.iterrows():
for index, prow in df_parkinglots.iterrows():
if crow['geom'].intersects(prow['geom']):
geometry = split(prow['geom'],crow['geom']) # split poly with line
split_lots.append(geometry)
There must be a better way to iterate through each line to find the intersecting poly, cut it and keep the attributes.
I thought about using the apply function to go through each row and add the new geom as another column to the original polygons (this would replace my last 2 lines above).
df_parkinglots['split_geom'] = df_parkinglots.apply(lambda prow: split(prow['geom'],crow['geom']), axis=1)
But shapely doesnt seem to like this.
For now I dont really care if the cut polys end up being multipart or single.