pyrosm icon indicating copy to clipboard operation
pyrosm copied to clipboard

BUG: Passing networkx graph to osmnx basic_stats no longer seems to work

Open Eph97 opened this issue 3 years ago • 2 comments

Again, really enjoying pyrosm. I've consistently been running into the same error even after trying many different attempts at getting around it so thought it might be worth creating an issue.

I had been using an older version of osmnx to get some basic graph stats from the data i'd been gathering using pyrosm. After updating osmnx for the first time in a while, it seems that the export networkx graphs no longer work with osmnx's basic_stats module.

I've spent quite some time trying to figure this out but it seems to be due to the newer version of networkx (networkx==2.5) now required by osmnx. I've attached a screenshot showing the error. This error I believe is typically due to the nodes list in the networkx graph being empty but the nodes list created by pyrosm is clearly not empty.

Screen Shot 2021-04-20 at 11 57 12 PM

To reproduce you could try:

from pyrosm import OSM, get_data
import osmnx as ox

# Initialize the reader
osm = OSM(get_data("helsinki_pbf"))

# Get all walkable roads and the nodes 
nodes, edges = osm.get_network(nodes=True)

# Create NetworkX graph
G = osm.to_graph(nodes, edges, graph_type="networkx")

ox.basic_stats(G)

I hope this isn't due to any installation errors but I've tried many times to reinstall all the packages or install in different ways but the problem persists. All of the examples in your Working with graphs notebook do seem to work though.

Eph97 avatar Apr 21 '21 04:04 Eph97

@Eph97 Thanks for reporting! 👍🏻 I will take a look at this. Could you still provide info about which version of OSMnx you have?

HTenkanen avatar Apr 21 '21 06:04 HTenkanen

@Eph97 Okay, I was able to reproduce the error. OSMnx adds a specific node attribute to the graph that calculates the number of streets connects to each intersection, and this is not currently done with pyrosm when exporting the graph.

For now, could you test whether the following workaround fixes the error that you receive?

import networkx as nx
import osmnx as ox
from pyrosm import OSM, get_data

# Initialize the reader
osm = OSM(get_data("helsinki_pbf"))

# Get all walkable roads and the nodes 
nodes, edges = osm.get_network(nodes=True)

# Create NetworkX graph
G = osm.to_graph(nodes, edges, graph_type="networkx")

# Workaround to fix the error
# ---------------------------
spn = ox.utils_graph.count_streets_per_node(G)
nx.set_node_attributes(G, values=spn, name="street_count")

ox.basic_stats(G)

By the way, please keep in mind that the pyrosm output graph to OSMnx is not yet fully compatible because OSMnx does some steps to clean the data which is not done currently by pyrosm. The idea is make pyrosm fully compatible in the future release, but for now I just want to warn you about this in case you use these statistics for research.. (i.e. I have not checked whether the statistical results from the graph are identical when comparing pyrosm and osmnx). I would be delighted to receive some feedback about this, in case you would do some comparisons between the stats at some point. 🙂

HTenkanen avatar Apr 21 '21 06:04 HTenkanen