Skip to content

Instantly share code, notes, and snippets.

@samapriya
Forked from richpsharp/planet_bb_query_demo.py
Last active March 4, 2020 04:09
Show Gist options
  • Select an option

  • Save samapriya/8468c6a8970cd8654f958945e0d30f7e to your computer and use it in GitHub Desktop.

Select an option

Save samapriya/8468c6a8970cd8654f958945e0d30f7e to your computer and use it in GitHub Desktop.
Planet Bounding Box quad query demo for Sam
import argparse
import requests
import sys
import os
from pySmartDL import SmartDL
from planet.api.auth import find_api_key
"""
requirements:
pip install planet requests pySmartDL
"""
try:
planet_api_key = find_api_key()
except:
print('Failed to get Planet Key try: planet init')
sys.exit()
def demo(mosaic_id, dest):
"""Demo of searching & downloading using multiple bounding boxes.
Parameters:
mosaic_id (str): planet mosaic id.
dest: Folder path to download quads.
Returns:
None.
"""
session = requests.Session()
session.auth = (planet_api_key, '')
bb_list = [
[-54.1520512104035, -15.4639278500847, -54.1514396667481, -15.4628317609468],
[-54.1617983579636, -15.4649057932912, -54.1611653566361, -15.4639306481247],
[-52.3038576215937, -15.0286879308609, -52.3029561614206, -15.0261818330127],
[-58.2029619812966, -15.6270690313958, -58.2020124793053, -15.6265808320473],
[-52.3027625901159, -15.6830265092333, -52.3019230586942, -15.6828379972032]]
for lx, ly, ux, uy in bb_list:
bb_query_url = (
'https://api.planet.com/basemaps/v1/mosaics/'
'%s/quads?bbox=%f,%f,%f,%f' % (mosaic_id, lx, ly, ux, uy))
mosaics_json = session.get(
bb_query_url, timeout=5.0)
for item in (mosaics_json.json())['items']:
downlink=item['_links']['download']
r = session.get(downlink,allow_redirects=False)
filelink = r.headers['Location']
filename = str(r.headers['Location']).split('%22')[-2]
localpath = os.path.join(dest,mosaic_id+'_'+filename)
if not os.path.exists(localpath):
print("Downloading: " + str(localpath))
obj = SmartDL(filelink, localpath)
obj.start()
path = obj.get_dest()
else:
print("File already exists SKIPPING: " + str(localpath))
"""
This works for small areas, and you can also check for the fact that there
are no item['_links'].get('_next') else paginate to get all links and download
"""
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Planet quads downloader multigeometry')
parser.add_argument(
'--mosaic_id', type=str, help='Mosaic ID to download: used as prefix to quads',default='4ce5863a-fb3f-4cad-a899-b8c053af1858')
parser.add_argument(
'--dest', help='Destination to download quads',default=None)
args = parser.parse_args()
demo(args.mosaic_id, args.dest)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment