Place | Lat | Long |
---|---|---|
Saluhallen | 57,7052433780123 | 11,9678099732456 |
Gothenburg Law Court | 57,7070260802736 | 11,9660123575804 |
Göteborg Centralstasjon | 57,7090180088649 | 11,972294301168 |
Sahlgrenska University Hospital | 57,6829267881712 | 11,9619088391943 |
Skatås motionscentrum | 57,7041859292408 | 12,0357343174424 |
Universitas Regia Gothoburgensis | 57,6985173380122 | 11,9715930268999 |
I have list of locations in Gothenburg, Sweden for which I calculated a route matrix (distance shortest path from each location to all others, only in one direction). Now for a demonstration, I wish to create a shapefile of the entire set of routes, somehow I don’t get how to combine all routes to a graph.
I am aware that I am supposed to use GeoPackage instead, but until now I always failed to open GeoPackage files in ArcMap. Also the problem creating the graph would remain the same…
I would be very grateful for your assistance.
My code looks as follows:
import osmnx as ox
import geopandas as gpd
import pandas as pd
city = 'Göteborg, Sweden'
graph = ox. graph_from_place(city, network_type='walk')
# locations dataframe from excel
filename = 'places.xlsx'
places = pd.read_excel(filename)
# for each place --> closest node in graph
place_nodes = []
for i in range(len(places)):
i_lat = places["Lat"].loc[i]
i_long = places["Long"].loc[i]
i_node = ox.nearest_nodes(graph, i_long, i_lat, return_dist=False)
place_nodes.append(i_node)
all_nodes = set()
for i_from in range(len(place_nodes)):
# only one direction
for i_to in range(i_from+1, len(place_nodes)):
# calc route
dist_nodes = ox.shortest_path(graph, place_nodes[i_from], place_nodes[i_to], weight='length')
print(dist_nodes)
# list of route edges
dist_edges = ox.utils_graph.route_to_gdf(graph, dist_nodes)
print (dist_edges)
# route length
length = dist_edges["length"].sum() / 1000
print("From", places["Place"].loc[i_from], "to", places["Place"].loc[i_to], "it's", length, "km.")
# set of all node ids without duplicates
all_nodes.update(set(dist_nodes) - all_nodes)
# first run
if i_from + i_to == 1:
all_edges = dist_edges
# all other runs_ gdf with all edges in routes
else:
all_edges = gpd.GeoDataFrame(pd.concat([all_edges, dist_edges], ignore_index=True))
nodes, edges = ox.graph_to_gdfs(graph)
# gdf with all nodes in routes
route_nodes = nodes.loc[all_nodes]
all_edges.set_index('osmid', inplace=True)
# create graph, then shapefile
shapegraph = ox.utils_graph.graph_from_gdfs(route_nodes, all_edges, graph_attrs=None)
ox.io.save_graph_shapefile(shapegraph, filepath=None, encoding='utf-8', directed=False)
I believe that reason for the error is hidden in the concatenation, as adding
.drop_duplicates()
to that line also throws an error…
Like I mentioned before, I am extremely grateful for any hint to what I did wrong.