I try to overlays (apply union) my multipolygon on the multiline with GeoPandas, but it seems like the GeoPandas overlay function work only with (Multi)polygon. Is there any way to make that with multiline and multipolygon with GeoPandas. Here is my code:

my_shp = gpd.read_file('my_shp.shp') # it's MultiLines shapefile
shp_buf = my_shp.buffer(5)
# as shp_buf it geoSeries i need to convert it to GeoDataFrame
df_shp_buf = gpd.GeoDataFrame({'geometry' : shp_buf})
union_1 = overlay(my_shp, df_shp_buf, how = 'union')

And I got this error:

TypeError: overlay only takes GeoDataFrames with (multi)polygon geometries
  • What output are you expecting overlaying polygons with lines? Split lines with polygon attributes? – BERA Mar 15 '19 at 10:42
  • Yeah exactly, that what i want to do, to split lines with polygon attributes – Elite Mar 15 '19 at 10:56
  • Can you hard-code some sample geometries using shapely objects? – Paul H Mar 15 '19 at 13:59
  • Yes, i think i can dp that – Elite Mar 18 '19 at 5:35

I answer my question maybe it will help, but i use fiona and shapely to do that.

First i recover the coords of shapefile and converted i in MultilineString geometry of shapely:

import fiona as fn
from shapely.geometry import MultiLineString

tab = []
test_shp = fn.open('test_shp.shp')    # the shapefile is a MultiLineString
with fn.open('my_shp.shp') as shp:    # the shapefile is a Multipolygon
    for i in range(0, len(shp)):
    geom = [tab[x]['geometry'] for x in range(0, len(tab))]
    xcoords = [list(geom[x].values())[1] for x in range(0, len(geom))]
    multi = MultiLineString(xcoords)

second time i apply the buffer to get an polygon

# buffer
multi_buf = multi.buffer(0.3)

Trird imake my union

xunion_1 = test.union(multi_buf)
| improve this answer | |

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.