10

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). Is there any way to get coordinates of an OSM node with this single id ?

geocodezip
  • 158,664
  • 13
  • 220
  • 245
Raphadasilva
  • 565
  • 1
  • 6
  • 21
  • My bad Alireza, I was in vacation and far from any screen ! I've just tested your lines and they worked fine. I validate your answer immediately, thank you again. – Raphadasilva Sep 26 '17 at 10:40
  • Sorry for the previous comment. happy to hear it works. – Alz Sep 26 '17 at 10:42

4 Answers4

14

you have all the attribute of each node and edge in the Graph. you can get node attributes using:

G.node[38862848]
#out: {'highway': nan,
# 'lat': 45.3210533,
# 'lon': -122.9790558,
# 'osmid': '38862848',
# 'ref': nan,
# 'x': 501641.47862882155,
# 'y': 5018616.5723966481}

G.node[38862848]['lat']
# out: 45.3210533

and to get edge attributes you can use G[u][v]:

G[5035130880][4963510289]
# out: 
#{0: {'bridge': 'yes',
#  'geometry': <shapely.geometry.linestring.LineString at 0x7f90ad7d5860>,
#  'highway': 'secondary',
#  'length': 671.332597496,
#  'name': 'Northwest 185th Avenue',
#  'oneway': False,
#  'osmid': [124454683, 24446714, 124454682]}}

All attributes are also in GeoDataFrame's of the graph. If you have list of nodes, the easiest way to get the geometry of all nodes is:

import osmnx as ox
import networkx as nx

gdf_nodes, gdf_edges = ox.graph_to_gdfs()
path = nx.shortest_path(G, G.nodes()[0], G.nodes()[1])
gdf_nodes.loc[path]
#out: 
#        highway    lat lon    osmid    ref x   y   geometry    traffic_signals
#5035130880 NaN 45.5637 -122.868    5035130880  NaN 510334  5.04558e+06 POINT (510334.0390091945 5045583.999886028) 0
#4963510289 NaN 45.5698 -122.868    4963510289  NaN 510329  5.04625e+06 POINT (510329.3114555664 5046254.728223645) 0
# ... 

the output is a GeoDataFrame.

Alz
  • 755
  • 6
  • 11
5

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
gboeing
  • 5,691
  • 2
  • 15
  • 41
3

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

eyoT
  • 49
  • 4
2

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').

tmo
  • 1,393
  • 1
  • 17
  • 47