我正在寻找一种从 OpenStreetMap (OSM) 数据中准确检索街道交叉口的方法。我知道有人提出并回答了类似的问题,但我可以从建议的方法中检索到的数据不是很准确。
首先,我知道以下问题:
上述问题的答案表明:
“查询给定边界框中的所有方式,并查找由两种或多种方式共享的节点,如另一个答案中所述。”
我遵循了这个建议并编写了一个 python 脚本,该脚本从我从OpenStreetMap下载的 xml 文件(osm 文件)中提取节点元素。以下是代码:
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 导出的数据运行此代码(例如,我使用从导出区域导出的数据:Min Lat:38.89239,Max Lat:38.89981,Min Lon:-77.03212 和 Max Lon:-77.02119。),它打印出如下所示的坐标:
38.8966440,-77.0259810
38.8973430,-77.0280900
38.9010391,-77.0270309
38.8961050,-77.0319620
...
如果我在谷歌地图上绘制这些坐标,它看起来像:
(我使用http://www.darrinward.com/lat-long/绘制数据。)显然数据包含一些不是交叉点的节点(它们可能是面向两条街道的商店。)
我做错了什么还是这是我可以从 OSM 获得的最佳“交集”数据?感谢您的帮助和评论。
最好的,