Mismatched numbers of nodes and edges using OSMnx

huangapple go评论59阅读模式
英文:

Mismatched numbers of nodes and edges using OSMnx

问题

我使用OSMnx库下载了旧金山地区的道路网络。我使用graph_to_gdfs()函数直接从图形中转换节点和边缘,与首先使用osmnx.io.save_graph_xml()导出图形,然后使用osmnx.graph_from_xml()函数重新加载相同图形,最后再使用graph_to_gdfs()函数转换图形进行比较,结果得到了不同数量的节点和边缘。通过直接转换图形,我得到了9671个节点和16030条边。但是,首先保存然后重新创建相同的图形,我得到了9649个节点和15975条边。谢谢您的帮助!

英文:

I'm downloading a road network using the OSMnx library for the San Francisco region. I get a different number of nodes and edges of the network using the graph_to_gdfs() function by directly converting to the nodes and edges from the graph compared to first exporting the graph using osmnx.io.save_graph_xml(), then reloading the same graph using osmnx.graph_from_xml() function, and finally converting the graph using the graph_to_gdfs() function. By directly converting the graph, I get 9671 nodes and 16030 edges. But, by first saving and then re-creating the same graph, I get 9649 nodes and 15975 edges. Thanks for your help!

Please see the code below:

import osmnx as ox

utn = ox.settings.useful_tags_node
oxna = ox.settings.osm_xml_node_attrs
oxnt = ox.settings.osm_xml_node_tags
utw = ox.settings.useful_tags_way
oxwa = ox.settings.osm_xml_way_attrs
oxwt = ox.settings.osm_xml_way_tags
utn = list(set(utn + oxna + oxnt))
utw = list(set(utw + oxwa + oxwt))
ox.settings.all_oneway = True
ox.settings.useful_tags_node = utn
ox.settings.useful_tags_way = utw
G = ox.graph_from_bbox(37.708300, 37.813300, -122.517100, -122.355100, network_type='drive')

nodes, edges = ox.utils_graph.graph_to_gdfs(G)
print(nodes.shape)
print(edges.shape)

ox.io.save_graph_xml(G, filepath="./data/SF_Road_Network.osm")

Recreated_G = ox.graph_from_xml('./data/SF_Road_Network.osm')
recreated_nodes, recreated_edges = ox.utils_graph.graph_to_gdfs(Recreated_G)
print(recreated_nodes.shape)
print(recreated_edges.shape)

答案1

得分: 2

你的代码有三个问题。

首先,你在graph_from_bbox函数调用中的northsouth参数位置颠倒了。不过,这个函数可能足够智能,可以在这种情况下弄清楚哪个应该是哪个,以创建一个有效的多边形。

其次,你不应该使用save_graph_xml来将图保存到磁盘以供后续在OSMnx中使用。根据文档

此函数仅存在于允许将图序列化为.osm文件格式的应用程序中,并具有符合此格式的约束。因此,此函数有一种有限的用例,不包括保存/加载图以供后续在OSMnx中分析。要将图保存/加载到磁盘以供以后在OSMnx中使用,应改为使用io.save_graphml和io.load_graphml函数。要从您从其他地方下载或生成的.osm文件加载图,请使用graph.graph_from_xml函数。

第三,你对图进行了两次简化。一次是当你调用graph_from_bbox时(默认情况下)。然后你保存它并在调用graph_from_xml时再次简化(默认情况下)。因此,它们会有差异。如果你在graph_from_xml调用中传递simplify=False,你将得到与原始图中相同数量的节点(但不同的边)。

简而言之,遵循文档中的建议:“要保存/加载图以供以后在OSMnx中使用,请使用io.save_graphml和io.load_graphml函数。”

英文:

Your code has three problems.

First, your north and south arguments are transposed in your graph_from_bbox call. Nevertheless, the function is probably smart enough to figure out which is supposed to be which in this case to make a valid polygon.

Second, you should not use the save_graph_xml to save a graph to disk for subsequent use in OSMnx. From the docs:

> This function exists only to allow serialization to the .osm file format for applications that require it, and has constraints to conform to that. As such, this function has a limited use case which does not include saving/loading graphs for subsequent OSMnx analysis. To save/load graphs to/from disk for later use in OSMnx, use the io.save_graphml and io.load_graphml functions instead. To load a graph from a .osm file that you have downloaded or generated elsewhere, use the graph.graph_from_xml function.

Third, you are simplifying your graph twice. Once (by default) when you call graph_from_bbox. Then you save it and simplify it again (by default) when you call graph_from_xml. Hence they diverge. If you pass simplify=False into your graph_from_xml call, you will have the same number of nodes (but not edges) as in the original graph.

Long story short, follow the documentation: "To save/load graphs to/from disk for later use in OSMnx, use the io.save_graphml and io.load_graphml functions instead."

huangapple
  • 本文由 发表于 2023年6月29日 11:22:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76577877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定