Hi,
Is there a way to get information on in which municipality a certain lean_to shelter is located, directly through Overpass API?
Welcome to the forum!
Assuming you have the OSM-ID of your shelter, try this:
node(id:8790201911);
is_in;
area._[boundary=administrative][admin_level=8];
out tags;
If your shelter is a way instead of a node, then try this (again with the OSM-ID):
way(id:436191619);
>;
is_in;
area._[boundary=administrative][admin_level=8];
out tags;
My examples return an area with name=Weigenheim
, which is the municipality.
Result of the query
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="Overpass API 0.7.62.5 1bd436f1">
<note>The data included in this document is from www.openstreetmap.org. The data is made available under ODbL.</note>
<meta osm_base="2025-04-26T21:27:16Z" areas="2024-12-31T00:49:33Z"/>
<area id="3600356752">
<tag k="admin_level" v="8"/>
<tag k="boundary" v="administrative"/>
<tag k="de:amtlicher_gemeindeschluessel" v="09575179"/>
<tag k="de:regionalschluessel" v="095755519179"/>
<tag k="name" v="Weigenheim"/>
<tag k="type" v="boundary"/>
<tag k="wikidata" v="Q511408"/>
<tag k="wikipedia" v="de:Weigenheim"/>
</area>
</osm>
Edit: If you insert [out:csv(name;false)]
before the first line of the query, then the result will just be “Weigenheim”, without all of the rest.
Maybe I should have added more info to my question. So its kind of the other way around now when I come to think about it.
I’m trying to make a search field where I can search for a city/municipality/county and get all lean_to shelters in the area. So I will not know the OSM-ID of the shelter, but I will know the area and then I’d like to show the shelters in that certain area.
You can even use the Overpass Turbo Wizard for that.
Just enter amenity=shelter and shelter_type=lean_to in "XXX"
(replace XXX with your municipality) into the wizard and it will generate the Overpass query for you:
/*
This has been generated by the overpass-turbo wizard.
The original search was:
“amenity=shelter and shelter_type=lean_to in XXX”
*/
[out:json][timeout:25];
// fetch area “XXX” to search in
{{geocodeArea:XXX}}->.searchArea;
// gather results
nwr["amenity"="shelter"]["shelter_type"="lean_to"](area.searchArea);
// print results
out geom;
This will output all lean_to shelters in your municipality.
However, sometimes this does not work correctly if the name of your municipality exists multiple times. If that happens, search for the relation of your municipality on osm.org, get the ID of the relation and use this query instead:
relation(62407);
map_to_area->.a;
nwr["amenity"="shelter"]["shelter_type"="lean_to"](area.a);
out geom;