I have two geodataframes: one containing many Linestrings, and another containing many Polygons. These lines and polygons intersect each other. The output I am trying to achieve is a new geodataframe containing links which have been split at any location that they intersect a polygon.
Simplified test code is as follows:
import geopandas
from shapely.geometry import Polygon, LineString
#Create geodataframe containing 2 links:
linkID = ['1','2']
link_geom = [LineString([(0, 0), (10, 10)]),LineString([(10, 10), (20, 10)])]
a = {'linkID':linkID,'geometry':link_geom}
gdf_links = geopandas.GeoDataFrame(a)
#Create geodataframe containing 2 polygons:
polyID = ['100','200']
poly_geom = [Polygon([(2, 1), (2, 3), (4, 3), (4, 1)]),Polygon([(15, 7), (15, 13), (18, 13), (18, 7)])]
b = {'polyID':polyID,'geometry':poly_geom}
gdf_poly = geopandas.GeoDataFrame(b)
You can see that the lines intersect the polygons by running the following code:
links = gdf_links.unary_union
polys = gdf_poly.unary_union
geopandas.GeoSeries([links,polys]).plot(cmap='tab10')
Which generates the following figure (annotation added in red/green to show the various elements involved):
Splitting the two lines wherever they intersect a polgyon will results in 6 segments (as labelled in green in the figure above).
Ideally, the output I would like to achieve is something like shown below, with a 'poly_intersect' column listing the ID of the polygon at which the split has occurred. Note that items 2 and 5 list two polygon ID's, because they intersect the polygon twice.

