0

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)):
    points.append((Point(list(merged_line[i].coords[1]))))

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

poly = Polygon(coords)

This yields a weird wrong geometry:

shape(poly)

If I try:

MultiPolygon(points)

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

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

Rodrigo Vargas
  • 273
  • 3
  • 17

1 Answers1

1

If you're trying to construct a graph using the ox.graph_from_X functions, it does not make sense to have polgons or multipolygons, as a graph model is merely a set of elements (nodes) linked to one another by edges.

If you just want the geometries (points, polygons, multipolygons, etc) of walkable elements in the area, then you should use the pois module to get a GeoDataFrame of geometries (without any graph topology), like:

import osmnx as ox
ox.config(use_cache=True, log_console=True)
place = 'Casco Viejo, Bilbao, Spain'
gdf = ox.pois_from_place(place, which_result=2, tags={'highway':'pedestrian'})
gdf.shape #(87, 56)

Finally, note that your usage of the infrastructure parameter was deprecated a few releases ago and removed in a previous release. You may want to update the package to the latest version.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thanks, Geoff, but the outcome isn't good enough for my purpose. When printing the result there are errors in the performance of many polygons. `fig, ax = ox.plot_shape(gdf.loc[(gdf.geom_type == 'Polygon') | (gdf.geom_type == 'MultiPolygon')])` – Rodrigo Vargas Jun 17 '20 at 06:53