7

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: enter image description here

(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,

Community
  • 1
  • 1
Kotaro
  • 481
  • 2
  • 9
  • 19
  • I am trying to do something similar to you. Currently I extract all nodes from 'ways' tag for highways of the type motorways (later I will include in other highway types--- primary, secondary etc.). (in a python dictionary with key being the way id and values being the list of nodes) Using you post, I am getting facing the same problem as you. Could you please let me know how can I get all intersecting nodes of the way type----Highway---Motorway only?? Any help will be appreciated :) – Abhishek Thakur Jan 31 '14 at 11:09
  • Here is another similar question on stackoverflow: http://stackoverflow.com/questions/12965090/get-list-of-all-intersections-in-a-city/18385182 – tyr Jan 31 '14 at 11:48

1 Answers1

5

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.

AlexWien
  • 28,470
  • 6
  • 53
  • 83
  • Hi AlexWien, Thank you very much! (1) Yes, I'll keep that in mind. (2) and (3) You were right, I got different types of ways mixed in. I extracted streets and now it got fixed :) Thanks again :) – Kotaro Feb 06 '13 at 03:28
  • 1
    @Kotaro : How did you finally solve this? What is the way to extract streets? – statBeginner Oct 14 '16 at 21:24
  • 1
    @statBeginner : In the above code that extracts `` elements, I ended up filtering out the highway elements that are unnecessary (e.g., `footway`). More specifically, I use `motorway`, `trunk`, `primary`, `secondary`, `tertialy`, and `residential`. See http://wiki.openstreetmap.org/wiki/Key:highway – Kotaro Oct 27 '16 at 22:39