Example #1
0
def landtrend(year_start, year_end, geojson, lang, gc_client, metadata):
    res = landtrend_get_data(year_start, year_end, geojson)

    PLOT_LOCK.acquire()

    plt = landtrend_make_plot(res, year_start, year_end)

    plt.tight_layout()
    f = tempfile.NamedTemporaryFile(suffix='.png').name
    plt.savefig(f, bbox_inches='tight')

    PLOT_LOCK.release()

    h = get_hash(f)

    url = Url(upload_to_google_cloud(gc_client, f), h)

    title = metadata['landtrend_plot']['title'][lang] + ' ({} - {})'.format(year_start, year_end)
    out = ImageryPNG(name='landtrend_plot',
                     lang=lang,
                     title=title,
                     date=[dt.date(year_start, 1, 1), dt.date(year_end, 12, 31)],
                     about=metadata['landtrend_plot']['about'][lang],
                     url=url)
    schema = ImageryPNGSchema()
    return {'landtrend_plot' : schema.dump(out)}
Example #2
0
def greenness_trend(year_start, year_end, geojson, lang, gc_client, metadata):
    start_date = dt.datetime(year_start, 1, 1)
    end_date = dt.datetime(year_end, 12, 31)
    point = ee.Geometry(geojson)
    region = point.buffer(BOX_SIDE / 2).bounds()
    ndvi = []
    for y in range(year_start, year_end + 1):
        ndvi.append(OLI_SR_COLL.filterDate('{}-01-1'.format(y), '{}-12-31'.format(y)) \
                .map(maskL8sr) \
                .map(calculate_ndvi) \
                .mean() \
                .addBands(ee.Image(y).float()) \
                .rename(['ndvi','year']))
    ndvi_coll = ee.ImageCollection(ndvi)

    # Compute linear trend function to predict ndvi based on year (ndvi trend)
    lf_trend = ndvi_coll.select(['year', 'ndvi']).reduce(ee.Reducer.linearFit())
    ndvi_trnd = (lf_trend.select('scale').divide(ndvi[0].select("ndvi"))).multiply(100)
                       
    # Define visualization parameter for ndvi trend and apply them
    p_ndvi_trnd = {'min': -10, 'max': 10, 'palette':['#9b2779','#ffffe0','#006500']}
    map_trnd = ndvi_trnd.visualize(**p_ndvi_trnd)
    map_trnd_mosaic = ee.ImageCollection.fromImages([map_trnd, ee.Image() \
            .int() \
            .paint(point.buffer(BOX_SIDE / 75), 1) \
            .visualize(**{'palette': ['black'], 'opacity': 1})]) \
            .mosaic()
    
    # Reproject ndvi mean image so it can retrieve data from every latitude 
    # without deforming the aoi bounding box
    map_trnd_mosaic = map_trnd_mosaic.reproject(scale=30, crs='EPSG:3857')
    
    # create thumbnail and retrieve it by url 
    trnd_url = map_trnd_mosaic.getThumbUrl({'region': region.getInfo(), 'dimensions': 256})
    trnd_name = 'ndvi_trnd.png'
    request.urlretrieve(trnd_url, trnd_name)    
    # read image and pass it to a 2-d numpy array
    trnd_frame = Image.open(trnd_name)
    ndvi_arr_trnd = np.array(trnd_frame)

    legend = Image.open(os.path.join(pathlib.Path(__file__).parent.absolute(), 
                             'ndvi_trd_{}.png'.format(lang.lower())))
    title = metadata['greenness_trend']['title'][lang] + ' ({} - {})'.format(year_start, year_end)
    f = plot_image_to_file(ndvi_arr_trnd, title, legend)
    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    about = metadata['greenness_trend']['about'][lang].format(YEAR_START=start_date.strftime('%Y/%m/%d'),
                                                        YEAR_END=end_date.strftime('%Y/%m/%d'))
    out = ImageryPNG(name='greenness_trend',
                     lang=lang,
                     title=title,
                     date=[start_date, end_date],
                     about=about,
                     url=url)
    schema = ImageryPNGSchema()
    return {'greenness_trend' : schema.dump(out)}
Example #3
0
def base_image(year, geojson, lang, gc_client, metadata):
    start_date = dt.datetime(year, 1, 1)
    end_date = dt.datetime(year, 12, 31)
    point = ee.Geometry(geojson)
    region = point.buffer(BOX_SIDE / 2).bounds()

    # Mask out clouds and cloud-shadows in the Landsat image
    range_coll = OLI_SR_COLL.filterDate(start_date.strftime('%Y-%m-%d'),
                                        end_date.strftime('%Y-%m-%d'))
    l8sr_y = (range_coll.map(maskL8sr).median().setDefaultProjection(
        range_coll.first().projection()))

    # Define visualization parameter for ndvi trend and apply them
    p_l8sr = {
        'bands': ['B4', 'B3', 'B2'],
        'min': 0,
        'max': 3000,
        'gamma': 1.5,
    }
    map_l8sr = l8sr_y.visualize(**p_l8sr)
    map_l8sr_mosaic = ee.ImageCollection.fromImages([map_l8sr, ee.Image().int() \
            .paint(point.buffer(BOX_SIDE / 75), 1) \
            .visualize(**{'palette': ['black'], 'opacity': 1})]) \
            .mosaic()

    # Reproject L8 image so it can retrieve data from every latitute
    # without deforming the aoi bounding box
    map_l8sr_mosaic = map_l8sr_mosaic.reproject(scale=30, crs='EPSG:3857')

    # create thumbnail and retrieve it by url
    l8sr_url = map_l8sr_mosaic.getThumbUrl({
        'region': region.getInfo(),
        'dimensions': 256
    })
    l8sr_name = 'l8sr.png'
    request.urlretrieve(l8sr_url, l8sr_name)

    # read image and pass it to a 2-d numpy array
    l8sr_frame = Image.open(l8sr_name)
    np_l8sr = np.array(l8sr_frame)

    title = metadata['base_image']['title'][lang] + ' ({})'.format(year)
    f = plot_image_to_file(np_l8sr, title)
    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    out = ImageryPNG(name='base_image',
                     lang=lang,
                     title=title,
                     date=[start_date, end_date],
                     about=metadata['base_image']['about'][lang],
                     url=url)
    schema = ImageryPNGSchema()
    return {'base_image': schema.dump(out)}
Example #4
0
def greenness(year, geojson, lang, gc_client, metadata):
    start_date = dt.datetime(year, 1, 1)
    end_date = dt.datetime(year, 12, 31)
    point = ee.Geometry(geojson)
    region = point.buffer(BOX_SIDE / 2).bounds()
    ndvi_mean = OLI_SR_COLL.filterDate(start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) \
            .map(maskL8sr) \
            .map(calculate_ndvi) \
            .mean() \
            .addBands(ee.Image(year).float()) \
            .rename(['ndvi','year'])
    
    # Define visualization parameter for ndvi trend and apply them
    p_ndvi_mean = {'bands':'ndvi', 'min': 0.3, 'max': 0.9, 'palette':['#ffffcc','#006600']}
    map_mean = ndvi_mean.visualize(**p_ndvi_mean)
    map_mean_mosaic = ee.ImageCollection.fromImages([map_mean, ee.Image() \
            .int() \
            .paint(point.buffer(BOX_SIDE / 75), 1) \
            .visualize(**{'palette': ['black'], 'opacity': 1})]) \
            .mosaic()
    
    # Reproject ndvi mean image so it can retrieve data from every latitute 
    # without deforming the aoi bounding box
    map_mean_mosaic = map_mean_mosaic.reproject(scale=30, crs='EPSG:3857')
    
    # create thumbnail and retrieve it by url
    mean_url = map_mean_mosaic.getThumbUrl({'region': region.getInfo(), 'dimensions': 256})
    mean_name = 'ndvi_mean.png'
    request.urlretrieve(mean_url, mean_name)
                       
    # read image and pass it to a 2-d numpy array
    mean_frame = Image.open(mean_name)
    np_mean = np.array(mean_frame)

    legend = Image.open(os.path.join(pathlib.Path(__file__).parent.absolute(), 
                                    'ndvi_avg_{}.png'.format(lang.lower())))
    title = metadata['greenness']['title'][lang] + ' ({})'.format(start_date.year)
    f = plot_image_to_file(np_mean, title, legend)
    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    about = metadata['greenness']['about'][lang].format(YEAR_START=start_date.strftime('%Y/%m/%d'),
                                                  YEAR_END=end_date.strftime('%Y/%m/%d'))
    out = ImageryPNG(name='greenness',
                     lang=lang,
                     title=title,
                     date=[start_date, end_date],
                     about=about,
                     url=url)
    schema = ImageryPNGSchema()
    return {'greenness' : schema.dump(out)}
Example #5
0
    def get_urls(self):
        resp = requests.get('https://www.googleapis.com/storage/v1/b/{bucket}/o?prefix={prefix}'.format(bucket=BUCKET, prefix=self.prefix))
        if not resp or resp.status_code != 200:
            raise GEETaskFailure('Failed to list urls for results from {}'.format(self.task))

        items = resp.json()['items']

        if len(items) < 1:
            raise GEETaskFailure('No urls were found for {}'.format(self.task))
        else:
            urls = []
            for item in items:
                urls.append(Url(item['mediaLink'], item['md5Hash']))
            return urls
Example #6
0
def greenness_trend(year_start, year_end, geojson, lang, gc_client):
    start_date = dt.datetime(year_start, 1, 1)
    end_date = dt.datetime(year_end, 12, 31)
    region = ee.Geometry(geojson).buffer(BOX_SIDE / 2).bounds()
    ndvi = []
    for y in range(year_start, year_end + 1):
        ndvi.append(OLI_SR_COLL \
                        .filterDate(start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) \
                        .map(maskL8sr) \
                        .map(calculate_ndvi) \
                        .mean() \
                        .addBands(ee.Image(y).float()) \
                        .rename(['ndvi','year']))
    ndvi_coll = ee.ImageCollection(ndvi)

    # Compute linear trend function to predict ndvi based on year (ndvi trend)
    lf_trend = ndvi_coll.select(['year',
                                 'ndvi']).reduce(ee.Reducer.linearFit())
    ndvi_trnd = (lf_trend.select('scale').divide(
        ndvi[0].select("ndvi"))).multiply(100)
    # Reproject ndvi_mean and ndvi_trnd images so they can be correctly passed to 'sampleRectangle()' function
    ndvi_trnd_reproject = ndvi_trnd.reproject(scale=30, crs='EPSG:3857')
    ndvi_trnd_band = ndvi_trnd_reproject.select('scale').sampleRectangle(
        region)
    # Get individual band arrays.
    ndvi_arr_trnd = ndvi_trnd_band.get('scale')
    # Transfer the arrays from server to client and cast as np array.
    ndvi_arr_trnd = np.array(ndvi_arr_trnd.getInfo()).astype('float64')

    legend = Image.open(
        os.path.join(
            pathlib.Path(__file__).parent.absolute(),
            'ndvi_trd_{}.png'.format(lang.lower())))
    f = plot_image_to_file(ndvi_arr_trnd, 'Greenness Trend', 'PiYG', legend)

    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    out = ImageryPNG(
        name='greenness_trend',
        lang=lang,
        title='This is a title',
        date=[start_date, end_date],
        about=
        "This is some about text, with a link in it to the <a href='http://trends.earth'>Trends.Earth</a> web page.",
        url=url)
    schema = ImageryPNGSchema()
    return {'greenness_trend': schema.dump(out)}
Example #7
0
def base_image(year, geojson, lang, gc_client):
    start_date = dt.datetime(year, 1, 1)
    end_date = dt.datetime(year, 12, 31)
    region = ee.Geometry(geojson).buffer(BOX_SIDE / 2).bounds()

    # Mask out clouds and cloud-shadows in the Landsat image
    range_coll = OLI_SR_COLL.filterDate(start_date.strftime('%Y-%m-%d'),
                                        end_date.strftime('%Y-%m-%d'))
    l8sr_y = (range_coll.map(maskL8sr).median().setDefaultProjection(
        range_coll.first().projection()))

    # Reproject L8 image so it can be correctly passed to 'sampleRectangle()'
    # function
    l8sr_mosaic_bands = l8sr_y.reproject(scale=30, crs='EPSG:3857') \
        .select(['B2', 'B3', 'B4']) \
        .sampleRectangle(region)

    with ThreadPoolExecutor(max_workers=4) as executor:
        res = []
        for b in ['B4', 'B3', 'B2']:
            res.append(executor.submit(get_band, l8sr_mosaic_bands, b))
        # Gather results and concatenate them
        out = {}
        for this_res in as_completed(res):
            out.update(this_res.result())
        rgb_img = np.concatenate((out['B4'], out['B3'], out['B2']), 2)

    # get the image max value
    img_max = np.max(rgb_img)

    # Scale the data to [0, 255] to show as an RGB image.
    rgb_img_plot = (255 * ((rgb_img - 100) / img_max)).astype('uint8')

    f = plot_image_to_file(rgb_img_plot, "Satellite Image")

    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    out = ImageryPNG(
        name='base_image',
        lang=lang,
        title='This is a title',
        date=[start_date, end_date],
        about=
        "This is some about text, with a link in it to the <a href='http://trends.earth'>Trends.Earth</a> web page.",
        url=url)
    schema = ImageryPNGSchema()
    return {'base_image': schema.dump(out)}
Example #8
0
def landtrend(year_start, year_end, geojson, lang, gc_client):
    res = landtrend_get_data(year_start, year_end, geojson)
    plt = landtrend_make_plot(res, year_start, year_end)

    plt.tight_layout()
    f = tempfile.NamedTemporaryFile(suffix='.png').name
    plt.savefig(f, bbox_inches='tight')
    h = get_hash(f)

    url = Url(upload_to_google_cloud(gc_client, f), h)

    #TODO: Need to return date as a period
    out = ImageryPNG(
        name='landtrend_plot',
        lang=lang,
        title='This is a title',
        date=[dt.date(year_start, 1, 1),
              dt.date(year_end, 12, 31)],
        about=
        "This is some about text, with a link in it to the <a href='http://trends.earth'>Trends.Earth</a> web page.",
        url=url)
    schema = ImageryPNGSchema()
    return {'landtrend_plot': schema.dump(out)}
Example #9
0
def greenness(year, geojson, lang, gc_client):
    start_date = dt.datetime(year, 1, 1)
    end_date = dt.datetime(year, 12, 31)
    region = ee.Geometry(geojson).buffer(BOX_SIDE / 2).bounds()
    ndvi_mean = OLI_SR_COLL.filterDate(start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d')) \
            .map(maskL8sr) \
            .map(calculate_ndvi) \
            .mean() \
            .addBands(ee.Image(year).float()) \
            .rename(['ndvi','year'])

    # Reproject ndvi_mean and ndvi_trnd images so they can be correctly passed
    # to 'sampleRectangle()' function
    ndvi_mean_reproject = ndvi_mean.reproject(scale=30, crs='EPSG:3857')
    ndvi_mean_band = ndvi_mean_reproject.select('ndvi').sampleRectangle(region)
    ndvi_arr_mean = np.array(ndvi_mean_band.get('ndvi').getInfo())

    legend = Image.open(
        os.path.join(
            pathlib.Path(__file__).parent.absolute(),
            'ndvi_avg_{}.png'.format(lang.lower())))
    f = plot_image_to_file(ndvi_arr_mean, 'Average Greenness', 'Greens',
                           legend)

    h = get_hash(f)
    url = Url(upload_to_google_cloud(gc_client, f), h)

    out = ImageryPNG(
        name='greenness',
        lang=lang,
        title='This is a title',
        date=[start_date, end_date],
        about=
        "This is some about text, with a link in it to the <a href='http://trends.earth'>Trends.Earth</a> web page.",
        url=url)
    schema = ImageryPNGSchema()
    return {'greenness': schema.dump(out)}