NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum

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%


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, 21:32

maxerickson's gravatar image

maxerickson
12.7k1083176
accept rate: 32%

result. infinitely more effective. Thank you.

(15 Jul '21, 20:44) arkriger

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:

×228

question asked: 12 Jul '21, 19:09

question was seen: 1,692 times

last updated: 15 Jul '21, 20:44

NOTICE: help.openstreetmap.org is no longer in use from 1st March 2024. Please use the OpenStreetMap Community Forum