Search code examples
pythonlistloopszipflickr

Loop through list and changing indexes


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.


Solution

  • 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)