-1

I am trying to calculate the basic stats of a graph created from a gdf using the following code:

ox.utiles_graph.graph_from_gdf(n,e)

Nodes and edges have been saved previously and resulted from:

G = graph.graph_from_place(some_place_name) 
n,e = ox.graph_to_gdfs(G)
n.save_to_file(filname,driver="ESRI Shapefile")
e.save_to_file(filname,driver="ESRI Shapefile")

I can plot the graph and also calculate travel times from it but I am unable to use the basic stats function. I get the following error:

 ---> 56 ox.basic_stats(G)

~/anaconda3/envs/ox/lib/python3.9/site-packages/osmnx/stats.py in basic_stats(G, area, clean_int_tol, clean_intersects, tolerance, circuity_dist)
    346     stats["edge_length_avg"] = stats["edge_length_total"] / stats["m"]
    347     stats["streets_per_node_avg"] = streets_per_node_avg(G)
--> 348     stats["streets_per_node_counts"] = streets_per_node_counts(G)
    349     stats["streets_per_node_proportions"] = streets_per_node_proportions(G)
    350     stats["intersection_count"] = intersection_count(G)

~/anaconda3/envs/ox/lib/python3.9/site-packages/osmnx/stats.py in streets_per_node_counts(G)
     80     """
     81     spn_vals = list(streets_per_node(G).values())
---> 82     return {i: spn_vals.count(i) for i in range(int(max(spn_vals)) + 1)}
     83 
     84 

ValueError: max() arg is an empty sequence

Any clue how I can soleve this?

Määäx
  • 39
  • 3

1 Answers1

0

If you want to save your graph to disk and then work with it again later, you should save it as a GraphML file. If you must save it as node/edge shapefiles for some reason, you'll need to manually calculate the street per node counts if you want to calculate basic stats:

import osmnx as ox
import networkx as nx

G = ox.graph_from_gdfs(some_nodes, some_edges)

spn = ox.utils_graph.count_streets_per_node(G)
nx.set_node_attributes(G, values=spn, name="street_count")
stats = ox.basic_stats(G)

Note that if you do it manually this way, your street per node counts will probably be somewhat inaccurate due to periphery effects. You won't have this problem if you just download the graph the usual way then save it as a GraphML file.

gboeing
  • 5,691
  • 2
  • 15
  • 41
  • Thanks for the quick answer. The reason for using Shapefiles is to be able to load the data for a specific neighbourhood tho. Is there any way to calculate the stats on a single neighbourhood described by a polygon ? – Määäx Nov 11 '21 at 11:49
  • Sure. See the usage examples and documentation: https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.graph.graph_from_polygon – gboeing Nov 11 '21 at 15:14