Load a Graph from .osm file using Osmnx/Python

Question:

I want to load a graph from XML, i.e. .osm file, using Osmnx Python library.
The .osm file contains roads not connected each other, for example only highway=primary and highway=primary_link of a country’s region.

I use the parameter retain_all to avoid discarding all the roads, since

retain_all: if True, return the entire graph even if it is not connected. otherwise, retain only the largest weakly connected component.

I use this instruction:

graph = ox.graph_from_xml('temp.osm', retain_all=True)

But I get the following error

AttributeError: 'float' object has no attribute 'deg2rad'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "D:code.py", line 37, in <module>
graph = ox.graph_from_xml('temp.osm', retain_all=True)
File "D:Pythonlibsite-packagesosmnxgraph.py", line 518, in graph_from_xml
G = _create_graph(response_jsons, bidirectional=bidirectional, retain_all=retain_all)
File "D:Pythonlibsite-packagesosmnxgraph.py", line 587, in _create_graph
G = distance.add_edge_lengths(G)
File "D:Pythonlibsite-packagesosmnxdistance.py", line 154, in add_edge_lengths
dists = great_circle_vec(c[:, 0], c[:, 1], c[:, 2], c[:, 3]).round(precision)
File "D:Pythonlibsite-packagesosmnxdistance.py", line 60, in great_circle_vec
y1 = np.deg2rad(lat1)

TypeError: loop of ufunc does not support argument 0 of type float which has no callable deg2rad method

If I remove retain_all parameter, of course, the error does not occurr but the graph will contain only one primary road.
How can I keep all the roads even if not connected in the map?

Asked By: dnyll

||

Answers:

I forgot to post my solution.
I solved using another Python library, called Pyrosm:

osm = OSM('temp.pbf')
nodes, edges = osm.get_network(nodes=True, network_type='driving')
graph = osm.to_graph(nodes, edges, graph_type='networkx', retain_all=True)
Answered By: dnyll