7

How to extract intersections in the OpenStreetMap? I need the longitude and latitude of the intersections, thanks!

4 Answers 4

5

There has been a similar question here. There is no direct API call to retrieve intersections. But you can query all ways in a given bounding box (for example directly via the API or via the Overpass API) and look for nodes shared by two or more ways as explained in the other answer.

5

Try the GeoNames findNearestIntersectionOSM API: http://api.geonames.org/findNearestIntersectionOSMJSON?lat=37.451&lng=-122.18&username=demo

The input is lng and lat of location and the response contains lng and lat of nearest intersection:

{"intersection":{...,"lng":"-122.1808293","lat":"37.4506505"}}

It is provided by GeoNames, but seems to be based on OpenStreetMap

3

As @scai perfectly explained, you have to process the raw OSM data yourself to find intersection nodes of ways.

Instead of writing your own program, you can try different algorithms first using the OverpassAPI.

  • Dependent on which kind of ways you are interested in, add the types of ways which should not count as intersection to the regv attribute (at two script sections). The type of ways can be found here: highway tags.
  • The BoundingBox is the part of the map you are viewing on the Overpass-tourbo Website.
  • The script is based on a scripted linked in another question's reply but I rewrote it to make it more readable and added comments (both here and in the original reply).

Sample Script:

<!-- Only select the type of ways you are interested in -->
<query type="way" into="relevant_ways">
  <has-kv k="highway"/>
  <has-kv k="highway" modv="not" regv="footway|cycleway|path|service|track"/>
  <bbox-query {{bbox}}/>
</query>

<!-- Now find all intersection nodes for each way independently -->
<foreach from="relevant_ways" into="this_way">  

  <!-- Get all ways which are linked to this way -->
  <recurse from="this_way" type="way-node" into="this_ways_nodes"/>
  <recurse from="this_ways_nodes" type="node-way" into="linked_ways"/>
  <!-- Again, only select the ways you are interested in, see beginning -->
  <query type="way" into="linked_ways">
    <item set="linked_ways"/>
    <has-kv k="highway"/>
    <has-kv k="highway" modv="not" regv="footway|cycleway|path|service|track"/>
  </query>

  <!-- Get all linked ways without the current way --> 
  <difference into="linked_ways_only">
    <item set="linked_ways"/>
    <item set="this_way"/>
  </difference>
  <recurse from="linked_ways_only" type="way-node" into="linked_ways_only_nodes"/>

  <!-- Return all intersection nodes -->
  <query type="node">
    <item set="linked_ways_only_nodes"/>
    <item set="this_ways_nodes"/>
  </query>
  <print/>
</foreach>
1

In case you have imported OSM data to postgis postgresql with osm2pgsql, you can query intersections like this:

SELECT
  ST_AsText(st_intersection(a.way, b.way)) AS way
FROM
  (select * from planet_osm_line where highway='service') AS a,
  (select * from planet_osm_line where highway='footway') AS b
WHERE
  ST_Intersects(a.way, b.way)
  AND a.osm_id != b.osm_id;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.