Exemplo n.º 1
0
def test_mosaic_tiler_with_imageDataClass():
    """Test mosaic tiler."""
    img, _ = mosaic.mosaic_reader(assets, _read_tile, xo, yo, zo)
    assert not img.assets
    assert not img.data
    assert not img.mask

    img, _ = mosaic.mosaic_reader(assets, _read_tile, x, y, z)
    assert img.data.shape == (3, 256, 256)
    assert img.mask.shape == (256, 256)
    assert img.mask.all()
    assert img.data[0][-1][-1] == 8682
    assert len(img.assets) == 1

    assert img.crs == WEB_MERCATOR_TMS.crs
    assert img.bounds == WEB_MERCATOR_TMS.xy_bounds(x, y, z)

    img, assets_used = mosaic.mosaic_reader(
        assets, _read_tile, x, y, z, pixel_selection=defaults.LowestMethod())
    assert assets_used == img.assets == assets
    assert img.crs == WEB_MERCATOR_TMS.crs
    assert img.bounds == WEB_MERCATOR_TMS.xy_bounds(x, y, z)

    img, assets_used = mosaic.mosaic_reader(
        assets,
        _read_preview,
        width=256,
        height=256,
        pixel_selection=defaults.LowestMethod(),
    )
    assert img.data.shape == (3, 256, 256)
    assert img.mask.shape == (256, 256)
    assert assets_used == img.assets == assets
    assert not img.crs
    assert not img.bounds

    bbox = [
        -75.98703377413767, 44.93504283293786, -71.337604723999,
        47.09685599202324
    ]
    with COGReader(assets[0]) as cog:
        crs1 = cog.dataset.crs

    with COGReader(assets[0]) as cog:
        crs2 = cog.dataset.crs

    img, assets_used = mosaic.mosaic_reader(assets,
                                            _read_part,
                                            bbox=bbox,
                                            dst_crs=crs1,
                                            bounds_crs=WGS84_CRS,
                                            max_size=1024)
    assert img.data.shape == (3, 690, 1024)
    assert img.mask.shape == (690, 1024)
    assert img.mask.any()
    assert assets_used == img.assets == assets
    assert img.crs == crs1 == crs2
    assert not img.bounds == bbox
    bbox_in_crs = transform_bounds(WGS84_CRS, crs1, *bbox, densify_pts=21)
    assert img.bounds == bbox_in_crs
Exemplo n.º 2
0
def test_tile_valid_default():
    """Should return a 3 bands array and a full valid mask."""
    with COGReader(COG_NODATA) as cog:
        # Full tile
        img = cog.tile(43, 24, 7)
        assert img.data.shape == (1, 256, 256)
        assert img.mask.all()
        assert img.band_names == ["1"]

        # Validate that Tile and Part gives the same result
        tile_bounds = WEB_MERCATOR_TMS.xy_bounds(43, 24, 7)
        data_part, _ = cog.part(
            tile_bounds,
            bounds_crs=WEB_MERCATOR_TMS.crs,
            width=256,
            height=256,
            max_size=None,
        )
        assert numpy.array_equal(img.data, data_part)

        # Partial tile
        data, mask = cog.tile(42, 24, 7)
        assert data.shape == (1, 256, 256)
        assert not mask.all()

        # Expression
        img = cog.tile(43, 24, 7, expression="b1*2,b1-100")
        assert img.data.shape == (2, 256, 256)
        assert img.band_names == ["b1*2", "b1-100"]

        with pytest.warns(ExpressionMixingWarning):
            img = cog.tile(43, 24, 7, indexes=(1, 2, 3), expression="b1*2")
            assert img.data.shape == (1, 256, 256)
        assert img.band_names == ["b1*2"]

        data, mask = cog.tile(43, 24, 7, indexes=1)
        assert data.shape == (1, 256, 256)

        img = cog.tile(
            43,
            24,
            7,
            indexes=(
                1,
                1,
            ),
        )
        assert img.data.shape == (2, 256, 256)
        assert img.band_names == ["1", "1"]

    # We are using a file that is aligned with the grid so no resampling should be involved
    with COGReader(COG_WEB) as cog:
        img = cog.tile(147, 182, 9)
        img_buffer = cog.tile(147, 182, 9, tile_buffer=10)
        assert img_buffer.width == 276
        assert img_buffer.height == 276
        assert not img.bounds == img_buffer.bounds
        assert numpy.array_equal(img.data, img_buffer.data[:, 10:266, 10:266])
Exemplo n.º 3
0
def test_tile_valid_default():
    """Should return a 3 bands array and a full valid mask."""
    with COGReader(COG_NODATA) as cog:
        # Full tile
        data, mask = cog.tile(43, 24, 7)
        assert data.shape == (1, 256, 256)
        assert mask.all()

        tile_bounds = WEB_MERCATOR_TMS.xy_bounds(43, 24, 7)
        data_part, _ = cog.part(
            tile_bounds,
            bounds_crs=WEB_MERCATOR_TMS.crs,
            width=256,
            height=256,
            max_size=None,
        )
        assert numpy.array_equal(data, data_part)

        # Partial tile
        data, mask = cog.tile(42, 24, 7)
        assert data.shape == (1, 256, 256)
        assert not mask.all()

        # Expression
        data, mask = cog.tile(43, 24, 7, expression="b1*2,b1-100")
        assert data.shape == (2, 256, 256)

        with pytest.warns(ExpressionMixingWarning):
            data, _ = cog.tile(43, 24, 7, indexes=(1, 2, 3), expression="b1*2")
            assert data.shape == (1, 256, 256)

        data, mask = cog.tile(43, 24, 7, indexes=1)
        assert data.shape == (1, 256, 256)

        data, mask = cog.tile(43, 24, 7, indexes=(
            1,
            1,
        ))
        assert data.shape == (2, 256, 256)
Exemplo n.º 4
0
def test_imageData_output():
    """Test ImageData output."""
    with COGReader(COG_NODATA) as cog:
        img = cog.tile(43, 24, 7)
        assert img.data.shape == (1, 256, 256)
        assert img.mask.all()
        assert img.count == 1
        assert img.data_as_image().shape == (256, 256, 1)

        assert numpy.array_equal(~img.as_masked().mask[0] * 255, img.mask)

        assert img.crs == WEB_MERCATOR_TMS.crs
        assert img.bounds == WEB_MERCATOR_TMS.xy_bounds(43, 24, 7)

        meta = parse_img(img.render(img_format="GTiff"))
        assert meta["driver"] == "GTiff"
        assert meta["crs"] == WEB_MERCATOR_TMS.crs
        assert meta["transform"]
        assert meta["count"] == 2

        res = img.render(img_format="NPY")
        arr = numpy.load(BytesIO(res))
        assert numpy.array_equal(arr[0:1], img.data)
        assert numpy.array_equal(arr[1], img.mask)

        img = cog.tile(43, 24, 7)
        assert img.data.dtype == "uint16"

        imgc = img.post_process(color_formula="Gamma R 3.1")
        assert imgc.data.dtype == "uint8"

        imgr = img.post_process(in_range=(img.data.min(), img.data.max()))
        assert not numpy.array_equal(img.data, imgr.data)
        assert imgr.data.dtype == "uint8"
        assert imgr.data.min() == 0
        assert imgr.data.max() == 255
        assert imgr.bounds == img.bounds
        assert imgr.crs == img.crs
        assert imgr.assets == img.assets

        imgc = imgr.post_process(color_formula="Gamma R 3.1")
        assert not numpy.array_equal(imgc.data, imgr.data)
        assert imgc.data.dtype == "uint8"
        assert imgc.bounds == img.bounds
        assert imgc.crs == img.crs
        assert imgc.assets == img.assets

        imgrc = img.post_process(in_range=(img.data.min(), img.data.max()),
                                 color_formula="Gamma R 3.1")
        assert numpy.array_equal(imgc.data, imgrc.data)

        bbox = (
            -56.624124590533825,
            73.52687881825946,
            -56.530950796449005,
            73.50183615350426,
        )
        img = cog.part(bbox)
        assert img.data.shape == (1, 11, 41)
        meta = parse_img(img.render(img_format="GTiff"))
        assert meta["crs"] == WGS84_CRS
        assert img.bounds == bbox

        img = cog.part(bbox, dst_crs=cog.dataset.crs)
        assert img.data.shape == (1, 29, 30)
        meta = parse_img(img.render(img_format="GTiff"))
        assert meta["crs"] == cog.dataset.crs
        assert not img.bounds == bbox

        img = cog.preview(max_size=128)
        assert img.data.shape == (1, 128, 128)
        assert img.bounds == cog.dataset.bounds
        meta = parse_img(img.render(img_format="GTiff"))
        assert meta["crs"] == cog.dataset.crs