OSMNX graph_from_gdfs KeyError: 'x' when converting a Geopackage to a Graph

房东的猫 提交于 2021-02-05 11:35:07

问题


I need to edit data downloaded by osmnx in geopackage format and then import it as a graph for calculating distances, isochrones etc.

Current Process:

  1. Download osm data using ox.graph_from_point
  2. Save to Geopackage edges and nodes using ox.io.save_graph_geopackage, to allow user to add edges, extra roads in QGIS by digitising roads (with snapping) and save edits.
  3. Convert edited edges back to OSMNX as a graph using ox.graph_from_gdfs.

At this point the 'ox.graph_from_gdfs' returns an empty Graph object. It appears to complain that the x attribute doesn't exist but the x and y attributes do exist in the geopackage nodes layer- so I don't understand the error.

Error:

coords = ((n, d["x"], d["y"]) for n, d in G.nodes(data=True))
KeyError: 'x'

Can anyone assist?

Code:

import osmnx as ox
import networkx as nx
import geopandas as gpd
from shapely.geometry import Point, LineString, MultiLineString,Polygon,MultiPolygon
print("OX ver: {}".format(ox.__version__))
print("NX ver: {}".format(nx.__version__))

geopPath = "osmnx_roaddata.gpkg"
xG = -1.08762688688598
yG = 53.9547041755247

orig = (yG,xG)
print(orig)

gdf_edges = gpd.read_file(geopPath, layer='edges')
gdf_nodes = gpd.read_file(geopPath, layer='nodes')
## Test to see if x exists in geodataframe- looks fine
#for index, row in gdf_nodes.iterrows():
#    print("y: {}. x: {}".format(row['y'],row['x']))


print("######## Using Existing geopackage road edges and nodes")

### readthedocs: graph_attrs (dict) – the new G.graph attribute dict; if None, add crs as the only graph-level attribute
## don't know what the graph attribute dict should contain...or if providing the crs object is what is expected...

G = ox.graph_from_gdfs(gdf_nodes,gdf_edges) #, graph_attrs=gdf_nodes.crs)
print("G appears empty....: '{}'".format(G))

origin_node = ox.get_nearest_node(G, orig)
print("Roads geopackage now being used as variable 'G' graph object")

I understand I will probably need to calculate any missing nodes for new roads that have been digitised. But I should still be able to create a valid G graph object using 'ox.graph_from_gdfs' I thought before I encounter that issue. I've tested another geopackage with no additional roads or nodes other than the osmnx downloaded ones and same result.

Using OSMnx 0.16.0, NetworkX 2.5.

geopPath Geopackage Download


回答1:


I will demonstrate how to do this with OSMnx v1.0 because it will be released in two days and provides slightly more robust support for converting GeoPandas GeoDataFrames to a NetworkX MultiDiGraph. See the docs for usage details.

Your problem appears to be that the u, v, and key columns in your edges GeoDataFrame contain null values, presumably from when you created them in QGIS. These are the unique identifiers of an edge and should be non-null integers. Both GeoDataFrames indexes should be unique.

import geopandas as gpd
import osmnx as ox

# create a graph, save as a GeoPackage
fp = 'graph.gpkg'
G = ox.graph_from_point((53.956748, -1.081676))
ox.save_graph_geopackage(G, fp)

# do some stuff in QGIS
# ensure the index attributes are non-null when you're finished
pass

# load GeoPackage as node/edge GeoDataFrames indexed as described in OSMnx docs
gdf_nodes = gpd.read_file(fp, layer='nodes').set_index('osmid')
gdf_edges = gpd.read_file(fp, layer='edges').set_index(['u', 'v', 'key'])
assert gdf_nodes.index.is_unique and gdf_edges.index.is_unique

# convert the node/edge GeoDataFrames to a MultiDiGraph
graph_attrs = {'crs': 'epsg:4326', 'simplified': True}
G2 = ox.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs)


来源:https://stackoverflow.com/questions/65496981/osmnx-graph-from-gdfs-keyerror-x-when-converting-a-geopackage-to-a-graph

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!