Last active
August 29, 2015 14:01
-
-
Save danhammer/03ecc7fc6341a9bcff03 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
def landsatID(alert_date, coords, offset_days=30): | |
"""get the ID of the Landsat 8 image that is closest to the | |
supplied alert date within the supplied GEE-formatted polygon | |
""" | |
d = datetime.datetime.strptime(alert_date, '%Y-%m-%d') | |
begin_date = d - datetime.timedelta(days=offset_days) | |
poly = ee.Feature.Polygon(coords) | |
coll = ee.ImageCollection('LANDSAT/LC8_L1T_TOA').filterDate(begin_date, alert_date) | |
coll = coll.filterBounds(poly) | |
# descending sort by acquisition time | |
desc = coll.sort('system:time_start', False).limit(1) | |
try: | |
idx = desc.getInfo()['features'][0]['id'] | |
except IndexError: | |
idx = None | |
print idx | |
return idx | |
def genImage(lon, lat, w, h, date): | |
"""Generates the pan-sharpened Landsat 8 image for the supplied | |
alert and image specs | |
""" | |
coords = createBox(lon, lat, w, h) | |
image_id = landsatID(date, coords) | |
if image_id: | |
loc = 'LANDSAT/%s' % image_id | |
input = ee.Image(loc) | |
rgb = input.select("B4","B3","B2") | |
pan = input.select("B8") | |
sharp = hsvpan(rgb, pan) | |
vis_params = {'min':0.01, 'max':0.5, 'gamma':1.2}; | |
visual_image = sharp.visualize(**vis_params) | |
params = {'scale':30, 'crs':'EPSG:4326', 'region':str(coords)} | |
url = visual_image.getThumbUrl(params) | |
else: | |
url = 'http://i.imgur.com/jRtVWde.png' | |
return url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment