0

Using code I obtained from the following link : http://python-overpy.readthedocs.org/en/latest/example.html I want to instead input variables that I have already obtained instead of directly inputting them,such as follows:

import overpy
api = overpy.Overpass()
result = api.query("node(min_lat,min_lon,max_lat,max_lon);out;")
len(result.nodes)

The variables min_lat etc are of type float. This is the error I am getting:

overpy.exception.OverpassBadRequest: Error: line 1: parse error: Unknown query clause 
Error: line 1: parse error: ')' expected - 'min_lat' found. 
Error: line 1: parse error: An empty query is not allowed 
Error: line 1: parse error: Unknown type ";" 
Error: line 1: parse error: An empty query is not allowed 

Any help is greatly appreciated as I am quite stuck and new to all of this thank you!

RyanKilkelly
  • 279
  • 1
  • 4
  • 15

1 Answers1

1

You are literally sending the query node(min_lat,min_lon,max_lat,max_lon);out; to the Overpass API, min_lat etc. are unchanged.

To fill the variables you can use string substitution:

"node(%s, %s, %s, %s);out;" % ( min_lat, min_lon, max_lat, max_lon )

The %s will get replaced with the variables you supply in the brackets.

import overpy
api = overpy.Overpass()

min_lat = 50.745
min_lon = 7.17
max_lat = 50.75
max_lon = 7.18

query = "node(%s, %s, %s, %s);out;" % ( min_lat, min_lon, max_lat, max_lon )
result = api.query(query)

print query
print len(result.nodes)
chrki
  • 6,143
  • 6
  • 35
  • 55
  • Thanks for your help, I think this has worked as there is no errors anymore, however nothing is printing out for some reason. Do you think this is a separate issue or have you any idea why this is happening. Thanks again for your help I really appreciate it! – RyanKilkelly Jan 30 '16 at 16:33
  • @RyanKilkelly The last line of your code (and tutorials) is `len(result.nodes)`, which works in the Python interpreter (in the command line) and will output some number. When running this code via `python script.py` (or in other ways) you have to add `print` like I did in my code, or else it will not display anything. – chrki Jan 30 '16 at 16:38
  • I have print added in just the same as your code but it is still not printing, not sure what the issue could be, but I will keep trying thank you! – RyanKilkelly Jan 30 '16 at 16:44