python - OSMNx : get coordinates of nodes using OSM id

标签 python openstreetmap networkx

我使用 Python 库 OSMNx 在城市旅行的几个步骤之间绘制了一条最佳路线。最后一个变量是a list of OSM ids .

现在,我正在尝试将这条路线保存为 shp 或 json 文件。问题是我需要每个节点的纬度/经度,但我没有找到一个 OSMNx 函数来做到这一点。

我尝试了 get_route_edge_attributes(但坐标不是此函数的有效属性)。有什么方法可以用这个单一 id 获取 OSM 节点的坐标?

提前致谢。

最佳答案

您拥有图中每个节点和边的所有属性。您可以使用以下方法获取节点属性:

G.node[38862848]
#out: {'highway': nan,
# 'lat': 45.3210533,
# 'lon': -122.9790558,
# 'osmid': '38862848',
# 'ref': nan,
# 'x': 501641.47862882155,
# 'y': 5018616.5723966481}

G.node[38862848]['lat']
# out: 45.3210533

要获取边缘属性,您可以使用 G[u][v]:

G[5035130880][4963510289]
# out: 
#{0: {'bridge': 'yes',
#  'geometry': <shapely.geometry.linestring.LineString at 0x7f90ad7d5860>,
#  'highway': 'secondary',
#  'length': 671.332597496,
#  'name': 'Northwest 185th Avenue',
#  'oneway': False,
#  'osmid': [124454683, 24446714, 124454682]}}

所有属性也在图形的 GeoDataFrame 中。 如果您有节点列表,获取所有节点几何形状的最简单方法是:

import osmnx as ox
import networkx as nx

gdf_nodes, gdf_edges = ox.graph_to_gdfs()
path = nx.shortest_path(G, G.nodes()[0], G.nodes()[1])
gdf_nodes.loc[path]
#out: 
#        highway    lat lon    osmid    ref x   y   geometry    traffic_signals
#5035130880 NaN 45.5637 -122.868    5035130880  NaN 510334  5.04558e+06 POINT (510334.0390091945 5045583.999886028) 0
#4963510289 NaN 45.5698 -122.868    4963510289  NaN 510329  5.04625e+06 POINT (510329.3114555664 5046254.728223645) 0
# ... 

输出是一个 GeoDataFrame。

关于python - OSMNx : get coordinates of nodes using OSM id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46238813/

相关文章:

python - 导入 Networkx 包时出错

python - 找到具有以下属性的边,如果您跟随它们,您将必须回到刚刚离开的节点才能到达图形的其余部分

python - 在 scrapy 中提取带有其他文本数据的嵌套标签作为字符串

javascript - OL3 - 检测功能点击时出现问题

iphone - 在 map View 上方添加固定按钮

android - 使用 Open Street Map 在错误的地理点显示覆盖

python - Django count 将查询与 contains 匹配

python - hmmlearn中的HMM模型是如何识别隐藏状态的

python - 如何捕获 IllegalMonthError

python - networkx 子图作为节点