简体   繁体   English

python OSM xml立交桥

[英]python OSM xml overpass

Reading the data from overpass-API, I have no problem getting the basic fields. 从Overpass-API读取数据,获取基本字段没有问题。 From the below example, the lat and lon are easily read. 从下面的示例中,可以轻松读取纬度和经度。 What I cannot manage is the read the various tags with K=xxxx, v=yyyyy ; 我无法管理的是读取带有K = xxxx,v = yyyyy的各种标签; I need to read the one with k="name" so as to build a list of city name, lat, lon. 我需要阅读带有k =“ name”的名称,以便建立城市名称,纬度,经度的列表。

The data included in this document is from www.openstreetmap.org. 本文档中包含的数据来自www.openstreetmap.org。 The data is made available under ODbL. 数据在ODbL下可用。

<node id="31024030" lat="51.0763933" lon="4.7224848">
  <tag k="is_in" v="Antwerpen, Belgium, Europe"/>
  <tag k="is_in:continent" v="Europe"/>
  <tag k="is_in:country" v="Belgium"/>
  <tag k="is_in:province" v="Antwerp"/>
  <tag k="name" v="Heist-op-den-Berg"/>
  <tag k="openGeoDB:auto_update" v="population"/>
  <tag k="openGeoDB:is_in" v="Heist-op-den-Berg,Heist-op-den-Berg,Mechelen,Mechelen,Antwerpen,Antwerpen,Vlaanderen,Vlaanderen,Belgique,Belgique,Europe"/>

Code that I have as yet: 我现在拥有的代码:

import xml.etree.cElementTree as ET
tree = ET.parse('target.osm')
root = tree.getroot()
allnodes=root.findall('node')
for node in allnodes:
 lat=node.get('lat')
 lon=node.get('lon')
 cityname='' # set default in case proper tag not found
 for tag in node.getiterator():
    print tag.attrib
    # add code here to get the cityname
 print lat,lon,cityname

You need to iterate over all children of each node and search for a element with a k="name" attribute: 您需要遍历每个节点的所有子节点并搜索具有k="name"属性的元素:

for tag in node.findall('tag'):
    if tag.attrib['k'] == 'name':
        cityname = tag.attrib['v']

Or by using your get() approach: 或使用您的get()方法:

for tag in node.findall('tag'):
    if tag.get('k') == 'name':
        cityname = tag.get('v')

Note that a node with a name doesn't necessarily represent a city in OSM. 请注意,具有名称的节点不一定代表OSM中的城市。 Instead, a city will have additional tags like place=* . 取而代之的是,一个城市将拥有诸如place = *之类的其他标签。

You might want to consider to make use of a existing OP-API wrapper . 您可能要考虑利用现有的OP-API包装器

If you don't, you would like to use the SAX XML interface for performance. 如果您不这样做,则希望使用SAX XML接口来提高性能。 So you will create a parser class and register for callbacks of XML subelements. 因此,您将创建一个解析器类并注册XML子元素的回调。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python 请求,下载 Overpass Turbo XML - Python Requests, download Overpass Turbo XML 尝试使用Python请求将xml文件发送到OpenStreetMap Overpass API - Trying to send xml file using Python Requests to OpenStreetMap Overpass API 如何使用python从OpenStreetMap网站导出的.osm文件的相同结构中,使用python将OSM文件保存为XML? - How to save OSM file in XML using python in the same structure of the exported .osm file from OpenStreetMap website? 使用 xml.etree.ElementTree 读取 pandas、python 中的 a.osm 文件 - reading a .osm file in pandas, python using xml.etree.ElementTree 如何在python中递归地将OSM键细分为字典树(XML到JSON) - How to recursively subdivide OSM key into dictionary tree in python (XML to JSON) 如何使用libchamplain在Python中加载本地磁贴或OSM XML? - How to use libchamplain to load local tiles or OSM XML in Python? 将获得的变量输入python中的Overpass API调用 - Input obtained variables to an Overpass API call in python 通过python中的XML.osm更改ET.iterpase()时的attrib值 - change attrib value while ET.iterpase() through XML.osm in python 无法通过Python使用Overpass API打印查询 - Failing to print query using Overpass API with Python 用Python控制Blender.osm - Controlling Blender.osm with Python
 
粤ICP备18138465号  © 2020-2025 STACKOOM.COM