1

I am trying to intersect two GeoPandas data frames, the first one is the street data (streets) and the second one is a buffer (polygon) around a point (buffer). I want to find the intersection of streets in this buffer zone and if a street extends outside of the buffer I want it to be cut on the border. I can do the following:

streets_in_buffer=geopandas.sjoin(streets,buffer,how='inner', op='intersects')

However, I cannot cut the street (in red) at the border of the buffer (in blue) and it extends outside of the boundary like this:

enter image description here

I was wondering if there is a way to only get the portion of the streets inside the circle and end up with something like this:

enter image description here

2

As stated in the description of geopandas.overlay:

Perform spatial overlay between two polygons.

And in your problem there is one polygon (buffer) and many linestrings (streets)

1) A first solution is to iterate over the streets geometries and compute the intersections with the buffer polygon or directly use unary_union ((Shapely unary_union) to intersect one shapely geometry with other, but the result is a shapely geometry (not a GeoDataFrame)

lines = streets.geometry.unary_union
intersection = lines.intersection(buffer.geometry[0])
type(intersection)
<class 'shapely.geometry.multilinestring.MultiLineString'>
# create a GeoDataFrame
result = geopandas.GeoDataFrame({'geometry':intersection})

It is possible to do it directly:

result = geopandas.GeoDataFrame(geometry=list(streets.unary_union.intersection(buffer.unary_union)))
print(result.head(2))
             geometry
0  LINESTRING (191.0345140167503 -88.977418522730...
1  LINESTRING (141.9720460135291 -210.61516114749...

2) A second solution is to use the function clip_shp() of earthpy.clip and the result is directly a GeoDataFrame (with the original attributes)

from earthpy import clip as cl
result = cl.clip_shp(streets, buffer)
type(result)
<class 'geopandas.geodataframe.GeoDataFrame'>
print(result.head(2))
   id                    geometry
1   2  LINESTRING (141.9720460135291 -210.61516114749...
0   1  LINESTRING (191.0345140167503 -88.977418522730...

enter image description here

PS

sjoin (geopanda.sjoin(streets, buffer, how='left', op='intersects') gives only the left geometry (or the right geometry) and not the geometry of the intersections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.