This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

variable inside overpass query

0

How can I pass a variable inside a overpass-turbo query? Typically (with Spyder) the query would be:

query = """[out:json][timeout:25];
//gather results
(// query part for: “university”
 way['name'='Cape Peninsula University of Technology (Bellville Campus)'];
);//print results
out body;
>;
out skel qt;"""
url = "http://overpass-api.de/api/interpreter"
r = requests.get(url, params={'data': query})
area = osm2geojson.json2geojson(r.json())

I want to read a parameter from a basic json and pass it to the query. Something like:

jparams = json.load(open('params.json'))

query = """[out:json][timeout:25];
//gather results
(// query part for: “university”
way['name'=jparams['read-variable']];
);//print results
out body;
>;
out skel qt;"""
url = "http://overpass-api.de/api/interpreter"
r = requests.get(url, params={'data': query})
area = osm2geojson.json2geojson(r.json())

How would I do this?

asked 12 Jul '21, 19:09

arkriger's gravatar image

arkriger
155131421
accept rate: 0%


One Answer:

1

You want some variation of Python's string format: https://docs.python.org/3/library/stdtypes.html#str.format

The code will be similar to this:

query = """[out:json][timeout:25];
//gather results
(// query part for: “university”
way['name'='{}'];
);//print results
out body;
>;
out skel qt;""".format(jparams['variable'])

There's lots of flexibility in exactly how you put it all together, using a named placeholder, f strings (which are closer to your example probably), and on and on.

answered 12 Jul '21, 21:32

maxerickson's gravatar image

maxerickson
12.7k1083176
accept rate: 32%

result. infinitely more effective. Thank you.

(15 Jul '21, 20:44) arkriger

Source code available on GitHub .