Ejemplo n.º 1
0
def test_tile_invalid_bounds(rio):
    """Should raise an error with invalid tile."""
    rio.open = mock_rasterio_open

    tile_z = 8
    tile_x = 177
    tile_y = 89

    with pytest.raises(TileOutsideBounds):
        sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z)
    assert rio.called_once()
Ejemplo n.º 2
0
def test_tile_invalid_bounds(monkeypatch):
    """
    Should raise an error with invalid tile
    """

    monkeypatch.setattr(sentinel2, "SENTINEL_BUCKET", SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 177
    tile_y = 89

    with pytest.raises(TileOutsideBounds):
        sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z)
Ejemplo n.º 3
0
def test_tile_valid_default(rio):
    """Should work as expected."""
    rio.open = mock_rasterio_open

    tile_z = 8
    tile_x = 77
    tile_y = 89

    data, mask = sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 4
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 SentinelTilerError("Cannot pass bands and expression")

    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 = sentinel2.tile(scene,
                                    x,
                                    y,
                                    z,
                                    bands=tuple(bands.split(",")),
                                    tilesize=tilesize)
    else:
        raise SentinelTilerError("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),
    )
Ejemplo n.º 5
0
def test_tile_valid_default(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(sentinel2, 'SENTINEL_BUCKET', SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89

    assert sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y,
                          tile_z).shape == (3, 256, 256)
Ejemplo n.º 6
0
def test_tile_valid_default(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(sentinel2, "SENTINEL_BUCKET", SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89

    data, mask = sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 7
0
def test_tile_valid_nrg(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(sentinel2, 'SENTINEL_BUCKET', SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = ('08', '04', '03')

    assert sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z,
                          rgb=bands).shape == (3, 256, 256)
Ejemplo n.º 8
0
def test_tile_invalid_band(monkeypatch):
    """Should raise an error on invalid band name."""
    monkeypatch.setattr(sentinel2, "SENTINEL_BUCKET", SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = "9A"

    with pytest.raises(InvalidBandName):
        data, mask = sentinel2.tile(SENTINEL_SCENE,
                                    tile_x,
                                    tile_y,
                                    tile_z,
                                    bands=bands)
Ejemplo n.º 9
0
def test_tile_valid_onband(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(sentinel2, 'SENTINEL_BUCKET', SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = '08'

    data, mask = sentinel2.tile(SENTINEL_SCENE, tile_x, tile_y, tile_z, bands=bands)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 10
0
def test_tile_valid_oneband(monkeypatch):
    """Test when passing a string instead of a tuple."""
    monkeypatch.setattr(sentinel2, "SENTINEL_BUCKET", SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = "08"

    data, mask = sentinel2.tile(SENTINEL_SCENE,
                                tile_x,
                                tile_y,
                                tile_z,
                                bands=bands)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 11
0
def test_tile_invalid_band(rio):
    """Should raise an error on invalid band name."""
    rio.open = mock_rasterio_open

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = "9A"

    with pytest.raises(InvalidBandName):
        data, mask = sentinel2.tile(SENTINEL_SCENE,
                                    tile_x,
                                    tile_y,
                                    tile_z,
                                    bands=bands)
    rio.assert_not_called()
Ejemplo n.º 12
0
def test_tile_valid_oneband(rio):
    """Test when passing a string instead of a tuple."""
    rio.open = mock_rasterio_open

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = "08"

    data, mask = sentinel2.tile(SENTINEL_SCENE,
                                tile_x,
                                tile_y,
                                tile_z,
                                bands=bands)
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 13
0
def test_tile_valid_nrg(rio):
    """Should work as expected"""
    rio.open = mock_rasterio_open

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = ("08", "04", "03")

    data, mask = sentinel2.tile(SENTINEL_SCENE,
                                tile_x,
                                tile_y,
                                tile_z,
                                bands=bands)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 14
0
def test_tile_valid_nrg(monkeypatch):
    """
    Should work as expected
    """

    monkeypatch.setattr(sentinel2, "SENTINEL_BUCKET", SENTINEL_BUCKET)

    tile_z = 8
    tile_x = 77
    tile_y = 89
    bands = ("08", "04", "03")

    data, mask = sentinel2.tile(SENTINEL_SCENE,
                                tile_x,
                                tile_y,
                                tile_z,
                                bands=bands)
    assert data.shape == (3, 256, 256)
    assert mask.shape == (256, 256)
Ejemplo n.º 15
0
def tile(scene, tile_z, tile_x, tile_y, tileformat):
    """Handle tile requests."""
    if tileformat == "jpg":
        tileformat = "jpeg"

    query_args = APP.current_request.query_params
    query_args = query_args if isinstance(query_args, dict) else {}

    bands = query_args.get("rgb", "04,03,02")
    bands = tuple(re.findall(r"[0-9A]{2}", bands))

    histoCut = query_args.get("histo", "-".join(["0,16000"] * 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 SentinelTilerError(
            "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 = sentinel2.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,
        )
    img = array_to_img(rtile, mask=mask)
    str_img = b64_encode_img(img, tileformat)
    return ("OK", f"image/{tileformat}", str_img)
Ejemplo n.º 16
0
def tile(scene, tile_z, tile_x, tile_y, tileformat):
    """Handle tile requests
    """
    if tileformat == 'jpg':
        tileformat = 'jpeg'

    query_args = APP.current_request.query_params
    query_args = query_args if isinstance(query_args, dict) else {}

    bands = query_args.get('rgb', '04,03,02')
    bands = tuple(re.findall(r'[0-9A]{2}', bands))

    histoCut = query_args.get('histo', '-'.join(['0,16000'] * 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 SentinelTilerError(
            '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 = sentinel2.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)
    img = array_to_img(rtile, mask=mask)
    str_img = b64_encode_img(img, tileformat)
    return ('OK', f'image/{tileformat}', str_img)
Ejemplo n.º 17
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 SentinelTilerError("Cannot pass bands and expression")
    if not bands and not expr:
        raise SentinelTilerError("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 = sentinel2.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),
    )