0

I have a csv file of bounding boxes, that I loaded using pandas in python. The dataframe is df and the column name is coord. Does anyone know how I could create a loop to pass the list to my overpass api query?

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.

Any help would be much appreciated!

Parfait
  • 104,375
  • 17
  • 94
  • 125
zelda26
  • 489
  • 2
  • 10
  • 33
  • Can we see some input data? And desired output? Some of us may not be familiar with overpass module but can help structure your the data. – Parfait Aug 09 '16 at 19:30
  • Thanks! The list of coordinates for the bboxes looks like: [(-30, 170, -20, 180), (-40, 110, -30, 120), (-10, 0, 0, 10), (30, 150, 40, 160,(0, 70, 10, 80), (10, 120, 20, 130),(-30, 130, -20, 140), (40, 20, 50, 30), (70, 60, 80, 70), (30, -160, 40, -150)] The output is a nested dictionary: {"features": [{"geometry": {"coordinates": [116.261146, -33.3416006], "type": "Point"}, "id": 293906560, "properties": {}, "type": "Feature"} – zelda26 Aug 09 '16 at 19:54
  • 1
    @JuliaDills, please edit your question with the data. Don't use a comment for that purpose. – Kartik Aug 09 '16 at 20:47

1 Answers1

2

There are a couple of this wrong here mainly with your approach:

  1. coords = [df['coord'][0:10]] is not required. You can replace that with a simpler df['coord'].tolist()
  2. You are initializing params with an empty list and then immediately after with a string, why?
  3. Your string to params is what is causing your difficulty. I would use the format method of Python strings to impute the coords in the query. 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. Your code will raise the error TypeError: Can't convert 'tuple' object to str implicitly, which mine will not.

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.

Kartik
  • 8,347
  • 39
  • 73