Search code examples
networkxopenstreetmaposmnx

Issue when adding new node in graph


Im having some issues while trying to add a new node to a graph (with OSMNX)

I need to calculate some distances on some areas that dont have nodes near.

Here is my code:

import networkx as nx
import osmnx as ox
from IPython.display import IFrame
import geopandas as gpd
from shapely.geometry import  Point

my_dict = {
  '001': {
    'y': -31.640224888841907,
    'x': -60.672566478771884,
    'street_count': 1
  }
}

tmp_list = []
for item_key, item_value in my_dict.items() :
  tmp_list.append({
    'geometry' : Point(item_value['x'], item_value['y']),
    'osmid': item_key,
    'y' : item_value['y'],
      'x' : item_value['x'],
    'street_count': item_value ['street_count']
   })
my_nodes = gpd.GeoDataFrame(tmp_list)

G = ox.graph_from_place("Santa Fe, Santa Fe, Argentina", network_type="drive", buffer_dist=5000)
nodes= ox.graph_to_gdfs(G, nodes=True, edges=False)
edges= ox.graph_to_gdfs(G, edges=True, nodes=False)
nodes = nodes.append(my_nodes, ignore_index = True)
G2 = ox.graph_from_gdfs(nodes, edges)

m1 = ox.plot_graph_folium(G2, popup_attribute="name", weight=2, color="#8b0000")

dest = (-60.70916, -31.64553)
ori=  (-60.66756, - 31.63719)

iniciocercano = ox.nearest_nodes(G2, ori[0], ori[1], return_dist=True)
finalcercano = ox.nearest_nodes(G2, dest[0], dest[1], return_dist=True)

pathDistance =  nx.shortest_path_length(G2, iniciocercano[0], finalcercano[0], weight="length")

route = nx.shortest_path(G2, iniciocercano[0], finalcercano[0])

And the error that Im getting is: Input contains NaN.

I also notice that the original graph (G) has: 9423 nodes and 25013 edges. And the new graph (G2) has: 18847 nodes and 25013 which is pretty strange. Somehow the nodes are getting duplicate.

Thank you for your time.


Solution

  • Your my_nodes GeoDataFrame is not indexed by osmid like it needs to be, and like your nodes GeoDataFrame is.