Skip to content

Instantly share code, notes, and snippets.

@danhammer
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danhammer/03ecc7fc6341a9bcff03 to your computer and use it in GitHub Desktop.
Save danhammer/03ecc7fc6341a9bcff03 to your computer and use it in GitHub Desktop.
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