Creating graph using OSMNX from geodataframe and shows error 'TypeError: cannot unpack non-iterable int object'
Creating graph using OSMNX from geodataframe and shows error 'TypeError: cannot unpack non-iterable int object'
我使用 osmnx 下载地图并导出为 geopackages,以便我可以在 QGIS 中对其进行编辑。
编辑好后(主要是把CRS改成GCJ-02),想把编辑好的边和节点导入osmnx做图做一些路由。
我使用
导入了它们
geopandas.read_file(),
并使用
转换为图表
osmnx.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)
这是错误:
road = ox.utils_graph.graph_from_gdfs(nodes, edges)
D:\Programs\Anaconda\envs\myenv\lib\site-packages\osmnx\utils_graph.py:155: UserWarning: discarding the gdf_nodes geometry column, though its values differ from the coordinates in the x and y columns
warnings.warn(
Traceback (most recent call last):
File "C:\Users\Ricedumplings\AppData\Local\Temp\ipykernel_34123169980.py", line 1, in <cell line: 1>
road = ox.utils_graph.graph_from_gdfs(nodes, edges)
File "D:\Programs\Anaconda\envs\myenv\lib\site-packages\osmnx\utils_graph.py", line 169, in graph_from_gdfs
for (u, v, k), attr_vals in zip(gdf_edges.index, gdf_edges.values):
文件 edge.gpkg 看起来像:
edges.iloc[1]
Out[51]:
u 436912662
v 436912681
key 0
osmid [337786597, 337786554, 337786537, 337786507, 3...
oneway True
lanes 3
ref G4202
name 四环路南段
highway motorway
length 1635.288
bridge yes
from 436912681
to 436912662
tunnel
width
maxspeed
access
junction
geometry LINESTRING (104.11850662686128 30.568117399805...
Name: 1, dtype: object
文件 nodes.gpkg 看起来像:
nodes.iloc[1]
Out[52]:
osmid 436912681
y 30.570557
x 104.115972
street_count 3
highway
ref
geometry POINT (104.11850662686128 30.568117399805935)
Name: 1, dtype: object
有人知道出了什么问题吗?谢谢!
我在网上查了一下,看到了一些解决办法,但是都是改代码。又因为我是个业余爱好者,犹豫要不要改osmnx的代码,所以一直没做。
顺便说一句,这是来自 'utils_graph.py' 的错误部分:
# add edges and their attributes to graph, but filter out null attribute
# values so that edges only get attributes with non-null values
attr_names = gdf_edges.columns.to_list()
for (u, v, k), attr_vals in zip(gdf_edges.index, gdf_edges.values):
data_all = zip(attr_names, attr_vals)
data = {name: val for name, val in data_all if isinstance(val, list) or pd.notnull(val)}
G.add_edge(u, v, key=k, **data)
根据您的描述,这个警告是完全可以预料的。如果修改了节点的几何形状,没有让x和y属性保持一致。下面的示例代码最能说明问题。
- 第一次转换回 图表 生成警告
- 确保非规范化列与几何图形一致后的第二次转换不会生成警告
import osmnx as ox
# get a graph and geodataframes
G = ox.graph_from_address('Guangzhou, China', network_type= "drive")
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
# this will make nodes inconsistent, x & y not equal to geometry
gdf_nodes["geometry"] = gdf_nodes.to_crs(gdf_nodes.estimate_utm_crs()).simplify(1000).to_crs(gdf_nodes.crs)
# hey presto - expected warning
ox.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)
# make nodes consistent...
gdf_nodes["x"] = gdf_nodes["geometry"].x
gdf_nodes["y"] = gdf_nodes["geometry"].y
ox.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)
我使用 osmnx 下载地图并导出为 geopackages,以便我可以在 QGIS 中对其进行编辑。 编辑好后(主要是把CRS改成GCJ-02),想把编辑好的边和节点导入osmnx做图做一些路由。
我使用
导入了它们 geopandas.read_file(),
并使用
转换为图表osmnx.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)
这是错误:
road = ox.utils_graph.graph_from_gdfs(nodes, edges)
D:\Programs\Anaconda\envs\myenv\lib\site-packages\osmnx\utils_graph.py:155: UserWarning: discarding the gdf_nodes geometry column, though its values differ from the coordinates in the x and y columns
warnings.warn(
Traceback (most recent call last):
File "C:\Users\Ricedumplings\AppData\Local\Temp\ipykernel_34123169980.py", line 1, in <cell line: 1>
road = ox.utils_graph.graph_from_gdfs(nodes, edges)
File "D:\Programs\Anaconda\envs\myenv\lib\site-packages\osmnx\utils_graph.py", line 169, in graph_from_gdfs
for (u, v, k), attr_vals in zip(gdf_edges.index, gdf_edges.values):
文件 edge.gpkg 看起来像:
edges.iloc[1]
Out[51]:
u 436912662
v 436912681
key 0
osmid [337786597, 337786554, 337786537, 337786507, 3...
oneway True
lanes 3
ref G4202
name 四环路南段
highway motorway
length 1635.288
bridge yes
from 436912681
to 436912662
tunnel
width
maxspeed
access
junction
geometry LINESTRING (104.11850662686128 30.568117399805...
Name: 1, dtype: object
文件 nodes.gpkg 看起来像:
nodes.iloc[1]
Out[52]:
osmid 436912681
y 30.570557
x 104.115972
street_count 3
highway
ref
geometry POINT (104.11850662686128 30.568117399805935)
Name: 1, dtype: object
有人知道出了什么问题吗?谢谢!
我在网上查了一下,看到了一些解决办法,但是都是改代码。又因为我是个业余爱好者,犹豫要不要改osmnx的代码,所以一直没做。
顺便说一句,这是来自 'utils_graph.py' 的错误部分:
# add edges and their attributes to graph, but filter out null attribute
# values so that edges only get attributes with non-null values
attr_names = gdf_edges.columns.to_list()
for (u, v, k), attr_vals in zip(gdf_edges.index, gdf_edges.values):
data_all = zip(attr_names, attr_vals)
data = {name: val for name, val in data_all if isinstance(val, list) or pd.notnull(val)}
G.add_edge(u, v, key=k, **data)
根据您的描述,这个警告是完全可以预料的。如果修改了节点的几何形状,没有让x和y属性保持一致。下面的示例代码最能说明问题。
- 第一次转换回 图表 生成警告
- 确保非规范化列与几何图形一致后的第二次转换不会生成警告
import osmnx as ox
# get a graph and geodataframes
G = ox.graph_from_address('Guangzhou, China', network_type= "drive")
gdf_nodes, gdf_edges = ox.graph_to_gdfs(G)
# this will make nodes inconsistent, x & y not equal to geometry
gdf_nodes["geometry"] = gdf_nodes.to_crs(gdf_nodes.estimate_utm_crs()).simplify(1000).to_crs(gdf_nodes.crs)
# hey presto - expected warning
ox.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)
# make nodes consistent...
gdf_nodes["x"] = gdf_nodes["geometry"].x
gdf_nodes["y"] = gdf_nodes["geometry"].y
ox.utils_graph.graph_from_gdfs(gdf_nodes, gdf_edges, graph_attrs=None)