简体   繁体   English

使用循环查询天桥api

[英]use loop to query overpass api

I have a csv file of bounding boxes, that I loaded using pandas in python. 我有一个边框的csv文件,该文件是我在python中使用熊猫加载的。 The dataframe is df and the column name is coord . 数据帧为df ,列名称为coord Does anyone know how I could create a loop to pass the list to my overpass api query? 有谁知道我如何创建一个循环来将列表传递到我的立交api查询?

I tried this: 我尝试了这个:

import time
import overpass
api = overpass.API(timeout=900)

coords = [df['coord'][0:10]]
for coord in coords:
    results =[]
    params =[]
    params = 'node["power"="generator"]'+coord+';out skel;'
    results = api.Get(params, responseformat="json")
    time.sleep(5.0)

However, I got a multiple requests error. 但是,我收到多个请求错误。

I also tried: 我也尝试过:

x={}
x = (api.Get(param) for param in params)

But that returned a python object ( <generator object <genexpr> at 0x11755c0f8> ) and I need the data as json. 但这返回了一个python对象( <generator object <genexpr> at 0x11755c0f8> ),我需要将数据作为json。

Any help would be much appreciated! 任何帮助将非常感激!

There are a couple of this wrong here mainly with your approach: 这主要是您的方法导致的一些错误:

  1. coords = [df['coord'][0:10]] is not required. coords = [df['coord'][0:10]]不是必需的。 You can replace that with a simpler df['coord'].tolist() 您可以将其替换为更简单的df['coord'].tolist()
  2. You are initializing params with an empty list and then immediately after with a string, why? 您要使用空列表初始化params ,然后使用字符串初始化params ,这是为什么?
  3. Your string to params is what is causing your difficulty. 您的params字符串是造成您困难的原因。 I would use the format method of Python strings to impute the coords in the query. 我将使用Python字符串的format方法在查询中插入坐标。 Doing this is safer because it handles type conversions and proper placement by itself. 这样做比较安全,因为它可以自行处理类型转换和正确的放置。 You cannot simply add a tuple of coordinates to a string and expect it to work. 您不能简单地将一个元组坐标添加到字符串中并期望它起作用。 Try printing the string you are assigning params to in your code, and print the same line from my solution below and you will see the difference. 尝试在代码中打印要为其分配params的字符串,并从下面的解决方案中打印同一行,您将看到区别。 Your code will raise the error TypeError: Can't convert 'tuple' object to str implicitly , which mine will not. 您的代码将引发错误TypeError: Can't convert 'tuple' object to str implicitly ,而我的则不会。

Here is a complete solution: 这是一个完整的解决方案:

import time
import overpass
api = overpass.API(timeout=900)

for coord in df['coord'].tolist():
    params = 'node["power"="generator"]{c};out skel;'.format(c=coord)
    results = api.Get(params, responseformat="json")
    time.sleep(5.0)

Note that if you get an error with the request, please provide a full traceback in your question. 请注意,如果您在请求中遇到错误,请在问题中提供完整的追溯。 That will help us narrow down the real cause of the error. 这将帮助我们缩小错误的真正原因。

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

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