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:
- Download osm data using ox.graph_from_point
- 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.
- 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