之前,我问了一个问题“如何从 OpenStreetMap 数据中找到街道交叉口的列表? ”。<way>
我修复了上一篇文章中的代码,该代码根据对上一个问题的响应来查找两个或多个 s 共享的节点。它(几乎)现在可以工作,我可以收集街道交叉口的坐标。(仅使用<way>
具有标签属性 'primary'、'secondary'、'residential' 或 'tertiary' 的 s 来查找多个 s 共享的节点<way>
解决了问题。我过滤掉了其他<way>
s,例如带有标签属性“建筑”)
我现在面临的问题是我收集的交叉点坐标并不详尽,我不知道这是由于OpenStreetMap数据的限制还是我的代码仍然混乱。请看下图。
谷歌地图上的大头针表示从 OpenStreeMap 收集的交叉点的纬度/经度坐标。尽管放置的引脚正确定位在交叉点上,但我使用的技术未能找到一些交叉点,例如同一图中红色箭头所指的交叉点。
我用来获取这些坐标的 osm 文件(xml 文件)是:https ://dl.dropboxusercontent.com/u/1047998/WashingtonDC.osm (此文件位于我的保管箱的公用文件夹中。如果它不可用,请见谅。 )
我用来抓取数据的代码如下:
def get_intersections(self, osm, input_type='file'):
"""
This method reads the passed osm file (xml) and finds intersections (nodes that are shared by two or more roads)
:param osm: An osm file or a string from get_osm()
"""
intersection_coordinates = []
if input_type == 'file':
tree = ET.parse(osm)
root = tree.getroot()
children = root.getchildren()
elif input_type == 'str':
tree = ET.fromstring(osm)
children = tree.getchildren()
counter = {}
for child in children:
if child.tag == 'way':
# Check if the way represents a "highway (road)"
# If the way that we are focusing right now is not a road,
# continue without checking any nodes
road = False
road_types = ('primary', 'secondary', 'residential', 'tertiary', 'service')
for item in child:
if item.tag == 'tag' and item.attrib['k'] == 'highway' and item.attrib['v'] in road_types:
road = True
if not road:
continue
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/
for child in children:
if child.tag == 'node' and child.attrib['id'] in intersections:
coordinate = child.attrib['lat'] + ',' + child.attrib['lon']
intersection_coordinates.append(coordinate)
return intersection_coordinates
我感谢任何意见、建议和解决方案。如果您能建议我收集交叉点坐标的任何其他方法,那也很棒。