Lukas
Lukas

Reputation: 23

OSMnx: How to get OSMnx stats from a GeoSeries

I would like to get the OSMnx stats for a GeoSeries in Python. I have a GeoSeries with 406 Polygons and I would like to have the OSMnx for each Polygon.

I succeed to get the OSMnx stats for a single Polygon. Herefore I used the following code:

polygonsnew = polygons.to_crs({'init': 'epsg:4326'})
osm = ox.core.graph_from_polygon(polygonsnew[101], network_type='drive')
stats = ox.basic_stats(osm)
stats

When I tried to get the graph for all the polygons by:

osm = ox.core.graph_from_polygon(polygonsnew, network_type='drive')

I get this error-message:

   1730     # verify that the geometry is valid and is a shapely Polygon/MultiPolygon
   1731     # before proceeding

I would like to know if there is a way to get the stats for all polygons. Thank you for your help!

Upvotes: 1

Views: 393

Answers (1)

gboeing
gboeing

Reputation: 6442

As you can see in the docs, graph_from_polygon takes a shapely Polygon or MultiPolygon, but you are passing it a GeoSeries. To get the graphs for all the polygons in a GeoSeries, you can either:

  1. Do them all at once in one single graph by taking the unary_union of the GeoSeries then passing its resulting (Multi)Polygon to OSMnx.
  2. Do them one at a time, by looping through your GeoSeries and passing the geometries one at a time to OSMnx.

Upvotes: 1

Related Questions