Created
April 25, 2016 20:29
-
-
Save Seagor/0bf6f1cdefc282de359e6e110065c87e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from planet import api | |
import sys, os | |
import urllib2, httplib | |
%matplotlib inline | |
import matplotlib.pyplot as plt | |
import matplotlib.image as mpimg | |
class NoRedirection(urllib2.HTTPErrorProcessor): | |
def http_response(self, request, response): | |
return response | |
https_response = http_response | |
opener = urllib2.build_opener(NoRedirection) | |
def get_image(scene, scene_type='visual', data_dir="./planet_images"): | |
img_dir = os.path.join(data_dir, scene_type) | |
if not os.path.exists( img_dir ): | |
os.makedirs(img_dir) | |
url = scene['properties']['data']['products'][scene_type]['full'] | |
filename = os.path.join(img_dir, '%s_%s.tif' % (scene['id'], scene_type)) | |
req = urllib2.Request(url, headers={"Authorization": "api-key %s" % api_key}) | |
response = opener.open(req) | |
if 'Location' in response.headers: | |
redirect_url = response.headers['Location'] | |
req = urllib2.Request(redirect_url) | |
response = opener.open(req) | |
CHUNK = 16 * 1024 | |
total = 0 | |
with open(filename, 'wb') as f: | |
while True: | |
chunk = response.read(CHUNK) | |
if not chunk: | |
sys.stdout.flush() | |
break | |
total += CHUNK | |
sys.stdout.flush() | |
sys.stdout.write('\r>> Downloading %s %d' % (filename, total)) | |
f.write(chunk) | |
scenes = {s['id']: s for i, s in data } | |
api_key = "65a347d4358348189a800a5eac3bcde7" | |
data_dir = './san_diego_4_11_16/' | |
scene_type = 'visual' | |
scene_meta = [ scenes[i] for i in scene_ids ] | |
for scene in scene_meta: | |
filename = os.path.join(data_dir, scene_type, scene['id'] + '_' + scene_type + '.tif') | |
if not os.path.exists(filename): | |
get_image(scene, scene_type=scene_type, data_dir=data_dir) | |
sys.stdout.write('Found %s \n' % (filename,)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment