TypeError("Must pass list-like as `names`."), Network generated via osm, geopandas, momepy. Objective: Run short path analysis via osmnx
I am trying to solve short path analysis via osmnx.distance.k_shortest_paths(G, orig, dest, k, weight='length')
I have used this approach as I am limited in the SSL/memory, that is why I am not using osm.get_network or ox.graph_from_place. I want to be able to filter precisely (highways etc...).
I have checked G , and I can draw it. Please find attached the code:
from pyrosm import OSM, get_data
import momepy
import matplotlib.pyplot as plt
import geopandas as gpd
import pandas as pd
import osmnx as ox
import networkx as nx
import scipy as sp
from openpyxl import Workbook
osm_keys_to_keep = "highway"
custom_filter = dict(highway=['trunk', 'motorway', 'motorway_link','primary'])
filter_type = "keep"
osm = OSM(get_data("palma"))
roadtanker = osm.get_data_by_custom_criteria(custom_filter=custom_filter,
osm_keys_to_keep=osm_keys_to_keep,
filter_type=filter_type)
roadtankerline=gpd.GeoDataFrame.explode(roadtanker,ignore_index=True)
roadtankerline2=roadtankerline.to_crs(3857)
G = momepy.gdf_to_nx(roadtankerline2,approach='dual')
print(type(G))
fig, ax = plt.subplots()
nx.draw(G, {n:[n[0], n[1]] for n in G.nodes}, ax=ax, node_size=0)
target_node = ox.distance.nearest_nodes(G,336678.742,4792756.319)
plt.show()
I have this message error:
Traceback (most recent call last):
File "C:\Users\xw6362\PycharmProjects\ES14\Transport 1.py", line 49, in
target_node = ox.distance.nearest_nodes(G,336678.742,4792756.319)
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\osmnx\distance.py", line 217, in nearest_nodes
nodes = utils_graph.graph_to_gdfs(G, edges=False, node_geometry=False)[["x", "y"]]
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\osmnx\utils_graph.py", line 56, in graph_to_gdfs
gdf_nodes.index.rename("osmid", inplace=True)
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
return func(*args, **kwargs)
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\pandas\core\indexes\multi.py", line 3821, in set_names
return super().set_names(names=names, level=level, inplace=inplace)
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\pandas\util\_decorators.py", line 331, in wrapper
return func(*args, **kwargs)
File "C:\Users\xw6362\.conda\envs\ES14\lib\site-packages\pandas\core\indexes\base.py", line 1932, in set_names
raise TypeError("Must pass list-like as `names`.")
TypeError: Must pass list-like as `names`.
*I do not know what went wrong as the type of variable seems okay, it may be linked to the indexation but I am not sure.
Do you have a path to a solution?