0

My problem is: I have the geographical coordinates of a place, but when I use ox.get_nearest_node(), the node I get is way too far from the coordinates and I don't know why:

import networkx as nx
import matplotlib.pyplot as plt
import osmnx as ox
import geopandas as gpd
ox.config(log_console=True, use_cache=True)


place = 'Portugal, Lisbon'
G = ox.graph_from_place(place, network_type='drive')
G = ox.project_graph(G)
hospitals = ox.pois_from_place(place, amenities=['hospital'])

coord_1 = (38.69950, -9.18767)  
target_1 = ox.get_nearest_node(G, coord_1)
print(target_1)

nc = ['r' if node==target_1 else 'gray' for node in G.nodes()]
ns = [50 if node==target_1 else 1 for node in G.nodes()]
fig, ax = ox.plot_graph(G, node_size=ns, node_color=nc, node_zorder=2)

From this I get the following node: 4751061000

The plot is:

enter image description here

And the hospital is near the sea:

enter image description here

DPM
  • 845
  • 7
  • 33
  • How about you print out the `coord_1`, `target_1` and any possible details of the `target_1` node? Also you may add dummy node at `coord_1` and plot it in green on the same plot, just to be sure. Since the locations seen different in plot and map, what if there are two hospitals with same name? That's why try printing out more info about `target_1` node – dumbPy May 15 '20 at 23:28

1 Answers1

1

It looks like your confusion is due to a projection issue. I ran your code without the following line, which is projecting G to UTM https://osmnx.readthedocs.io/en/stable/osmnx.html#osmnx.projection.project_graph, and the hospital you want is returned.

G = ox.project_graph(G)

enter image description here

Matthew Borish
  • 3,016
  • 2
  • 13
  • 25
  • Thank you!! Turns out I was mixing projected coordinates with not projected coordinates. Thank you for your feedback! – DPM May 16 '20 at 19:51