I'm retrieving some geometries from OpenStreetMap with OSMnx library with the following code:

G = ox.graph_from_place('Casco Viejo, Bilbao, Spain', network_type='walk', 
                        retain_all=True, buffer_dist = 50, which_result = 2,
                        infrastructure = 'relation["highway" = "pedestrian"]')

which yields the following graph composed by shapely linestrings:

enter image description here

Then I convert the graph into a geopandas geodataframe:

ped = ox.graph_to_gdfs(G, nodes = False)

I've tried this to convert Linestrings to Points and then Points to Multipolygon

Is there a way to convert this linestrings into a shapely Multipolygon:

from shapely import geometry, ops

# combine them into a multi-linestring
multi_line = geometry.MultiLineString(list(ped['geometry']))

merged_line = ops.linemerge(multi_line)

from shapely.geometry import Point, MultiPoint

points = []
for i in range(0, len(merged_line)):

coords = [p.coords[:][0] for p in points]

poly = Polygon(coords)

This yields a weird wrong geometry:


If I try:


It gives this Error Message: TypeError: 'Point' object is not subscriptable

Is there a way to transform Linestrings into Multipolygon and this into GeoDataFrame?


For that you need to use unary_union and polygonize (see Split polygon by MultiLineString - shapely)

ped = ox.graph_to_gdfs(G, nodes = False)
from shapely.ops import unary_union, polygonize
multi_line = ped.geometry.values
border_lines = unary_union(multi_line)
result = MultiPolygon(polygonize(border_lines))

The resulting polygons

enter image description here

enter image description hereenter image description here


To fix the issue of holes, a solution is to retain only the polygons with holes

polys = []
result = list(ops.polygonize(border_lines))
for poly in result:
    if len(poly.interiors) >= 1: # only polygon with holes

In one line:

polys = [poly for poly in result if len(poly.interiors) >= 1]

Only two polygons with holes

enter image description here

  • thanks! inside polygons are actually holes, is there a way to fix this issue as well? Jun 16 '20 at 13:07
  • 1
    look at New in the answer
    – gene
    Jun 16 '20 at 14:56

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.