[英]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.