Search code examples
geospatialopenstreetmapgeooverpass-api

How to get only the nearby subset of a way's nodes


I'm using the Overpass API to query Open Street Maps for nearby road segments. I am pretty sure that my query is returning all of the nodes of the nearby way... but I only want nearby nodes of the nearby way.

In the documentation it references this problem:

In general, you will be rather interested in complete data than just elements of a single type. First, there are several valid definitions of what "complete map data" means. The first unclear topic is what to do with nodes outside the bounding box which are members of ways that lie partly inside the bounding box.

The same question repeats for relations. If you wait for a turn restriction, you may prefer to get all elements of the relation included. If your bounding box hits for example the border of Russia, you likely don't want to download ten thousands kilometers of boundary around half the world.

But I looked at the subsequent examples and didn't see the solution.

Basically, in their example, how would I restrict the elements returned to those strictly in the bounding box (rather than returning the whole boundary of Russia)?

My current query is

way (around:100,50.746,7.154) [highway~"^(secondary|tertiary)$"];
>;
out ids geom;

I'm thinking maybe I need to change it to node (around:...) and then recurse upwards to the way to query for the highway tag but I'm not sure if I am even on the right track.


Solution

  • Actually, it's even a bit more complicated, as you need the set intersection of all nodes in a 100m distance and those nodes belonging to one of the relevant ways. Here's how your query should look like: Adjust distance, tags for ways as needed.

    Note that depending on the tagging, there's no guarantee that you will find a node in a certain distance, especially if roads tend to be rather straight and longish. This for sure will impact your results, so a bit experimenting with a suitable radius is probably needed.

    // Find nodes up to 100m around center point 
    // (center is overpass turbo specific for center point lat/lon in current map view)
    node(around:100,{{center}})->.aroundnodes;
    
    // recurse up to ways with highway = secondary/tertiary
    way(bn.aroundnodes)[highway~"^(secondary|tertiary)$"]->.allways;
    
    // determine nodes belonging to found ways
    node(w.allways)->.waynodes;
    
    ( 
    // determine intersection of all ways' nodes and nodes around center point  
      node.waynodes.aroundnodes;  
    // and return ways (intersection is just a workaround for a bug)  
      way.allways.allways; 
     );
    out;
    

    check it out in overpass turbo: http://overpass-turbo.eu/s/hPV