def analyze_recent_data(lat, lon, start, end, sort_by, bands, bmin, bmax, opacity): """Returns metadata and *first* tile url from GEE for all Sentinel images in date range ('start'-'end') that intersect with lat,lon. #Example of valid inputs (for area focused on Tenerife) lat = -16.589 lon = 28.246 start ='2017-03-01' end ='2017-03-10' """ loop = asyncio.new_event_loop() try: data = RecentTiles.recent_data(lat=lat, lon=lon, start=start, end=end, sort_by=sort_by) data = loop.run_until_complete( RecentTiles.async_fetch(loop, RecentTiles.recent_tiles, data, bands, bmin, bmax, opacity, 'first')) except RecentTilesError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return jsonify(data=serialize_recent_data(data, 'recent_tiles_data')), 200
def analyze(geojson, area_ha): """Analyze BiomassLoss""" logging.info('[ROUTER]: Getting biomassloss v2') if not geojson: return error(status=400, detail='Geojson is required') threshold, begin, end, table = set_params() try: data = BiomassLossService.analyze(geojson=geojson, threshold=threshold, begin=begin, end=end) except BiomassLossError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha if table: return jsonify( data=serialize_biomass_table_v2(data, 'biomass-loss')), 200 else: return jsonify(data=serialize_biomass_v2(data, 'biomass-loss')), 200
def analyze(geojson, area_ha): """Analyze histogram""" if not geojson: return error(status=400, detail='Geojson is required') threshold, begin, end, table = set_params() layer = get_layer() count_pixels = return_pixel_count() if not layer: logging.debug(get_landcover_types()) return error(status=400, detail='Layer type must ' \ 'be one of {}'.format(', '.join(get_landcover_types()))) try: data = HistogramService.analyze(geojson=geojson, threshold=threshold, begin=begin, end=end, layer=layer, count_pixels=count_pixels) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha return jsonify(data=serialize_histogram(data, layer)), 200
def analyze(geojson, area_ha): """Analyze landcover""" layer = get_layer() count_pixels = return_pixel_count() if not layer: return error(status=400, detail='Layer type must ' \ 'be one of {}'.format(', '.join(get_landcover_types()))) if not geojson: return error(status=400, detail='Geojson is required') try: data = LandcoverService.analyze(geojson=geojson, layer=layer, count_pixels=count_pixels) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha logging.info(data) return jsonify(data=serialize_landcover(data, layer)), 200
def analyze(geojson, area_ha): """Analyze Hansen""" if not geojson: return error(status=400, detail='Geojson is required') threshold, begin, end, table = set_params() #logging.info(f'[ROUTER]: umd params thresh={threshold}, {begin}, {end}, {table}') if request.args.get('aggregate_values', '').lower() == 'false': aggregate_values = False else: aggregate_values = True try: data = HansenService.analyze(geojson=geojson, threshold=threshold, begin=begin, end=end, aggregate_values=aggregate_values) except HansenError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error(f"[ROUTER]: {e}") return error(status=500, detail='Generic Error') data['area_ha'] = area_ha logging.error(f"[ROUTER]: dict returned {data}") if table and not aggregate_values: return jsonify(data=serialize_table_umd(data, 'umd')), 200 else: return jsonify(data=serialize_umd(data, 'umd')), 200
def analyze(year): """Generate Landsat tile url for a requested YEAR""" try: data = LandsatTiles.analyze(year=year) except LandsatTilesError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return jsonify(data=serialize_landsat_url(data, 'landsat_tiles_url')), 200
def analyze(year, z, x, y, map_object): """Generate Landsat tile url for a requested YEAR""" try: data = LandsatTiles.analyze(year, z, x, y, map_object) except LandsatTilesError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return redirect(data['url'])
def get_latest(): """Get latest date forma250""" logging.info('[ROUTER]: Getting latest date') try: data = Forma250Service.latest() except FormaError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return jsonify(data=serialize_forma_latest(data, 'forma250gfw')), 200
def analyze(geojson, area_ha): """Analyze Population""" logging.info('[ROUTER]: Population Getting Total') try: data = PopulationService.analyze(geojson=geojson) except PopulationError as e: logging.error(f'[ROUTER]: {e.message}') return error(status=500, detail=e.message) except Exception as e: logging.error(f'[ROUTER]: {str(e)}') return error(status=500, detail='Generic Error') data['area_ha'] = area_ha data['population_density'] = data['population'].get('population_count') / area_ha return jsonify(data=serialize_population(data, 'population')), 200
def analyze(geojson, area_ha): """Analyze Mangrove Biomass""" logging.info('[ROUTER]: Mangrove Getting biomass') try: data = MangroveBiomassService.analyze(geojson=geojson) except MangroveBiomassError as e: logging.error(f'[ROUTER]: {e.message}') return error(status=500, detail=e.message) except Exception as e: logging.error(f'[ROUTER]: {str(e)}') return error(status=500, detail='Generic Error') data['area_ha'] = area_ha data['biomass_density'] = data['biomass'].get('b1') / area_ha return jsonify( data=serialize_mangrove_biomass(data, 'mangrove-biomass')), 200
def analyze(geojson, area_ha): """Call Geodescriber""" try: app = request.args.get('app', 'gfw') lang = request.args.get('lang','en') template = request.args.get('template', '').lower() in ['t', 'true'] gd_result = GeodescriberService.analyze(geojson=geojson, area_ha=area_ha, app=app, lang=lang, template=template) logging.info(f'[Geodescriber ROUTER]: result {gd_result}') return jsonify(data=gd_result), 200 except GeodescriberError as e: logging.error(f'[Geodescriber ROUTER]: {e.message}') return error(status=500, detail=e.message) except Exception as e: logging.error(f'[Geodescriber ROUTER]: {e}') return error(status=500, detail='Generic Error')
def analyze_recent_thumbs(data_array, bands, bmin, bmax, opacity): """Takes an array of JSON objects with a 'source' key for each tile url to be returned. Returns an array of 'source' names and 'thumb_url' values. """ loop = asyncio.new_event_loop() try: data = loop.run_until_complete( RecentTiles.async_fetch(loop, RecentTiles.recent_thumbs, data_array, bands, bmin, bmax, opacity)) except RecentTilesError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return jsonify(data=serialize_recent_url(data, 'recent_thumbs_url')), 200
def wrapper(*args, **kwargs): if request.method == 'GET': #logging.info(f'[decorater - geom]: Getting area by GET {request}') geojson = request.args.get('geojson') if not geojson: return error(status=400, detail='geojson is required') elif request.method == 'POST': geojson = request.get_json().get( 'geojson', None) if request.get_json() else None try: area_ha = AreaService.tabulate_area(geojson) except Exception as e: return error(status=500, detail=str(e)) kwargs["geojson"] = geojson kwargs['area_ha'] = area_ha return func(*args, **kwargs)
def wrapper(*args, **kwargs): if request.method == 'GET': lat = request.args.get('lat') lon = request.args.get('lon') start = request.args.get('start') end = request.args.get('end') sort_by = request.args.get('sort_by', None) bmin = request.args.get('min', None) bmax = request.args.get('max', None) opacity = request.args.get('opacity', 1.0) bands = request.args.get('bands', None) if not lat or not lon or not start or not end: return error( status=400, detail= '[RECENT] Parameters: (lat, lon. start, end) are needed') kwargs["lat"] = lat kwargs["lon"] = lon kwargs["start"] = start kwargs["end"] = end kwargs["sort_by"] = sort_by kwargs["bmin"] = bmin kwargs["bmax"] = bmax kwargs["opacity"] = float(opacity) kwargs["bands"] = bands return func(*args, **kwargs)
def analyze(geojson, area_ha): """Analyze WHRC Biomass""" logging.info('[ROUTER]: WHRC Getting biomass') if not geojson: return error(status=400, detail='A Geojson argument is required') threshold, start, end, table = set_params() logging.info(f'[ROUTER]: whrc biomass params {threshold}, {start}, {end}') try: data = WHRCBiomassService.analyze(geojson=geojson, threshold=threshold) except WHRCBiomassError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha data['biomass_density'] = data['biomass'].get('b1') / area_ha return jsonify(data=serialize_whrc_biomass(data, 'whrc-biomass')), 200
def analyze(geojson, area_ha): """Analyze NLCD Landcover""" logging.info('[ROUTER]: Getting nlcd landcover v2') if not geojson: return error(status=400, detail='Geojson is required') try: data = NLCDLandcover.analyze(geojson=geojson) except NLCDLandcoverError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha logging.info(f'[data]: {data}') return jsonify( data=serialize_nlcd_landcover_v2(data, 'nlcd-landcover')), 200
def wrapper(*args, **kwargs): if request.method == 'GET': try: wdpa_id = request.view_args.get('id') geojson, area_ha = GeostoreService.get_wdpa(wdpa_id) except GeostoreNotFound: return error(status=404, detail='Geostore not found') kwargs["geojson"] = geojson kwargs["area_ha"] = area_ha return func(*args, **kwargs)
def analyze(img_id): """ """ try: data = ClassificationService.classify(img_id=img_id) except ClassificationError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) return jsonify( data=serialize_classifier_output(data, 'recent_tiles_classifier')), 200
def wrapper(*args, **kwargs): if request.method == 'GET': img_id = request.args.get('img_id') if not img_id: return error( status=400, detail= 'An img_id string is needed (from Landsat-8 or Sentinel-2 collections).' ) kwargs["img_id"] = img_id return func(*args, **kwargs)
def wrapper(*args, **kwargs): if request.method == 'GET': try: iso = request.view_args.get('iso') id1 = request.view_args.get('id1') geojson, area_ha = GeostoreService.get_subnational(iso, id1) except GeostoreNotFound: return error(status=404, detail='Geostore not found') kwargs["geojson"] = geojson kwargs["area_ha"] = area_ha return func(*args, **kwargs)
def analyze(geojson, area_ha): """Analyze Soil Carbon""" # logging.info('[ROUTER]: In Soil carbon router') if not geojson: return error(status=400, detail='A Geojson argument is required') threshold, start, end, table = set_params() # logging.info(f'[ROUTER]: soil carbon params {threshold}, {start}, {end}') try: data = SoilCarbonService.analyze(geojson=geojson, threshold=threshold) except soilCarbonError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha data['soil_carbon_density'] = data['total_soil_carbon'].get( 'b1_first') / area_ha return jsonify(data=serialize_soil_carbon(data, 'soil-carbon')), 200
def analyze(geojson, area_ha): """Analyze forma250""" logging.info('[ROUTER]: Getting forma') if not geojson: return error(status=400, detail='Geojson is required') threshold, begin, end, table = set_params() try: data = Forma250Service.analyze(geojson=geojson, start_date=begin, end_date=end) except FormaError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') data['area_ha'] = area_ha return jsonify(data=serialize_forma(data, 'forma250gfw')), 200
def wrapper(*args, **kwargs): if request.method == 'GET': geostore = request.args.get('geostore') if not geostore: return error(status=400, detail='Geostore is required') try: geojson, area_ha = GeostoreService.get(geostore) except GeostoreNotFound: return error(status=404, detail='Geostore not found') elif request.method == 'POST': geojson = request.get_json().get( 'geojson', None) if request.get_json() else None try: area_ha = AreaService.tabulate_area(geojson) except Exception as e: logging.info(f"[middleware geo hash] Exception") return error(status=500, detail=str(e)) kwargs["geojson"] = geojson kwargs["area_ha"] = area_ha return func(*args, **kwargs)
def analyze(lat, lon, start, end): """Generate Sentinel tile url for a requested lat lon point and time prd #Example of valid inputs (for area focused on Tenerife) lat = -16.589 lon = 28.246 start ='2017-03-01' end ='2017-03-10' """ try: data = SentinelTiles.proxy_sentinel(lat=lat, lon=lon, start=start, end=end) except SentinelTilesError as e: logging.error('[ROUTER]: ' + e.message) return error(status=500, detail=e.message) except Exception as e: logging.error('[ROUTER]: ' + str(e)) return error(status=500, detail='Generic Error') return jsonify( data=serialize_sentinel_url(data, 'sentinel_tiles_url')), 200
def wrapper(*args, **kwargs): if request.method == 'GET': lat = request.args.get('lat') lon = request.args.get('lon') start = request.args.get('start') end = request.args.get('end') if not lat or not lon or not start or not end: return error(status=400, detail='Some parameters are needed') kwargs["lat"] = lat kwargs["lon"] = lon kwargs["start"] = start kwargs["end"] = end return func(*args, **kwargs)
def analyze(timeseries, window=None, mc_number=None, bin_number=None): """Analyze Monte Carlo""" if not timeseries: return error(status=400, detail='A timeseries is required') #logging.info(f'[ROUTER MC]: timeseries={timeseries}') try: data = MCAnalysisService.analyze(timeseries=timeseries, window=window, mc_number=mc_number, bin_number=bin_number) #logging.info(f"[MC router] data: {data}") data['mc_number'] = mc_number data['window'] = window data['bin_number'] = bin_number except MCAnalysisError as e: #logging.error(f'[ROUTER MC error]: {e.message}') return error(status=500, detail=e.message) except Exception as e: #logging.error(f'[ROUTER MC generic error]: {e}') return error(status=500, detail='Generic Error') #logging.info(f"[MC router] serialiser: {serialize_mc(data, 'mc_timeseries_analysis')}") return jsonify(data=serialize_mc(data, 'mc_timeseries_analysis')), 200
def composite(geojson, instrument, date_range, thumb_size, classify, band_viz, get_dem, get_stats, show_bounds, cloudscore_thresh): """ Get a composite satellite image for a geostore id . """ try: data = CompositeService.get_composite_image(geojson=geojson, instrument=instrument, \ date_range=date_range, thumb_size=thumb_size, classify=classify, \ band_viz=band_viz, get_stats=get_stats, get_dem=get_dem, \ show_bounds=show_bounds, cloudscore_thresh=cloudscore_thresh) logging.info(f"[ROUTER]: Result {data}") except CompositeError as e: logging.error(f'[ROUTER]: {e.message}') return error(status=500, detail=e.message) return jsonify(serialize_composite_output(data, 'composite_service')), 200
def wrapper(*args, **kwargs): if request.method == 'POST': data_array = request.get_json().get('timeseries') window = request.args.get('window', None) if window: window = int(window) mc_number = request.args.get('mc_number', None) if mc_number: mc_number = int(mc_number) bin_number = request.args.get('bin_number', None) if bin_number: bin_number = int(bin_number) if not data_array: return error(status=400, detail='[MC] Timeseries is required') kwargs["timeseries"] = data_array kwargs["window"] = window kwargs["mc_number"] = mc_number kwargs["bin_number"] = bin_number return func(*args, **kwargs)
def wrapper(*args, **kwargs): if request.method == 'POST': data_array = request.get_json().get('source_data', []) bands = request.args.get('bands', None) if not bands: bands = request.get_json().get('bands', None) bmin = request.args.get('min', None) bmax = request.args.get('max', None) opacity = request.args.get('opacity', 1.0) if not data_array: return error(status=400, detail='[THUMBS] Some parameters are needed') kwargs["data_array"] = data_array kwargs["bands"] = bands kwargs["bmin"] = bmin kwargs["bmax"] = bmax kwargs["opacity"] = float(opacity) return func(*args, **kwargs)
def internal_server_error(e): return error(status=500, detail='Internal Server Error')