将 OSM 节点转换为纬度/经度

时间:2021-07-15 17:33:28

标签: python python-requests openstreetmap

简单描述一下我在做什么 - 我有一个包含约 65,000 条驾驶路线的 CSV 文件,其中包含起始邮政编码和结束邮政编码,我想为每条路线获取沿途的纬度/经度坐标列表。

所以我的第一步是转到 OSRM 项目 API 并使用以下代码取出沿途的节点

route = requests.get(f'http://router.project-osrm.org/route/v1/driving/{StartLL};{EndLL}?alternatives=false&annotations=nodes')
routejson = route.json()
route_nodes = routejson['routes'][0]['legs'][0]['annotation']['nodes']

接下来我想获取这些节点并将它们转换为纬度/经度坐标,但我已经从 openstreetmaps 中找到了一个 API 来执行此操作,但是它将永远循环遍历 66,000 条记录,每个记录都有几百/千个节点.

for node in route_nodes:
    response_xml = requests.get(f'https://api.openstreetmap.org/api/0.6/node/{node}')
    response_xml_as_string = response_xml.content
    responseXml = ET.fromstring(response_xml_as_string)
    for child in responseXml.iter('node'):
        RouteNodeLL.append((float(child.attrib['lat']), float(child.attrib['lon'])))

我知道有一个 .osm.pbf 文件可以下载,但我不知道如何从中提取节点。

有没有办法做到这一点?即使这意味着完全重新设计逻辑/想法?

1 个答案:

答案 0 :(得分:0)

<块引用>

我知道有一个 .osm.pbf 文件可以下载,但我不知道如何从中提取节点。

看看 osmium-toolhow to extract objects by ID

引用手册:

<块引用>

以下命令将从文件中取出节点 17 和 18、路 42 和关系 3:

osmium getid input.osm.pbf n17 n18 w42 r3 -o output.osm.pbf

要获取您所在地区的 osm.pbf 文件,请查看 OSM wiki,特别是 country and area extracts