0

I am working on this bit of code here which basically is creating a bbox and splits this bbox into a lot of smaller bboxes since one can only access 4000 metadatas with one request.

#borders of the bbox
longmax = 15.418483 #longitude top right
longmin = 4.953142 #longitude top left
latmax = 54.869808 #latitude top 
latmin = 47.236219 #latitude bottom


#longitude
longstep = longmax - longmin 
longstepx = longstep / 10 #longitudal steps the model shall perfom

print (longstepx)


#latitude
latstep = latmax - latmin
latstepx = latstep / 10 #latitudal steps the model shall perform

print(latstepx)


#create list of steps through coordinates longitude
llong = []
while longmin < longmax:
    longmin+=longstepx
    llong.append(+longmin)

print (len(llong)) #make sure lists have the same lengths


#create list of steps through coordinates latitude
llat = []
while latmin < latmax:
    latmin+=latstepx
    llat.append(+latmin)

print (len(llat)) #make sure lists have the same lengths


#create the URLs and store in list
urls = []
for lat,long,lat1,long1 in (zip(llat, llong,llat[+1],llong[+1])):
    for pages in range (1,5):
        print ("https://api.flickr.com/services/rest/method=flickr.photos.search&format=json&api_key=5..b&nojsoncallback=1&page={}&per_page=500&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(pages,lat,long,lat1,long1))
print (urls)

Works fine until the last part, starting from creating the list "urls". I want the loop to go through the lists llat and llong AND go through these lists, only one value further than the first two.

llong    llat  
4 5 6 7   8 9 10 11

I want it to take with (zip(llong, llat) the values "4" and "8" (which works) and then with (zip(llong[+1], llat[+1]) the values "5" and "9" and insert them into my link. Furthermore I want it to insert pagenumbers. Ideally the loop created a link with four numbers 4,5,8,9. Then I want it to create 4 links with the numbers in the range of 1:5, save the links and go on to next four numbers and so forth.

But that does not work at all...

Puh, I hope I expressed myself clear enough. I am not looking for a ready to use solution. I want to learn since I am very new to python.

Thanks.

four-eyes
  • 10,740
  • 29
  • 111
  • 220

1 Answers1

1

I think you intended to write:

#create the URLs and store in list
urls = []
for lat, long, lat1, long1 in (zip(llat, llong, llat[1:], llong[1:])):
    for page in range (1,5):
        print ("https://api.flickr.com/services/rest/method=flickr.photos.search&format=json&api_key=5..b&nojsoncallback=1&page={}&per_page=500&bbox={},{},{},{}&accuracy=1&has_geo=1&extras=geo,tags,views,description".format(page, lat, long, lat1, long1))

Note the difference:

In [1]: L = [1,2,3]

In [2]: L
Out[2]: [1, 2, 3]

In [3]: L[1]
Out[3]: 2

In [4]: L[1:]
Out[4]: [2, 3]

Also, note that we can replace this:

llong = []
while longmin < longmax:
    longmin+=longstepx
    llong.append(+longmin)

With this:

llong = range(longmin + longstepx, longmax + longstepx, longstepx)

But I imagine that you intended to have this, which would include longmin in your area, and would not include longmax (which is the opposite of your original code).

llong = range(longmin, longmax, longstepx)
Bill Lynch
  • 80,138
  • 16
  • 128
  • 173
  • Hey. I changed it how you suggested and it worked just fine. I am amazed that the for loop under the loop just worked like this. I just realised the `:` and the intending mistake as I posted the question. What I do not understand is your explanation though... – four-eyes Jul 07 '14 at 01:06
  • @Christoph: Perhaps this is useful: http://stackoverflow.com/questions/509211/pythons-slice-notation – Bill Lynch Jul 07 '14 at 01:07
  • I am wondering why the [1:end] not works. Did they change it in 3.3? – four-eyes Jul 07 '14 at 01:33
  • 1. What is the value of `end`? 2. In Python 2.x, the while loop and the `range()` have identical performance. In Python 3.x, the range() will generate the elements of the list on demand, which is nicer for large lists. – Bill Lynch Jul 07 '14 at 01:37
  • Should be a number out of my list. It all should be the calculated numbers from my lists! – four-eyes Jul 07 '14 at 01:38
  • Ah, in the second answere in fred you postet it says this `a[start:end] # items start through end-1`. There should not be any value assigned to `end`, or am I totally mistaken? Ahh, so I might want to change my while loops to range()? – four-eyes Jul 07 '14 at 01:43
  • `end` is an index in the list. So `a[1:5]` would have elements `[1,2,3,4]`. – Bill Lynch Jul 07 '14 at 01:57
  • ahh, I thought it "searches" the end of the list automatically... Alright, I get it :) – four-eyes Jul 07 '14 at 13:31