def analyze(threshold, geojson, begin, end, layer, count_pixels): """Take a user specified raster (currently only globcover) and a threshold, and return a dictionary of loss pixel count per year and per raster type for a given geometry. TODO: build this out """ try: hansen_asset_id = SETTINGS['gee']['assets']['hansen'] lulc_asset_id = SETTINGS['gee']['assets'][layer] begin = int(begin.split('-')[0][2:]) end = int(end.split('-')[0][2:]) # Grab loss band depending on the threshold of interest loss_band_name = 'loss_{0}'.format(threshold) loss_band = ee.Image(hansen_asset_id).select(loss_band_name) lulc_asset_id = SETTINGS['gee']['assets'][layer] band_name = SETTINGS['gee']['lulc_band'].get(layer, 'b1') lulc_band = ee.Image(lulc_asset_id).select(band_name) masked_lulc = lulc_band.updateMask(loss_band.mask()) combine_image = loss_band.multiply(500).add(masked_lulc) if count_pixels: reducer = ee.Reducer.frequencyHistogram().unweighted() else: reducer = ee.Reducer.frequencyHistogram().unweighted().group() combine_image = combine_image.addBands([ee.Image.pixelArea()]) reduce_args = { 'reducer': reducer, 'geometry': get_region(geojson), 'scale': 27.829872698318393, 'bestEffort': False, 'maxPixels': 1e10 } area_stats = combine_image.reduceRegion(**reduce_args).getInfo() if count_pixels: area_stats = area_stats[loss_band_name] else: area_stats = flatten_area_hist(area_stats) decoded_response = HistogramService.decode_response( begin, end, area_stats, layer) add_lulc_names = lookup(layer, decoded_response, count_pixels) return {'result': add_lulc_names} except Exception as error: logging.error(str(error)) raise HistogramError(message='Error in Histogram Analysis')
def analyze(geojson, layer, count_pixels): """Take a user specified raster (currently only globcover) and return a dictionary of land cover type and pixel count (or area) type for a given geometry. """ try: lulc_asset_id = SETTINGS['gee']['assets'][layer] band_name = SETTINGS['gee']['lulc_band'].get(layer, 'b1') logging.info(lulc_asset_id) logging.info(band_name) lulc_band = ee.Image(lulc_asset_id).select(band_name) if count_pixels: reducer = ee.Reducer.frequencyHistogram().unweighted() else: reducer = ee.Reducer.frequencyHistogram().unweighted().group() lulc_band = lulc_band.addBands([ee.Image.pixelArea()]) reduce_args = { 'reducer': reducer, 'geometry': get_region(geojson), 'scale': 27.829872698318393, 'bestEffort': False, 'maxPixels': 1e13 } area_stats = lulc_band.reduceRegion(**reduce_args).getInfo() if count_pixels: area_stats = area_stats[band_name] if not count_pixels: area_stats = flatten_area_hist(area_stats) add_lulc_names = lookup(layer, area_stats, count_pixels) return {'result': add_lulc_names} except Exception as error: logging.error(str(error)) raise LandcoverError(message='Error in Landcover Analysis')
def analyze(geojson): """ Analyze NLCD Landcover """ try: logging.info(f'[nlcd-landcover-service]: Initialize analysis') # nlcd land cover d = {} # Extract int years to work with for time range valid_years = [{ 'id': 'NLCD2001', 'year': 2001 }, { 'id': 'NLCD2006', 'year': 2006 }, { 'id': 'NLCD2011', 'year': 2011 }, { 'id': 'NLCD2016', 'year': 2016 }] # Gather assets band_name = 'landcover' landcover_asset = SETTINGS.get('gee').get('assets').get( 'us_landcover') image_list = [ ee.Image(f"{landcover_asset}/{year.get('id')}") for year in valid_years ] us_landcover = ee.ImageCollection(image_list).select(band_name) # region = ee.Feature(geojson).geometry() region = get_region(geojson) scale = 30 logging.info( f'[nlcd-landcover-service]: built assets for analysis, using {band_name}' ) # Calculate landcover with a collection operation method stats = us_landcover.map( ImageColIntersect(region, scale, ee.Reducer.frequencyHistogram())).getInfo() logging.info(f'[nlcd-landcover-service]: retreived {stats}') # Format data structure data = [{ 'id': d['id'], 'stats': { k: v * 30 * 30 * 1e-4 for k, v in d['properties'][band_name].items() } } for d in stats['features']] tmp = {} for el in data: year = [y['year'] for y in valid_years if y['id'] == el['id']][0] tmp[year] = lookup('nlcd_landcover', el['stats'], False) d['nlcd_landcover'] = tmp return d except Exception as error: logging.error(str(error)) raise NLCDLandcoverError(message='Error in LandCover Analysis')