How do I find the nearest road for many objects?

I have an overpass-turbo query that finds roads near mosques: overpass turbo

(This is adapted from an example on the wiki here)

I want a text output that tells me which road is near each mosque.

But if I ask for out tags, I get what is apparently an unordered list of all the objects the query returns: mosque, mosque, road, road, road, road, road, road, road, road, road, road, mosque, mosque, mosque, road, etc.

I’ve found a query How to find nearest road(API), which would work on each mosque individually; but not for 10s or 100s at a time.

Can anyone help?

Thank you.

How about ordering the output:

[out:json][timeout:25];

( nwr["amenity"="place_of_worship"]["religion"="muslim"]({{bbox}});
)->.mosques;

// output objects
.mosques;
out geom;

way(around.mosques:25)[highway][highway!~"^(footway|path)$"]
  (if:  is_tag(name) )->.streets  ;

// then output near objects
.streets;
out geom;
1 Like

Good idea! Thanks.

Works well in about 80% of cases in my small trial.

Some mosques return two streets. Is it possible to return only the nearest street?

The API query I linked says it’s for “nearest street”: I’ve tried putting those terms in this query, but I still get multiple streets per mosque.

Which part does “ordering”? This doesn’t sort anything. For one thing, better exclude structures by [!bridge][!tunnel][!embankment][!cutting][!indoor] (also [highway!=corridor] , but not significant if [highway][name] is used) to be safe.
Overpass doesn’t have a distance function. You can’t calculate it for sorting.
There are also many distance measures, each suitable for different cases. Overpass would need to implement multiple of them, and the user still need to choose which to use.
For completeness, out 1 doesn’t mean it will return the closest. It’s a database operation only.
There are many ideas for these. The most basic hack I thought about before might be to add an integer function, which would allow sorting by abusing the id. That would work for object lengths. But there still needs to be a distance function for the question here. Additional sort orders · Issue #81 · drolbr/Overpass-API · GitHub
In the meanwhile, and actually ever after, use QGIS for these large-scale processing tasks. Or as another hack, abuse Conflation in JOSM to spatial join as a crude GIS suite. JOSM/Plugins/Conflation - OpenStreetMap Wiki
If you need an API or something for programs, there are various options

Fundamentally, remember to consider your purpose. “Nearest” road doesn’t necessarily correspond to the address (reverse geocoding), or access point as a routing target (not all routers are capable of this).

1 Like

Thank you for all that. I will look at QGIS.

My purpose is to find (as best possible) the address of a mosque that has no addr: tags. I realise “nearest road” will not always be correct, but to me it seems better than nothing.

Can you think of a better solution?

Hi.
You probably want to associate each mosque with the street(s) nearby. To do that you need to loop over each mosque, find the streets around, and ‘make’ an object that puts these things together – pretty much a database join.
Here is a request that does precisely that, and produces a CSV result : overpass turbo. You won’t see anything on the map in Overpass Turbo, however you can import that CSV file (or directly use the request) into uMap.

1 Like