1

I created a Graph with Networkx and its nodes look like the following:

dict(itertools.islice(H.nodes.items(), 3))

> {{'node1': 'lon': coordx_1,
    'lat': coordy_1,
    'name': 'Road1'}, 
    {'node2': 'lon':coordx_2, 
    'lat': coordy_2, 
    'name': 'Road2'}, ...}

I also have a serie of individual points with their respective coordinates:

lon1, lat1 = coordx_3, coordy_3 #let's name it 'PointA'
lon2, lat2 = coordx_4, coordy_4 #let's name it 'PointB'

Coordinates from the Graph and from the individual points are in EPSG:4326.

I am trying to identify which node in the Graph is the closest to each set of coordinates. E.g: Which node is the closest to PointA and which one is the closest to PointB

I found some codes online that allow the computation of the shortest distance between two points but I can't seem to manage to apply it to the Graph I'm working with as I keep getting :

AttributeError: 'tuple' object has no attribute 'items'

Is there a way to do that?

I.M.
  • 344
  • 3
  • 14
  • 2
    This is the problem of nearest point detection - you do not need to solve it with networkx.You can use KD trees along with coordinates matrix: https://stackoverflow.com/a/32781737/5260876 – ilia Oct 23 '20 at 11:14
  • Thank you very much @ilia I will check it out. – I.M. Oct 29 '20 at 11:18

0 Answers0