Exemplo n.º 1
0
def test_tile_valid_default(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(cbers, "CBERS_BUCKET", CBERS_BUCKET)

    tile_z = 10
    tile_x = 664
    tile_y = 495
    data, mask = cbers.tile(CBERS_MUX_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 401
    tile_y = 585
    data, mask = cbers.tile(CBERS_AWFI_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 370
    tile_y = 535
    data, mask = cbers.tile(CBERS_PAN10M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 390
    tile_y = 547
    data, mask = cbers.tile(CBERS_PAN5M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 2
0
def test_tile_invalid_bounds(monkeypatch):
    """
    Should raise an error with invalid tile
    """

    monkeypatch.setattr(cbers, 'CBERS_BUCKET', CBERS_BUCKET)

    tile_z = 10
    tile_x = 694
    tile_y = 495

    with pytest.raises(TileOutsideBounds):
        cbers.tile(CBERS_SCENE, tile_x, tile_y, tile_z)
Exemplo n.º 3
0
def tile(
    scene,
    z,
    x,
    y,
    scale=1,
    ext="png",
    bands=None,
    expr=None,
    rescale=None,
    color_formula=None,
    color_map=None,
):
    """Handle tile requests."""
    if ext == "jpg":
        driver = "jpeg"
    elif ext == "jp2":
        driver = "JP2OpenJPEG"
    else:
        driver = ext

    if bands and expr:
        raise CbersTilerError("Cannot pass bands and expression")
    if not bands and not expr:
        raise CbersTilerError("Need bands or expression")

    if bands:
        bands = tuple(bands.split(","))

    tilesize = scale * 256

    if expr is not None:
        tile, mask = expression(scene, x, y, z, expr, tilesize=tilesize)
    elif bands is not None:
        tile, mask = cbers.tile(scene, x, y, z, bands=bands, tilesize=tilesize)

    rtile, rmask = _postprocess(tile,
                                mask,
                                tilesize,
                                rescale=rescale,
                                color_formula=color_formula)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
Exemplo n.º 4
0
def test_tile_valid_custom(monkeypatch):
    """Should return custom band combination tiles."""
    monkeypatch.setattr(cbers, "CBERS_BUCKET", CBERS_BUCKET)

    tile_z = 10
    tile_x = 664
    tile_y = 495
    bands = ("8", "7", "6")
    data, mask = cbers.tile(CBERS_MUX_SCENE,
                            tile_x,
                            tile_y,
                            tile_z,
                            bands=bands)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 401
    tile_y = 585
    bands = ("16", "15", "14")
    data, mask = cbers.tile(CBERS_AWFI_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 370
    tile_y = 535
    bands = ("4", "3", "2")
    data, mask = cbers.tile(CBERS_PAN10M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 390
    tile_y = 547
    bands = ("1", "1", "1")
    data, mask = cbers.tile(CBERS_PAN5M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 5
0
def tile(
    scene: str,
    z: int,
    x: int,
    y: int,
    scale: int = 1,
    ext: str = "png",
    bands: str = None,
    expr: str = None,
    rescale: str = None,
    color_formula: str = None,
    color_map: str = None,
) -> Tuple[str, str, BinaryIO]:
    """Handle tile requests."""
    driver = "jpeg" if ext == "jpg" else ext

    if bands and expr:
        raise CbersTilerError("Cannot pass bands and expression")

    tilesize = scale * 256

    if expr is not None:
        tile, mask = expression(scene, x, y, z, expr=expr, tilesize=tilesize)
    elif bands is not None:
        tile, mask = cbers.tile(scene,
                                x,
                                y,
                                z,
                                bands=tuple(bands.split(",")),
                                tilesize=tilesize)
    else:
        raise CbersTilerError("No bands nor expression given")

    rtile, rmask = _postprocess(tile,
                                mask,
                                rescale=rescale,
                                color_formula=color_formula)

    if color_map:
        color_map = get_colormap(color_map, format="gdal")

    options = img_profiles.get(driver, {})
    return (
        "OK",
        f"image/{ext}",
        array_to_image(rtile,
                       rmask,
                       img_format=driver,
                       color_map=color_map,
                       **options),
    )
Exemplo n.º 6
0
def test_tile_valid_nrg(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(cbers, 'CBERS_BUCKET', CBERS_BUCKET)

    tile_z = 10
    tile_x = 664
    tile_y = 495
    bands = (8, 7, 6)
    data, mask = cbers.tile(CBERS_MUX_SCENE, tile_x, tile_y, tile_z, bands=bands)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
        
    tile_z = 10
    tile_x = 401
    tile_y = 585
    bands = (16, 15, 14)
    data, mask = cbers.tile(CBERS_AWFI_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)

    tile_z = 10
    tile_x = 370
    tile_y = 535
    bands = (4, 3, 2)
    data, mask = cbers.tile(CBERS_PAN10M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
    
    tile_z = 10
    tile_x = 390
    tile_y = 547
    bands = (1, 1, 1)
    data, mask = cbers.tile(CBERS_PAN5M_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 7
0
def test_tile_valid_default(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(cbers, 'CBERS_BUCKET', CBERS_BUCKET)

    tile_z = 10
    tile_x = 664
    tile_y = 495

    data, mask = cbers.tile(CBERS_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 8
0
def test_tile_valid_onband(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(cbers, 'CBERS_BUCKET', CBERS_BUCKET)

    tile_z = 10
    tile_x = 664
    tile_y = 495
    bands = 8

    data, mask = cbers.tile(CBERS_SCENE, tile_x, tile_y, tile_z, rgb=bands)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 9
0
def test_tile_invalid_band(monkeypatch):
    """Should raise an error on invalid band name."""
    monkeypatch.setattr(cbers, "CBERS_BUCKET", CBERS_BUCKET)

    tile_z = 10
    tile_x = 390
    tile_y = 547
    bands = "21"

    with pytest.raises(InvalidBandName):
        data, mask = cbers.tile(CBERS_PAN5M_SCENE,
                                tile_x,
                                tile_y,
                                tile_z,
                                bands=bands)
Exemplo n.º 10
0
def test_tile_valid_oneband(monkeypatch):
    """Test when passing a string instead of a tuple."""
    monkeypatch.setattr(cbers, "CBERS_BUCKET", CBERS_BUCKET)

    tile_z = 10
    tile_x = 390
    tile_y = 547
    bands = "1"

    data, mask = cbers.tile(CBERS_PAN5M_SCENE,
                            tile_x,
                            tile_y,
                            tile_z,
                            bands=bands)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 11
0
def tile(scene, tile_z, tile_x, tile_y, tileformat):
    """Handle tile requests
    """
    query_args = APP.current_request.query_params
    query_args = query_args if isinstance(query_args, dict) else {}

    bands = query_args.get('rgb', '7,6,5')
    bands = tuple(re.findall(r'\d+', bands))

    histoCut = query_args.get('histo', ';'.join(['0,255'] * len(bands)))
    histoCut = re.findall(r'\d+,\d+', histoCut)
    histoCut = list(map(lambda x: list(map(int, x.split(','))), histoCut))

    if len(bands) != len(histoCut):
        raise CbersTilerError(
            'The number of bands doesn\'t match the number of histogramm values'
        )

    tilesize = query_args.get('tile', 256)
    tilesize = int(tilesize) if isinstance(tilesize, str) else tilesize

    tile, mask = cbers.tile(scene,
                            tile_x,
                            tile_y,
                            tile_z,
                            bands,
                            tilesize=tilesize)

    rtile = np.zeros((len(bands), tilesize, tilesize), dtype=np.uint8)
    for bdx in range(len(bands)):
        rtile[bdx] = np.where(
            mask,
            linear_rescale(tile[bdx],
                           in_range=histoCut[bdx],
                           out_range=[0, 255]), 0)

    tile = array_to_img(rtile, tileformat, mask=mask)

    if tileformat == 'jpg':
        tileformat = 'jpeg'

    return ('OK', f'image/{tileformat}', tile)
Exemplo n.º 12
0
def ratio(scene, tile_z, tile_x, tile_y, tileformat):
    """
    Handle processing requests
    """
    query_args = CBERS_APP.current_request.query_params
    query_args = query_args if isinstance(query_args, dict) else {}

    ratio_value = query_args.get('ratio', 'ndvi')

    if ratio_value not in RATIOS.keys():
        raise CbersTilerError('Invalid ratio: {}'.format(ratio_value))

    equation = RATIOS[ratio_value]['eq']
    band_names = list(set(re.findall('b[0-9]{1,2}', equation)))
    bands = tuple(map(lambda x: x.strip('b'), band_names))

    tilesize = query_args.get('tile', 256)
    tilesize = int(tilesize) if isinstance(tilesize, str) else tilesize

    tile = cbers.tile(scene, tile_x, tile_y, tile_z, bands, tilesize=tilesize)
    for bdx, b in enumerate(band_names):
        globals()[b] = tile[bdx]

    tile = np.where(
        reduce(lambda x, y: x * y, [globals()[i] for i in band_names]) > 0,
        np.nan_to_num(ne.evaluate(equation)), -9999)

    range_val = equation = RATIOS[ratio_value]['rg']
    rtile = np.where(
        tile != -9999,
        linear_rescale(tile, in_range=range_val, out_range=[1, 255]),
        0).astype(np.uint8)

    tile = array_to_img(rtile,
                        tileformat,
                        color_map=get_colormap(name='cfastie'))
    if tileformat == 'jpg':
        tileformat = 'jpeg'

    return ('OK', f'image/{tileformat}', tile)