Solved

How to request 4-band basemaps?

  • 19 November 2023
  • 1 reply
  • 34 views

Badge +1

Hello, 

I am trying to figure out how to use either the basemaps api or the orders api to download polygon selections of basemaps. So far, I have this code:
 

 # Order api stuff for basemaps
def download_basemap(self, session, request, download_location, file_prefix):
#POST request to get data
headers = {'content-type': 'application/json'}
response = session.post(self.ORDERS_API_URL, data=json.dumps(request), headers=headers)
print(response.json())
#Construct url for getting request status
order_id = response.json()['id']
order_url = self.ORDERS_API_URL + '/' + order_id

#Check request status, once it's done download data
while(True):
r = session.get(order_url)
response = r.json()
state = response['state']
print(state)
end_states = ['success', 'failed', 'partial']
if state in end_states:
break
time.sleep(10)

#Download data
response = session.get(order_url).json()
mosaic_name = response["products"][0]['mosaic_name']
coordinates = response["products"][0]['geometry']['coordinates']
#print("name and coords:", mosaic_name, coordinates)
#print(json.dumps(response, indent=1))

#Download each file
results = response['_links']['results']
#print(json.dumps(results, indent=1))
for r in results:
download_url = r["location"]

# Save data to file
# https://stackoverflow.com/questions/37573483/progress-bar-while-download-file-over-http-with-requests
#print(r["name"], mosaic_name, coordinates)
download_string_name = r["name"].replace("/", "_")
filesavepath = str(download_location) + "/" + str(file_prefix) + "_" + str(download_string_name)
response = session.get(download_url, stream=True)
total_size_in_bytes = int(response.headers.get('content-length', 0))
block_size = 1024 #1 Kilobyte?
print("-Downloading", filesavepath, "-", total_size_in_bytes/1e9, "gb")

# Download file 1kb at a time
progress_bar = tqdm.tqdm(total=total_size_in_bytes, unit='iB', unit_scale=True, position=0)
with open(filesavepath, 'wb') as file:
for data in response.iter_content(block_size):
progress_bar.update(len(data))
file.write(data)
progress_bar.close()

# 3 band data....
basemapRequest = {
"name": "Basemap order with geometry",
"source_type": "basemaps",
"order_type":"full",
"products": [
{
"mosaic_name": "global_monthly_2022_06_mosaic",
"geometry":{
"type": "Polygon",
"coordinates":[[[14.60, 52.30], [14.61, 52.30], [14.61, 52.31], [14.60, 52.31], [14.60, 52.30]]],
}
}
]
}
API_Wrapper.download_basemap(session, basemapRequest, "../Data/ForestTest8", "testprefix")

However this seems to return a visual basemap (RBG only). I’ve been trying to add fields to the json request like “item_types” and “product_bundle”, but it’s still returning the same visual basemap data. I’m not sure what I’m missing here…

It might be relevant to say that my account is under “Education and Research” and is the ‘Basic’ (free) plan.

icon

Best answer by elyhienrich 31 January 2024, 17:33

View original

1 reply

Userlevel 7
Badge +13

Hi @ZacharyBowyer 

Please see the answer on the post How to order a (SR) basemap?

Let us know if you have any other questions! 

Reply