The
Overpass language makes it hard to query
OpenStreetMap data that would otherwise be easy to determine with normal SQL. When I tried to learn Overpass, it seemed to me that the language is based on (or even a successor to)
Brainfuck, apparently developed under the influence of LSD.
Querying an Overpass API endpoint
Python
The following snippet gives an idea how an Overpass API endpoint can be queried with
Python:
import requests
query = """
[out:csv(::id,name)];
relation[admin_level="2"];
out;
"""
response = requests.get(
'http://overpass-api.de/api/interpreter',
headers = {'Accept-Charset': 'utf-8'},
params = {'data' : query }
)
response.encoding='utf-8'
with open('overpass-result.csv', 'w', encoding='utf-8') as file:
file.write(response.text)
There is also the
overpy library.
PowerShell
The following snippet gives an idea how an Overpass API endpoint can be queried with
PowerShell:
$query = @'
[out:json];
nwr [name=Freienstein];
out;
'@
$res = invoke-webRequest -method post -body (@{'data' = $query}) https://overpass-api.de/api/interpreter
$resTxt = $res.Content
$resJson = convertFrom-json $resTxt
foreach($elem in $resJson.elements) {
"id: $($elem.id) ($($elem.type))"
if ($elem.type -eq 'node') {
" $($elem.lat) $($elem.lon)"
}
…
}
Timestamp of data
The overpass database does not have the most actual data. The timestamp of the OSM database is returned in a timestamp_osm_base
field:
$ curl -s http://overpass-api.de/api/interpreter --data '[out:json]; way(45593112); out;' | jq -r .osm3s.timestamp_osm_base
2024-05-29T07:10:00Z
Overpass Turbo URL query parameters
Q=query | Run query |
q=base64-encoded-query | Like Q= , but the query is Base64 encoded. |
C= | Latitude/longitude and zoom |
R | Run query (no parameter) |
template=predefined-tempate | Use a predefined template (key , key-type , key-value , key-value-type or type-id ). |
w | Use wizard |