I am looking for a way to retrieve street intersections accurately from OpenStreetMap (OSM) data. I am aware that similar questions were asked and answered, but the data I could retrieve from the suggested method is not very accurate.
First of all, I am aware of following questions:
The answers to the aforementioned questions suggest to:
"Query all ways in a given bounding box and look for nodes shared by two or more ways as explained in the other answer."
I followed this suggestion and wrote a python script that extracts node elements from an xml file (osm file) that I downloaded from OpenStreetMap. Following is the code:
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
If I run this code with the data I exported from OSM (e.g., I used data exported from Area of Export: Min Lat: 38.89239, Max Lat: 38.89981, Min Lon: -77.03212, and Max Lon: -77.02119.), it prints out coordinates that look like:
38.8966440,-77.0259810
38.8973430,-77.0280900
38.9010391,-77.0270309
38.8961050,-77.0319620
...
If I plot these coordinates on Google Maps, it looks like:
(I used http://www.darrinward.com/lat-long/ to plot data.) Apparently the data contains some nodes that are not intersections (they are probably stores that are facing towards two steets.)
Am I doing something wrong or is this the best "intersection" data I can get from OSM? I appreciate your help and comments.
Best,
First Tipp:
Do not only compare with Google Maps, compare your coordinates mainly with OpenStreetMap visualisation. Especially complex street crossings, although they represent the same road, can be different modelled.
2): Look if you really using the right type of ways: Are that foot paths, mixed with streets? There are various different types, with differnet attributes: accessible for vehicles, etc. In Google MAps, the white roads are that one that are accessible by vehicles
3) Further look, if you dont get house polygons mixed in.