Since geopandas can't perform overlay/intersection with lines, I found a workaround:
import geopandas as gpd import fiona from shapely.geometry import shape, MultiPolygon, LineString, MultiLineString lines = gpd.read_file('lines.shp') polyons= gpd.read_file('polygons.shp') pol_f = fiona.open('polygons.shp') pol_shp = MultiPolygon([shape(x["geometry"]) for x in pol_f]) lines2 = lines.geometry.apply(lambda x: x.intersection(pol_shp)) lines2.to_file('lines_int.shp')
But it does not let me because there are the empty geometries are GeometryCollections.
I tried this to filter, but it won't work.
lines3 = lines2[lines2.geometry != np.nan] lines3 = lines2[lines2.geometry == LineString] lines3 = lines2[lines2.geometry == LineString()] lines3 = lines2[lines2.geometry == MultiLineString()] lines3 = lines2[lines2.geometry == MultiLineString] lines3 = lines2[lines2.geometry != GeometryCollection]