OpenStreetMap

Yikes, this stuff is just not terribly … intuitive. I received a helpful response from maxerikson. He said:

Here's an Overpass Turbo query that returns all boundary=administrative that
are inside the current view:

 [out:json][timeout:25][bbox:{{bbox}}];
 relation["boundary"="administrative"];
 out body;
 >;
 out body;

Now, just for giggles, google a question like “specify bbox in overpass?” It is a bit too abstract. It did point me to the OP documentation but I already knew where that was. What I wanted to know was: How do I replace that {{bbox}} above with a bbox defined by coordinates.

It turns out to be:

 relation["boundary"="administrative"](37.3612,-122.082,37.4326,-121.965);out body;

It is not:

 relation["boundary"="administrative"](-122.082,37.3612,-121.96537.4326);out body;

This is wrong even if the documentation insists that the bounding box is “usually” specified in the order: min long, min lat, max long, max lat. Which clearly does not work. Er…..

Well, and then one has the question of how one is going to access the data. I wanted to use python, so I installed overpy.

I finally got this.

 $ cat 1.py
 import overpy
 
 api = overpy.Overpass()
 
 # bbox = min Longitude , min Latitude , max Longitude , max Latitude 
 
 # My box:
 
 #    "minlat": 37.3612,
 #    "minlon": -122.082,
 #    "maxlat": 37.4326,
 #    "maxlon": -121.965
 
 # result = api.query('[out:json][timeout:25]'
 #    '[bbox:{{bbox|-0.489|51.28|0.236|51.686}}];'
 #    'relation["boundary"="administrative"];'
 #    'out body;'
 #    '>;'
 #    'out body;')
 
 #result = api.query('[out:json][timeout:25]'
 #    '[bbox:{{bbox|-122.082|37.3612|-121.965|37.4326}}];'
 #    'relation["boundary"="administrative"];'
 #    'out body;'
 #    '>;'
 #    'out body;')
 
 # result = api.query("node(50.745,7.17,50.75,7.18);out;")
 
 # result = api.query('node'
 #     '["name"~"^Holtorf"](49.7,7.1,50.8,7.25);out body;');

 result = api.query('relation'
                    '["boundary"="administrative"]'
                    '(37.3612,-122.082,37.4326,-121.965);out body;')

 print 'relations %d' % len(result.relations)
 
 for relation in result.relations:
     print ''
     for tag in relation.tags:
         print '"%s" --> "%s"' % (tag, relation.tags.get(tag, "n/a"))
 print ''
 $ 

Wow. That was certainly … easy. Yeah, that’s the ticket. It was easy…..

Discussion

Comment from mmd on 15 August 2015 at 10:14

{{bbox}} is just semantic sugar, which is only defined for overpass turbo (the Web frontend). Overpass API (the server) really has no idea about it.

When you run a query in overpass turbo, {{bbox}} will be automatically replaced by the current bbox before the query is sent to the Overpass API backend.

The translated version will then look like this:

[bbox: 48, 16, 49, 17];

See http://wiki.openstreetmap.org/wiki/Overpass_turbo/Extended_Overpass_Queries for overpass turbo shortcuts.

Log in to leave a comment