我正在寻找一种从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
Run Code Online (Sandbox Code Playgroud)
如果我运行此代码,我从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
...
Run Code Online (Sandbox Code Playgroud)
如果我在Google地图上绘制这些坐标,它看起来像:
(我用http://www.darrinward.com/lat-long/绘制数据.)显然,数据包含一些节点是不交叉(他们很可能正面临对两种steets店.)
我做错了什么,或者这是我从OSM获得的最佳"交叉"数据?感谢您的帮助和评论.
最好,
第一次提克:
不仅要与Google地图进行比较,还要将您的坐标主要与OpenStreetMap可视化进行比较.特别复杂的街道交叉口虽然代表相同的道路,但可以进行不同的建模.
2):看看你是否真的使用了正确的方式:那条步道与街道混合在一起吗?有各种不同的类型,具有不同的属性:车辆可访问等.在Google MAps中,白色道路是车辆可以访问的道路
3)进一步看,如果你没有混合的房子多边形.