Exemplo n.º 1
0
def test_threads():
    """Test mosaic tiler."""
    assets = [asset1, asset2, asset1, asset2, asset1, asset2]

    (tnothread, _), _ = mosaic.mosaic_reader(assets,
                                             _read_tile,
                                             x,
                                             y,
                                             z,
                                             threads=0)
    (tmulti_threads, _), _ = mosaic.mosaic_reader(assets,
                                                  _read_tile,
                                                  x,
                                                  y,
                                                  z,
                                                  threads=1)
    numpy.testing.assert_array_equal(tnothread, tmulti_threads)

    (t, _), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     threads=0,
                                     chunk_size=2)
    assert t.shape == (3, 256, 256)
    (t, _), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     threads=2,
                                     chunk_size=4)
    assert t.shape == (3, 256, 256)
Exemplo n.º 2
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.º 3
0
def test_mosaic_tiler_Stdev():
    """Test Stdev mosaic methods."""
    tile1, _ = _read_tile(assets[0], x, y, z)
    tile2, _ = _read_tile(assets[1], x, y, z)

    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.StdevMethod())
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == numpy.std([tile1[0][-1][-1], tile2[0][-1][-1]])
    assert t[1][-1][-1] == numpy.std([tile1[1][-1][-1], tile2[1][-1][-1]])
    assert t[2][-1][-1] == numpy.std([tile1[2][-1][-1], tile2[2][-1][-1]])
Exemplo n.º 4
0
def test_stac_mosaic_tiler(rio):
    """Test mosaic tiler with STACReader."""
    rio.open = mock_rasterio_open

    def _reader(src_path: str, *args, **kwargs) -> ImageData:
        """Read tile from an asset"""
        with STACReader(src_path) as stac:
            return stac.tile(*args, **kwargs)

    (data, mask), assets_used = mosaic.mosaic_reader(
        [stac_asset],
        _reader,
        71,
        102,
        8,
        assets="green",
        threads=0,
    )
    assert assets_used == [stac_asset]
    assert data.shape == (1, 256, 256)
    assert mask.shape == (256, 256)
Exemplo n.º 5
0
    def tile(  # type: ignore
        self,
        x: int,
        y: int,
        z: int,
        reverse: bool = False,
        **kwargs: Any,
    ) -> Tuple[ImageData, List[str]]:
        """Get Tile from multiple observation."""
        mosaic_assets = self.assets_for_tile(x, y, z)
        if not mosaic_assets:
            raise NoAssetFoundError(f"No assets found for tile {z}-{x}-{y}")

        if reverse:
            mosaic_assets = list(reversed(mosaic_assets))

        def _reader(asset: str, x: int, y: int, z: int, **kwargs: Any) -> ImageData:
            with self.reader(asset, **self.reader_options) as src_dst:
                return src_dst.tile(x, y, z, **kwargs)

        return mosaic_reader(mosaic_assets, _reader, x, y, z, **kwargs)
Exemplo n.º 6
0
def test_mosaic_tiler():
    """Test mosaic tiler."""
    # test with mosaic_tiler for compatibility
    (t, m), assets_used = mosaic.mosaic_tiler(assets, x, y, z, _read_tile)
    assert len(assets_used) == 1
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8682

    # test with default and full covered tile and default options
    (t, m), assets_used = mosaic.mosaic_reader(assets, _read_tile, x, y, z)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8682

    # Test last pixel selection
    assetsr = list(reversed(assets))
    (t, m), _ = mosaic.mosaic_reader(assetsr, _read_tile, x, y, z)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8057

    (t, m), _ = mosaic.mosaic_reader(assets, _read_tile, x, y, z, indexes=1)
    assert t.shape == (1, 256, 256)
    assert m.shape == (256, 256)
    assert t.all()
    assert m.all()
    assert t[0][-1][-1] == 8682

    # Test darkest pixel selection
    (t, m), assets_used = mosaic.mosaic_reader(
        assets, _read_tile, x, y, z, pixel_selection=defaults.LowestMethod())
    assert len(assets_used) == 2
    assert m.all()
    assert t[0][-1][-1] == 8057

    (to, mo), _ = mosaic.mosaic_reader(assets_order,
                                       _read_tile,
                                       x,
                                       y,
                                       z,
                                       pixel_selection=defaults.LowestMethod())
    numpy.testing.assert_array_equal(t[0, m], to[0, mo])

    # Test brightest pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.HighestMethod())
    assert m.all()
    assert t[0][-1][-1] == 8682

    (to,
     mo), _ = mosaic.mosaic_reader(assets_order,
                                   _read_tile,
                                   x,
                                   y,
                                   z,
                                   pixel_selection=defaults.HighestMethod())
    numpy.testing.assert_array_equal(to, t)
    numpy.testing.assert_array_equal(mo, m)

    # test with default and partially covered tile
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     xp,
                                     yp,
                                     zp,
                                     pixel_selection=defaults.HighestMethod())
    assert t.any()
    assert not m.all()

    # test when tiler raise errors (outside bounds)
    (t, m), _ = mosaic.mosaic_reader(assets, _read_tile, 150, 300, 9)
    assert not t
    assert not m

    # Test mean pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.MeanMethod())
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8369

    # Test mean pixel selection
    (t, m), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.MeanMethod(enforce_data_type=False),
    )
    assert m.all()
    assert t[0][-1][-1] == 8369.5

    # Test median pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.MedianMethod())
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8369

    # Test median pixel selection
    (t, m), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.MedianMethod(enforce_data_type=False),
    )
    assert m.all()
    assert t[0][-1][-1] == 8369.5

    # Test invalid Pixel Selection class
    with pytest.raises(Exception):

        class aClass(object):
            pass

        mosaic.mosaic_reader(assets,
                             _read_tile,
                             x,
                             y,
                             z,
                             pixel_selection=aClass())

    # test with preview
    # NOTE: We need to fix the output width and height because each preview could have different size
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_preview,
                                     width=256,
                                     height=256)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
Exemplo n.º 7
0
def test_mosaic_tiler():
    """Test mosaic tiler."""
    # test with default and full covered tile and default options
    (t, m), assets_used = mosaic.mosaic_reader(assets, _read_tile, x, y, z)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8682
    assert t.dtype == m.dtype

    # Test last pixel selection
    assetsr = list(reversed(assets))
    (t, m), _ = mosaic.mosaic_reader(assetsr, _read_tile, x, y, z)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8057
    assert t.dtype == m.dtype

    (t, m), _ = mosaic.mosaic_reader(assets, _read_tile, x, y, z, indexes=1)
    assert t.shape == (1, 256, 256)
    assert m.shape == (256, 256)
    assert t.all()
    assert m.all()
    assert t[0][-1][-1] == 8682
    assert t.dtype == m.dtype

    # Test darkest pixel selection
    (t, m), assets_used = mosaic.mosaic_reader(
        assets, _read_tile, x, y, z, pixel_selection=defaults.LowestMethod())
    assert len(assets_used) == 2
    assert m.all()
    assert t[0][-1][-1] == 8057
    assert t.dtype == m.dtype

    (to, mo), _ = mosaic.mosaic_reader(assets_order,
                                       _read_tile,
                                       x,
                                       y,
                                       z,
                                       pixel_selection=defaults.LowestMethod())
    numpy.testing.assert_array_equal(t[0, m], to[0, mo])
    assert to.dtype == mo.dtype

    # Test brightest pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.HighestMethod())
    assert m.all()
    assert t[0][-1][-1] == 8682
    assert t.dtype == m.dtype

    (to,
     mo), _ = mosaic.mosaic_reader(assets_order,
                                   _read_tile,
                                   x,
                                   y,
                                   z,
                                   pixel_selection=defaults.HighestMethod())
    numpy.testing.assert_array_equal(to, t)
    numpy.testing.assert_array_equal(mo, m)
    assert to.dtype == mo.dtype

    # test with default and partially covered tile
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     xp,
                                     yp,
                                     zp,
                                     pixel_selection=defaults.HighestMethod())
    assert t.any()
    assert not m.all()
    assert t.dtype == m.dtype

    # test when tiler raise errors (outside bounds)
    with pytest.raises(EmptyMosaicError):
        mosaic.mosaic_reader(assets, _read_tile, 150, 300, 9)

    # Test mean pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.MeanMethod())
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8369
    assert t.dtype == m.dtype

    # Test mean pixel selection
    (t, m), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.MeanMethod(enforce_data_type=False),
    )
    assert m.all()
    assert t[0][-1][-1] == 8369.5
    assert t.dtype == m.dtype

    # Test median pixel selection
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     pixel_selection=defaults.MedianMethod())
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8369
    assert t.dtype == m.dtype

    # Test median pixel selection
    (t, m), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.MedianMethod(enforce_data_type=False),
    )
    assert m.all()
    assert t[0][-1][-1] == 8369.5
    assert t.dtype == m.dtype

    (t, m), _ = mosaic.mosaic_reader(
        assets_order,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.LastBandHigh(),
        indexes=(1, 2, 3, 1),
    )
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8682
    assert t.dtype == m.dtype

    (t, m), _ = mosaic.mosaic_reader(
        assets_order,
        _read_tile,
        x,
        y,
        z,
        pixel_selection=defaults.LastBandLow(),
        indexes=(1, 2, 3, 1),
    )
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8057
    assert t.dtype == m.dtype

    # Test pixel selection as _class_, not instance of class
    (t, m), assets_used = mosaic.mosaic_reader(
        assets, _read_tile, x, y, z, pixel_selection=defaults.FirstMethod)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert m.all()
    assert t[0][-1][-1] == 8682
    assert t.dtype == m.dtype

    # Test invalid Pixel Selection class
    with pytest.raises(InvalidMosaicMethod):

        class aClass(object):
            pass

        mosaic.mosaic_reader(assets,
                             _read_tile,
                             x,
                             y,
                             z,
                             pixel_selection=aClass())

    # test with preview
    # NOTE: We need to fix the output width and height because each preview could have different size
    # Also because the 2 assets cover different bbox, getting the preview merged together doesn't make real sense
    (t, m), _ = mosaic.mosaic_reader(assets,
                                     _read_preview,
                                     width=256,
                                     height=256)
    assert t.shape == (3, 256, 256)
    assert m.shape == (256, 256)
    assert t.dtype == m.dtype
Exemplo n.º 8
0
def test_threads():
    """Test mosaic tiler."""
    assets = [asset2, asset1, asset1, asset2, asset1, asset2]

    # TileOutSide bounds should be ignored but no tile is created
    with pytest.raises(EmptyMosaicError):
        mosaic.mosaic_reader(assets, _read_tile, xo, yo, zo, threads=2)

    # TileOutSide bounds should be ignored but no tile is created
    with pytest.raises(EmptyMosaicError):
        mosaic.mosaic_reader(assets, _read_tile, xo, yo, zo, threads=0)

    # Only cover asset1
    xpp = 147
    ypp = 180
    zpp = 9

    with pytest.raises(TileOutsideBounds):
        mosaic.mosaic_reader(
            assets,
            _read_tile,
            xpp,
            ypp,
            zpp,
            pixel_selection=defaults.MedianMethod,
            allowed_exceptions=None,
        )

    # Partial tile, some assets should Error with TileOutside bounds
    (tnothread, _), a = mosaic.mosaic_reader(
        assets,
        _read_tile,
        xpp,
        ypp,
        zpp,
        threads=0,
        pixel_selection=defaults.MedianMethod,
    )
    assert len(a) == 3
    assert tnothread.shape

    # Partial tile, some assets should Error with TileOutside bounds
    (tnothread, _), a = mosaic.mosaic_reader(
        assets,
        _read_tile,
        xpp,
        ypp,
        zpp,
        threads=1,
        pixel_selection=defaults.MedianMethod,
    )
    assert len(a) == 3
    assert tnothread.shape

    (tnothread, _), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        xpp,
        ypp,
        zpp,
        threads=0,
        pixel_selection=defaults.MedianMethod,
    )
    (tmulti_threads, _), _ = mosaic.mosaic_reader(
        assets,
        _read_tile,
        xpp,
        ypp,
        zpp,
        threads=3,
        pixel_selection=defaults.MedianMethod,
    )
    numpy.testing.assert_array_equal(tnothread, tmulti_threads)

    (t, _), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     threads=0,
                                     chunk_size=2)
    assert t.shape == (3, 256, 256)
    (t, _), _ = mosaic.mosaic_reader(assets,
                                     _read_tile,
                                     x,
                                     y,
                                     z,
                                     threads=2,
                                     chunk_size=4)
    assert t.shape == (3, 256, 256)
Exemplo n.º 9
0
args = sys.argv
z = int(args[1])
x = int(args[2])
y = int(args[3])

def _reader(asset: str, x: int, y: int, z: int) -> ImageData:
    with COGReader(asset) as src_dst:
        return src_dst.tile(x, y, z, indexes=[1,2,3])

# warmup gdal
mosaic_assets_warmup = [
    "https://naipeuwest.blob.core.windows.net/naip/v002/pa/2013/pa_100cm_2013/39077/m_3907709_ne_18_1_20130906.tif",
    "https://naipeuwest.blob.core.windows.net/naip/v002/pa/2013/pa_100cm_2013/39077/m_3907709_nw_18_1_20130906.tif",
    "https://naipeuwest.blob.core.windows.net/naip/v002/pa/2013/pa_100cm_2013/39077/m_3907710_nw_18_1_20130906.tif"
]
data_warmup, _ = mosaic_reader(mosaic_assets_warmup, _reader, 1161, 1552, 12)
image_warmup = data_warmup.post_process(
    in_range=None,
    color_formula=None,
)
format = ImageType.png
image_warmup.render(
    add_mask=True,
    img_format=format,
    colormap=None,
    **format.profile,
)
# warmup end


start = time()