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 test_recent_tile_url():
    """
    Test a known source returns expected tile url.
    """

    col_data = {
        'source': 'COPERNICUS/S2/20200530T115219_20200530T115220_T28RCS'
    }

    kwargs = {
        'col_data': col_data,
        'bands': None,
        'bmin': None,
        'bmax': None,
        'opacity': None
    }

    method_response = RecentTiles.recent_tiles(**kwargs)
    tile_url = method_response.get('tile_url', '')
    source = method_response.get('source', '')

    assert 'source' in method_response
    assert source in col_data['source']
    assert 'tile_url' in method_response
    assert 'https://earthengine.googleapis.com/v1alpha/projects/earthengine-legacy/maps/' in tile_url
def test_recent_data_type():
    """
    Test a known lat, lon iniput gives back expected metadata response.
    """

    # Canaries Test
    lon = -16.644
    lat = 28.266

    start = '2020-01-01'
    end = '2020-02-01'

    kwargs = {
        'lat': lat,
        'lon': lon,
        'start': start,
        'end': end,
        'sort_by': None
    }

    method_response = RecentTiles.recent_data(**kwargs)
    first_tile = method_response[0]

    spacecraft_list = ['Sentinel', 'Landsat']

    assert len(method_response) > 1
    assert type(first_tile.get('bbox')) == dict
    assert type(first_tile.get('source')) == str
    assert 0 <= first_tile.get('cloud_score') <= 100
    assert type(first_tile.get('spacecraft')) == str
    assert any([
        spacecraft in first_tile.get('spacecraft')
        for spacecraft in spacecraft_list
    ])
    assert type(first_tile.get('date')) == str
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 test_asset_number():
    """
    Test a known source returns expected number of tile urls.
    """

    kwargs = {
        'lat': -9.23750030205276,
        'lon': -49.0543414324293,
        'start': '2019-01-02',
        'end': '2020-01-01',
        'sort_by': None
    }

    point = ee.Geometry.Point(float(kwargs['lon']), float(kwargs['lat']))
    s2_size = ee.ImageCollection('COPERNICUS/S2').filterDate(
        kwargs['start'], kwargs['end']).filterBounds(point).size()
    l8_size = ee.ImageCollection('LANDSAT/LC08/C01/T1_RT_TOA').filterDate(
        kwargs['start'], kwargs['end']).filterBounds(point).size()
    expected_size = s2_size.getInfo() + l8_size.getInfo()

    tiles_data = RecentTiles.recent_data(**kwargs)
    assert len(tiles_data) == expected_size