Reputation: 1
I am trying to generate a list of unique pairs from two ranges. I then want to use this list of unique pairs as part of a URL in a loop to download a large batch of data.
I have managed to generate my ranges, and a list of lists combining my two ranges, then a list of unique combinations of my values...but I don't know how to plug this into a loop that uses my code block.
# Going to try and construct a list of lists or table from the range of tile coordinates we need to pull, then we can try to loop through it
# Create ranges
range1 = range(634797, 634876)
range2 = range(776458, 776512)
# Create a list of lists
table_data = [
["Range 1", "Range 2"],
[range1, range2],
[list(range1), list(range2)] # Convert ranges to lists for printing
]
# Print the table
for row in table_data:
print(row)
# Ok this gives us two lists that contain all of our unique tile coordinates, lets try and generate all the unique combinations
combinations = []
for x in range1:
for y in range2:
combinations.append((x, y))
length = len(combinations)
print("The count is: " + str(length))
print(combinations)
# Getting there, combinations now has our full unique list of pairs, need to figure out how to iterate through the combinations in our functional code block
Here is the code that I want to modify to loop while plugging my unique pairs into the two variables I have declared. I tried to setup the variables for the URL, and the file, but don't understand how to bring this all together.
#This is our functional version, we will modify it to loop through a batch of rasters
import requests
myVal1 =
myVal2 =
url = "https://api.nearmap.com/tiles/v3/surveys/6e33c960-4310-11ee-90f9-b3c1b4806389/North/21/" + str(myVal1) + "/" + str(myVal2) + ".jpg?apikey=MYKEY"
headers = {"accept": "image/*"}
response = requests.get(url, headers=headers)
print (response.status_code)
if response.status_code== 200:
with open('/arcgis/home/nearmap/' + str(myVal1) + str(myVal2) +'.jpg', 'wb') as myImage:
myImage.write(response.content)
Upvotes: -1
Views: 41
Reputation: 1
This is what worked for me:
import requests
# Define ranges
range1 = range(634797, 634876)
range2 = range(776458, 776512)
# Generate unique coordinate pairs
combinations = [(x, y) for x in range1 for y in range2]
# API key
API_KEY = "SECRET"
# Loop through all pairs and fetch images
for myVal1, myVal2 in combinations:
url = f"https://api.nearmap.com/tiles/v3/surveys/6e33c960-4310-11ee-90f9-b3c1b4806389/North/21/{myVal1}/{myVal2}.jpg?apikey={API_KEY}"
headers = {"accept": "image/*"}
response = requests.get(url, headers=headers)
if response.status_code == 200:
file_path = f"/arcgis/home/nearmap/{myVal1}_{myVal2}.jpg"
with open(file_path, 'wb') as myImage:
myImage.write(response.content)
print(f"Downloaded: {file_path}")
else:
print(f"Failed to download {myVal1}, {myVal2}: {response.status_code}")
Upvotes: 0