#momepy
#30DayMapChallenge Day27: New tool
A little experiment with urban geometry using momepy.
#30DayMapChallenge #Map #Cartography #matplotlib #geopandas #osm #momepy
November 27, 2026 at 6:08 PM
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?
gis.stackexchange.com
July 31, 2026 at 1:10 AM
Python: How to convert shapefile of a road network to a Complete Graph network using Networkx
How can I convert a geodataframe of LineStrings to a Complete Graph with NetworkX? I have a geodataframe of a collection of linestrings that geographically represent a road network (purple). I have 2 points (red and blue) representing A and B. I'd like to find the shortest route between A and B travelling along the road network. I have converted the geodataframe to a network using momepy library: road = gpd.read_file(input_road.shp) #need to explode roads, as is multiline exploded = road.explode(index_parts=True) G = momepy.gdf_to_nx(exploded, approach="primal",length='mm_len',multigraph=True) This successfully converts my road network to a networkx multigraph, whilst preserving distance (mm_len) between points. Then I add points A and B to the network by getting the closest nodes on the road network to these points: my_points = gpd.read_file(my_points) #Firstly, need to convert the coordinates to a tuple of x,y coordinates for my_point in my_points .geometry.values: coords_values = re.findall('[0-9.]+ [0-9.]+', str(my_point )) #convert list of strings separated by spaces, to a list of coords as list. coords = [sub.split(' ') for sub in coords_values ][0] coords_tuple= tuple(map(float, coords )) coords_list.append(coords_tuple) #add this coord tuple as a column in the dataframe my_points["coords_syntax"] = coords_list #this dictionary will be used to select the network nodes. The key is the coordinate tuple, and the attribute will be the 'Name' of the point dict_df_mypoints = home.set_index('coords_syntax')['Name'].to_dict() #select the nearest node points on the road network to these coordinate tuples A = list(G.nodes()) #iterate through dict of coords: my_points for point in dict_df_mypoints : #get closest node (using KDTree) between point and the array of cords A generated above closest_node = A[scipy.spatial.KDTree(A).query(point)[1]] #make these above closest nodes have attribute my_point, with other attributes (dict_df[point]) G.nodes[closest_node]['my_point'] = dict_df[point] So now within all the nodes of my road network, I have 2 representing points A and B respectively. Their node key is the coordinate pair. However, when I run the shortest_path algorithm between A and B I get this error: nx.shortest_path(G,source = (455529.02164326626, 206374.9504608615),target = (454340.3426543578, 207204.53480888018)) >>NetworkXNoPath: No path between (455529.02164326626, 206374.9504608615) and (454340.3426543578, 207204.53480888018) And when I check the edges of these nodes, they are only connected to their 2 neighbours, and no other node: nx.single_source_shortest_path_length(G,source = (455529.02164326626, 206374.9504608615)) >>{(455529.02164326626, 206374.9504608615): 0, >>(455582.6204962559, 206424.4603359318): 1, >>(455596.5948359391, 206455.62556823122): 2} So with my road network, I think I need ALL my nodes to be connected to one another (rather than just to their neighbours) with edges, otherwise the shortest_path won't work. I need to make my network a Complete Graph. However, I have no idea how to do that- it can't be done with Momepy. However I am unsure how to create a NetworkX Graph from scratch whilst preserving the geographical distances between the nodes of the road network - this is what drew me to using Momepy in the first place.
gis.stackexchange.com
May 27, 2026 at 7:26 PM
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?
gis.stackexchange.com
March 29, 2026 at 6:10 PM
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?
gis.stackexchange.com
November 27, 2025 at 12:07 PM
Python: How to convert shapefile of a road network to a Complete Graph network using Networkx
How can I convert a geodataframe of LineStrings to a Complete Graph with NetworkX? I have a geodataframe of a collection of linestrings that geographically represent a road network (purple). I have 2 points (red and blue) representing A and B. I'd like to find the shortest route between A and B travelling along the road network. I have converted the geodataframe to a network using momepy library: road = gpd.read_file(input_road.shp) #need to explode roads, as is multiline exploded = road.explode(index_parts=True) G = momepy.gdf_to_nx(exploded, approach="primal",length='mm_len',multigraph=True) This successfully converts my road network to a networkx multigraph, whilst preserving distance (mm_len) between points. Then I add points A and B to the network by getting the closest nodes on the road network to these points: my_points = gpd.read_file(my_points) #Firstly, need to convert the coordinates to a tuple of x,y coordinates for my_point in my_points .geometry.values: coords_values = re.findall('[0-9.]+ [0-9.]+', str(my_point )) #convert list of strings separated by spaces, to a list of coords as list. coords = [sub.split(' ') for sub in coords_values ][0] coords_tuple= tuple(map(float, coords )) coords_list.append(coords_tuple) #add this coord tuple as a column in the dataframe my_points["coords_syntax"] = coords_list #this dictionary will be used to select the network nodes. The key is the coordinate tuple, and the attribute will be the 'Name' of the point dict_df_mypoints = home.set_index('coords_syntax')['Name'].to_dict() #select the nearest node points on the road network to these coordinate tuples A = list(G.nodes()) #iterate through dict of coords: my_points for point in dict_df_mypoints : #get closest node (using KDTree) between point and the array of cords A generated above closest_node = A[scipy.spatial.KDTree(A).query(point)[1]] #make these above closest nodes have attribute my_point, with other attributes (dict_df[point]) G.nodes[closest_node]['my_point'] = dict_df[point] So now within all the nodes of my road network, I have 2 representing points A and B respectively. Their node key is the coordinate pair. However, when I run the shortest_path algorithm between A and B I get this error: nx.shortest_path(G,source = (455529.02164326626, 206374.9504608615),target = (454340.3426543578, 207204.53480888018)) >>NetworkXNoPath: No path between (455529.02164326626, 206374.9504608615) and (454340.3426543578, 207204.53480888018) And when I check the edges of these nodes, they are only connected to their 2 neighbours, and no other node: nx.single_source_shortest_path_length(G,source = (455529.02164326626, 206374.9504608615)) >>{(455529.02164326626, 206374.9504608615): 0, >>(455582.6204962559, 206424.4603359318): 1, >>(455596.5948359391, 206455.62556823122): 2} So with my road network, I think I need ALL my nodes to be connected to one another (rather than just to their neighbours) with edges, otherwise the shortest_path won't work. I need to make my network a Complete Graph. However, I have no idea how to do that- it can't be done with Momepy. However I am unsure how to create a NetworkX Graph from scratch whilst preserving the geographical distances between the nodes of the road network - this is what drew me to using Momepy in the first place.
gis.stackexchange.com
August 2, 2025 at 8:08 PM
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?
gis.stackexchange.com
July 29, 2025 at 5:07 AM
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?
gis.stackexchange.com
March 30, 2025 at 3:09 PM
Python: How to convert shapefile of a road network to a Complete Graph network using Networkx
How can I convert a geodataframe of LineStrings to a Complete Graph with NetworkX? I have a geodataframe of a collection of linestrings that geographically represent a road network (purple). I have 2 points (red and blue) representing A and B. I'd like to find the shortest route between A and B travelling along the road network. I have converted the geodataframe to a network using momepy library: road = gpd.read_file(input_road.shp) #need to explode roads, as is multiline exploded = road.explode(index_parts=True) G = momepy.gdf_to_nx(exploded, approach="primal",length='mm_len',multigraph=True) This successfully converts my road network to a networkx multigraph, whilst preserving distance (mm_len) between points. Then I add points A and B to the network by getting the closest nodes on the road network to these points: my_points = gpd.read_file(my_points) #Firstly, need to convert the coordinates to a tuple of x,y coordinates for my_point in my_points .geometry.values: coords_values = re.findall('[0-9.]+ [0-9.]+', str(my_point )) #convert list of strings separated by spaces, to a list of coords as list. coords = [sub.split(' ') for sub in coords_values ][0] coords_tuple= tuple(map(float, coords )) coords_list.append(coords_tuple) #add this coord tuple as a column in the dataframe my_points["coords_syntax"] = coords_list #this dictionary will be used to select the network nodes. The key is the coordinate tuple, and the attribute will be the 'Name' of the point dict_df_mypoints = home.set_index('coords_syntax')['Name'].to_dict() #select the nearest node points on the road network to these coordinate tuples A = list(G.nodes()) #iterate through dict of coords: my_points for point in dict_df_mypoints : #get closest node (using KDTree) between point and the array of cords A generated above closest_node = A[scipy.spatial.KDTree(A).query(point)[1]] #make these above closest nodes have attribute my_point, with other attributes (dict_df[point]) G.nodes[closest_node]['my_point'] = dict_df[point] So now within all the nodes of my road network, I have 2 representing points A and B respectively. Their node key is the coordinate pair. However, when I run the shortest_path algorithm between A and B I get this error: nx.shortest_path(G,source = (455529.02164326626, 206374.9504608615),target = (454340.3426543578, 207204.53480888018)) >>NetworkXNoPath: No path between (455529.02164326626, 206374.9504608615) and (454340.3426543578, 207204.53480888018) And when I check the edges of these nodes, they are only connected to their 2 neighbours, and no other node: nx.single_source_shortest_path_length(G,source = (455529.02164326626, 206374.9504608615)) >>{(455529.02164326626, 206374.9504608615): 0, >>(455582.6204962559, 206424.4603359318): 1, >>(455596.5948359391, 206455.62556823122): 2} So with my road network, I think I need ALL my nodes to be connected to one another (rather than just to their neighbours) with edges, otherwise the shortest_path won't work. I need to make my network a Complete Graph. However, I have no idea how to do that- it can't be done with Momepy. However I am unsure how to create a NetworkX Graph from scratch whilst preserving the geographical distances between the nodes of the road network - this is what drew me to using Momepy in the first place.
gis.stackexchange.com
March 27, 2025 at 3:06 AM