簡體   English   中英

python OSM xml立交橋

[英]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 = *之類的其他標簽。

您可能要考慮利用現有的OP-API包裝器

如果您不這樣做,則希望使用SAX XML接口來提高性能。 因此,您將創建一個解析器類並注冊XML子元素的回調。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM