OSMNx : get coordinates of nodes using OSM id

Viewed 13139

I used the Python library OSMNx to draw an optimal route between several steps of a city trip. The final variable is a list of OSM ids.

Now, I'm trying to save this route as a shp or json files. The problem is that I need for that the latitude/longitude of each node, but I didn't found an OSMNx function to do that.

I tried get_route_edge_attributes (but coordinates are not a valid attribute for this function). There is any way to get coordinates of an OSM node with this single id ?

Thanks in advance.

4 Answers

See also this on GitHub for more details:

The x and y attributes are your node coordinates. If your graph is unprojected, then they are in lat-lon (degree units).

If you've projected your graph, then x and y are your projected node coordinates (in meters or whatever units your projected coordinate system uses) and the nodes will also have additional lat and lon attributes that contain the original unprojected coordinates.

import osmnx as ox
G = ox.graph_from_place('Piedmont, CA, USA', network_type='drive')
node_id = list(G.nodes)[0]
G.nodes[node_id]['x'] #lon
G.nodes[node_id]['y'] #lat

G.node[38862848]['y'] for latitude and G.node[38862848]['x'] for longitude

28-01-2021 update

After a update of the NetworkX API, notice that you need to use the plural: nodes e.g.

G.nodes[node_id]['x'] 

should work.

Doing G.node results in an error ('MultiDiGraph' object has no attribute 'node').

Related