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, 19:09

arkriger's gravatar image

arkriger
851512
accept rate: 0%


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.

permanent link

answered 12 Jul, 21:32

maxerickson's gravatar image

maxerickson
12.6k1080176
accept rate: 31%

result. infinitely more effective. Thank you.

(15 Jul, 20:44) arkriger
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×195

question asked: 12 Jul, 19:09

question was seen: 251 times

last updated: 15 Jul, 20:44

powered by OSQA