how to find intersections from OpenStreetMap?

2019-04-12 20:21发布

问题:

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

回答1:

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.



回答2:

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>