[GIS] GeoPandas cut lines with polygon

geopandaspython

I wanted to clip lines with polygon geometry:

from shapely.geometry import Polygon, LineString
import geopandas as gpd

polygon = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
line1 = LineString([(0.5, 0.5), (2, 2)])
line2 = LineString([(0.9, 0.9), (2, 0.6)])


poly_gdf = gpd.GeoDataFrame(geometry=[polygon])
line_gdf = gpd.GeoDataFrame(geometry=[line1, line2])

fig, ax = plt.subplots()
poly_gdf.plot(ax=ax, alpha=0.5)
line_gdf.plot(ax=ax, color='green')

enter image description here

When I do it with:

gpd.overlay(line_gdf, poly_gdf, how='difference')

The result is:

TypeError: overlay only takes GeoDataFrames with (multi)polygon  geometries.

Best Answer

Reference documentation of geopandas.overlay and error message are clear:

TypeError: overlay only takes GeoDataFrames with (multi)polygon  geometries

So, I modified your code for including a second polygon:

from shapely.geometry import Polygon, LineString
import geopandas as gpd
from matplotlib import pyplot as plt

polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])
polygon2 = Polygon([(0.5, 0.5), (1.5, 0.5), (1, 1), (0, 1), (0.5, 0.5)])
line1 = LineString([(0.5, 0.5), (2, 2)])
line2 = LineString([(0.9, 0.9), (2, 0.6)])

poly_gdf1 = gpd.GeoDataFrame(geometry=[polygon1])
poly_gdf2 = gpd.GeoDataFrame(geometry=[polygon2])
line_gdf = gpd.GeoDataFrame(geometry=[line1, line2])

overlay = gpd.overlay(poly_gdf1, poly_gdf2, how='difference')

plt.ion()

fig, ax = plt.subplots()
poly_gdf1.plot(ax=ax, alpha=0.5)
poly_gdf2.plot(ax=ax, alpha=0.5)
overlay.plot(ax=ax, color='red', alpha=0.5)
line_gdf.plot(ax=ax, color='green')

and result (red area) of overlay with two polygons is produced without any error:

enter image description here

On the other hand, if you want to clip lines with polygon geometry you can use intersection or difference as in following script:

from shapely.geometry import Polygon, LineString
import geopandas as gpd
from matplotlib import pyplot as plt

polygon1 = Polygon([(0, 0), (1, 0), (1, 1), (0, 1), (0, 0)])

line1 = LineString([(0.5, 0.5), (2, 2)])
line2 = LineString([(0.9, 0.9), (2, 0.6)])

poly_gdf1 = gpd.GeoDataFrame(geometry=[polygon1])
line_gdf = gpd.GeoDataFrame(geometry=[line1])

plt.ion()

fig, ax = plt.subplots()
poly_gdf1.plot(ax=ax, alpha=0.1)

difference = line_gdf.difference(poly_gdf1)
intersection = line_gdf.intersection(poly_gdf1)

difference.plot(ax=ax, alpha=0.5, color='magenta')
intersection.plot(ax=ax, alpha=0.5, color='green')

Result is presented in following image:

enter image description here

Related Question