简体   繁体   English

将获得的变量输入python中的Overpass API调用

[英]Input obtained variables to an Overpass API call in python

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: 使用我从以下链接获得的代码: http : //python-overpy.readthedocs.org/en/latest/example.html我想输入已经获得的变量,而不是直接输入它们,例如:

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. min_lat等变量的类型为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! 非常感谢任何帮助,因为我对此感到很困惑,并且对所有这些都陌生,谢谢!

You are literally sending the query node(min_lat,min_lon,max_lat,max_lon);out; 您实际上是在发送查询node(min_lat,min_lon,max_lat,max_lon);out; to the Overpass API, min_lat etc. are unchanged. 到Overpass API, min_lat等不变。

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. %s将替换为您在方括号中提供的变量。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM