我正在尋找一種方法來從OpenStreetMap(OSM)數據中準確檢索準確的街道交叉路口。我意識到類似的問題被問及並得到解答,但我可以從建議的方法中檢索到的數據不是很準確。如何從OpenStreetMap數據中找到街道交叉路口的列表?
首先,我知道以下問題:
上述問題的答案建議:
「查詢所有方法在給定的邊界框中查找由兩種或多種方式共享的節點作爲exp排在另一個答案。「
我遵循了這一建議,並寫道,提取從我從OpenStreetMap下載一個XML文件(OSM文件)節點元素python腳本。以下是代碼:
try:
from xml.etree import cElementTree as ET
except ImportError, e:
from xml.etree import ElementTree as ET
def extract_intersections(osm, verbose=True):
# This function takes an osm file as an input. It then goes through each xml
# element and searches for nodes that are shared by two or more ways.
# Parameter:
# - osm: An xml file that contains OpenStreetMap's map information
# - verbose: If true, print some outputs to terminal.
#
# Ex) extract_intersections('WashingtonDC.osm')
#
tree = ET.parse(osm)
root = tree.getroot()
counter = {}
for child in root:
if child.tag == 'way':
for item in child:
if item.tag == 'nd':
nd_ref = item.attrib['ref']
if not nd_ref in counter:
counter[nd_ref] = 0
counter[nd_ref] += 1
# Find nodes that are shared with more than one way, which
# might correspond to intersections
intersections = filter(lambda x: counter[x] > 1, counter)
# Extract intersection coordinates
# You can plot the result using this url.
# http://www.darrinward.com/lat-long/
intersection_coordinates = []
for child in root:
if child.tag == 'node' and child.attrib['id'] in intersections:
coordinate = child.attrib['lat'] + ',' + child.attrib['lon']
if verbose:
print coordinate
intersection_coordinates.append(coordinate)
return intersection_coordinates
如果我運行此代碼,我從OSM導出的數據(如,我用從出口地區出口數據:最小緯度:38.89239,最大緯度:38.89981,閔咯嗯:-77.03212,和最大經度:-77.02119),它打印出看起來像座標:
38.8966440,-77.0259810
38.8973430,-77.0280900
38.9010391,-77.0270309
38.8961050,-77.0319620
...
如果我繪製谷歌地圖這些座標,它看起來像:
(我用http://www.darrinward.com/lat-long/繪製數據。)顯然,數據包含一些節點是不交叉口(他們很可能正面臨對兩種steets店)
難道我做錯了什麼,或者這是最好的「交集」數據我可以從OSM獲得?我感謝您的幫助和評論。
最佳,
我試圖做類似你的東西。目前,我提取「方式」的類型高速公路的高速公路標籤的所有節點(以後我會包括其他類型的高速公路---小學,中學等)。 (與鍵爲標識和值的方式爲節點列表中選擇一個Python字典) 使用您發佈時我正在面臨着同樣的問題,因爲你。你能告訴我怎樣才能得到所有類型的交叉節點----高速公路---只有高速公路?? 任何幫助將不勝感激:) –
這裏是計算器另一個相似的問題:http://stackoverflow.com/questions/12965090/get-list-of-all-intersections-in-a-city/18385182 – tyr