[英]python OSM xml overpass
从Overpass-API读取数据,获取基本字段没有问题。 从下面的示例中,可以轻松读取纬度和经度。 我无法管理的是读取带有K = xxxx,v = yyyyy的各种标签; 我需要阅读带有k =“ name”的名称,以便建立城市名称,纬度,经度的列表。
本文档中包含的数据来自www.openstreetmap.org。 数据在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"/>
我现在拥有的代码:
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
您需要遍历每个节点的所有子节点并搜索具有k="name"
属性的元素:
for tag in node.findall('tag'):
if tag.attrib['k'] == 'name':
cityname = tag.attrib['v']
或使用您的get()
方法:
for tag in node.findall('tag'):
if tag.get('k') == 'name':
cityname = tag.get('v')
请注意,具有名称的节点不一定代表OSM中的城市。 取而代之的是,一个城市将拥有诸如place = *之类的其他标签。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.