python OSM xml立交桥
问题描述:
从overpass-API读取数据,我没有问题获取基本字段。从下面的例子中,lat和lon很容易读取。我不能管理的是读取K = xxxx,v = yyyyy;我需要阅读k =“name”的那一个,以便建立一个城市名称lat,lon的列表。python OSM xml立交桥
本文档中包含的数据来自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=*。
+0
第一种方法就像一种魅力 - 我所关注的一切。似乎“说谢谢”在这里是政治上不正确的,所以我只欠你一个......欢呼! – Karlchen9 2014-09-29 16:36:39
忘记包含我的python代码的开始行(但很难确定如何正确格式化它,输入时出错了):import xml.etree.cElementTree as ET tree = ET.parse('target.osm ') root = tree.getroot() –
Karlchen9
2014-09-29 11:59:00
感谢管理员加入到原始问题 - 一个确实在这里得到宠爱。 – Karlchen9 2014-09-29 16:53:03