I am looking for a way to accurately get street intersections from OpenStreetMap (OSM) data. I know that similar questions were asked and answered, but the data that I could extract from the proposed method is not very accurate.
First of all, I know the following questions:
Answers to the above questions suggest:
"Request all paths in a given bounding box and look for nodes shared by two or more methods, as described in another answer."
I followed this suggestion and wrote a python script that extracts node elements from an xml file (osm file), which I downloaded from OpenStreetMap . Below 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 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 that I exported from OSM (for example, I used data exported from the export area: Min Lat: 38.89239, Max Lat: 38.89981, Min Lon: -77.03212 and Max Lon: -77.02119.), It produces coordinates that look like this:
38.8966440,-77.0259810 38.8973430,-77.0280900 38.9010391,-77.0270309 38.8961050,-77.0319620 ...
If I draw these coordinates on Google Maps, it looks like this: 
(I used http://www.darrinward.com/lat-long/ to build the data.) Apparently, the data contains some nodes that are not intersections (they probably store this facing two paths.)
Am I doing something wrong or is this the best “intersection data" I can get from OSM? I appreciate your help and comments.
Best