我需要编辑由osmnx下载的数据在重力场格式,然后导入它作为一个图表计算距离,等时表等。
目前的程序:
此时,'ox.graph_from_gdfs‘返回一个空图对象。它似乎抱怨x属性不存在,但x和y属性确实存在于地质灾害节点层--所以我不理解错误。
错误:
coords = ((n, d["x"], d["y"]) for n, d in G.nodes(data=True))
KeyError: 'x'有人能帮忙吗?
代码:
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")据我所知,对于已经数字化的新道路,我可能需要计算任何缺失的节点。但在遇到这个问题之前,我仍然可以使用“ox.graph_from_gdfs”创建一个有效的G图对象。我已经测试了另一个地质公园,除了osmnx下载的道路或节点之外,没有其他的道路或节点,结果也是一样的。
使用OSMnx 0.16.0,NetworkX 2.5。
geopPath 地理资料下载
发布于 2020-12-30 03:42:25
我将演示如何在OSMnx v1.0中做到这一点,因为它将在两天内发布,并为将GeoPandas GeoDataFrames转换为NetworkX MultiDiGraph提供了更健壮的支持。有关使用详细信息,请参阅医生们。
您的问题似乎是,您的边缘中的u、v和key列包含空值,大概是从您在QGIS中创建它们时开始的。这些是边的唯一标识符,应该是非空整数.两个GeoDataFrames索引都应该是唯一的。
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
复制相似问题