Example #1
0
def test_raster_get_stats_validOptions():
    """Should return a valid dict with array statistics."""
    stats = utils.raster_get_stats(
        S3_PATH, indexes=3, overview_level=1, percentiles=(10, 90), dst_crs="epsg:3857"
    )
    assert stats["bounds"]["crs"] == "epsg:3857"
    assert len(stats["statistics"]) == 1
    assert stats["statistics"][3]["pc"] == [77, 178]

    stats = utils.raster_get_stats(S3_PATH, indexes=(3,))
    assert len(stats["statistics"]) == 1
    assert stats["statistics"][3]["pc"] == [54, 192]
Example #2
0
def metadata(address, pmin=2, pmax=98, **kwargs):
    """
    Return image bounds and band statistics.

    Attributes
    ----------
    address : str or PathLike object
        A dataset path or URL. Will be opened in "r" mode.
    pmin : int, optional, (default: 2)
        Histogram minimum cut.
    pmax : int, optional, (default: 98)
        Histogram maximum cut.
    kwargs : optional
        These are passed to 'rio_tiler.utils.raster_get_stats'
        e.g: overview_level=2, dst_crs='epsg:4326'

    Returns
    -------
    out : dict
        Dictionary with image bounds and bands statistics.

    """
    info = {"address": address}
    info.update(
        utils.raster_get_stats(address, percentiles=(pmin, pmax), **kwargs))
    return info
Example #3
0
def test_raster_get_stats_validNodata():
    """Should return a valid dict with array statistics."""
    with pytest.warns(NoOverviewWarning):
        stats = utils.raster_get_stats(S3_NODATA_PATH)
    assert stats["bounds"]
    assert len(stats["statistics"]) == 3
    assert stats["statistics"][1]["pc"] == [12, 198]
    assert stats["statistics"][2]["pc"] == [28, 201]
    assert stats["statistics"][3]["pc"] == [56, 192]

    with pytest.warns(NoOverviewWarning):
        stats = utils.raster_get_stats(S3_NODATA_PATH, nodata=0)
    assert stats["bounds"]
    assert len(stats["statistics"]) == 3
    assert stats["statistics"][1]["pc"] == [12, 198]
    assert stats["statistics"][2]["pc"] == [28, 201]
    assert stats["statistics"][3]["pc"] == [56, 192]
Example #4
0
def test_raster_get_stats_validAlpha():
    """Should return a valid dict with array statistics."""
    with pytest.warns(NoOverviewWarning):
        stats = utils.raster_get_stats(S3_ALPHA_PATH)
    assert len(stats["statistics"]) == 3
    assert stats["statistics"][1]["pc"] == [12, 199]
    assert stats["statistics"][2]["pc"] == [29, 201]
    assert stats["statistics"][3]["pc"] == [56, 193]
Example #5
0
def test_raster_get_stats_valid():
    """Should return a valid dict with array statistics."""
    stats = utils.raster_get_stats(S3_PATH)
    assert stats["bounds"]
    assert stats["bounds"]["crs"] == CRS({"init": "EPSG:4326"})
    assert len(stats["statistics"]) == 3
    assert stats["statistics"][1]["pc"] == [12, 198]
    assert stats["statistics"][2]["pc"] == [27, 201]
    assert stats["statistics"][3]["pc"] == [54, 192]
Example #6
0
def test_raster_get_stats_expr():
    """Validate that expressions work for raster statistics."""
    resampling_method = "bilinear"
    rio_stats = utils.raster_get_stats(
        S3_PATH, overview_level=1, resampling_method=resampling_method, expr="b1 + b2"
    )
    
    assert rio_stats["statistics"][1]["pc"] == [38, 400]
    assert rio_stats["statistics"][1]["max"] == 507
    assert rio_stats["statistics"][1]["min"] == 0

    rio_stats = utils.raster_get_stats(
        S3_PATH, overview_level=1, resampling_method=resampling_method, expr="b1 - b2"
    )
    
    assert rio_stats["statistics"][1]["pc"] == [-18, 39]
    assert rio_stats["statistics"][1]["max"] == 88
    assert rio_stats["statistics"][1]["min"] == -80
Example #7
0
 def _s1_metadata(src_path, percentiles, **kwarg):
     with rasterio.open(src_path) as src_dst:
         with WarpedVRT(
                 src_dst,
                 src_crs=src_dst.gcps[1],
                 src_transform=transform.from_gcps(src_dst.gcps[0]),
                 src_nodata=0,
         ) as vrt_dst:
             return utils.raster_get_stats(vrt_dst,
                                           percentiles=percentiles,
                                           **kwarg)
Example #8
0
def test_raster_get_stats_ovr():
    """Validate that overview level return the same result than reeading the overview."""
    resampling_method = "bilinear"
    rio_stats = utils.raster_get_stats(
        S3_PATH, overview_level=1, resampling_method=resampling_method
    )

    with rasterio.open(S3_PATH, overview_level=1) as src_dst:
        indexes = src_dst.indexes
        arr = src_dst.read(resampling=Resampling[resampling_method], masked=True)
        stats = {indexes[b]: utils._stats(arr[b], bins=10) for b in range(arr.shape[0])}
    assert rio_stats["statistics"] == stats
Example #9
0
def test_raster_get_stats_valid():
    """Should return a valid dict with array statistics."""
    stats = utils.raster_get_stats(S3_PATH)
    assert stats["bounds"]
    assert stats["bounds"]["crs"] == CRS({"init": "EPSG:4326"})
    assert len(stats["statistics"]) == 3
    assert stats["statistics"][1]["pc"] == [11, 199]
    assert stats["statistics"][2]["pc"] == [26, 201]
    assert stats["statistics"][3]["pc"] == [54, 192]
    assert stats["minzoom"]
    assert stats["maxzoom"]
    assert len(stats["band_descriptions"]) == 3
    assert (1, "band1") == stats["band_descriptions"][0]

    with rasterio.open(S3_PATH) as src_dst:
        stats = utils.raster_get_stats(src_dst)
        assert stats["bounds"]
        assert stats["bounds"]["crs"] == CRS({"init": "EPSG:4326"})
        assert len(stats["statistics"]) == 3
        assert stats["statistics"][1]["pc"] == [11, 199]
        assert stats["statistics"][2]["pc"] == [26, 201]
        assert stats["statistics"][3]["pc"] == [54, 192]
        assert stats["minzoom"]
        assert stats["maxzoom"]
        assert len(stats["band_descriptions"]) == 3
        assert (1, "band1") == stats["band_descriptions"][0]

    stats = utils.raster_get_stats(COG_DST)
    assert stats["minzoom"]
    assert stats["maxzoom"]
    assert len(stats["band_descriptions"]) == 1
    assert (1, "b1") == stats["band_descriptions"][0]

    stats = utils.raster_get_stats(S3_PATH, histogram_bins=20)
    assert len(stats["statistics"][1]["histogram"][0]) == 20

    stats = utils.raster_get_stats(S3_PATH,
                                   histogram_bins=None,
                                   histogram_range=[30, 70])
    assert len(stats["statistics"][1]["histogram"][0]) == 10
Example #10
0
    def metadata(
        self,
        pmin: float = 2.0,
        pmax: float = 98.0,
        histogram_bins: Union[int, List, Tuple[int, ...]] = 20,
        **kwargs: Any,
    ) -> Dict:
        """
        Compute array statistics.

        see https://docs.scipy.org/doc/numpy/reference/generated/numpy.histogram.html
        for detailed options

        Attributes
        ----------
            pmin : float, optional
                Min percentile. Default is 2.0.
            pmax : float, optional
                Max percentile. Default is 98.0.
            histogram_bins : int or sequence of scalars or str, optional
                Histogram bins. Default is 10.
            kwargs : Any, optional
                Additional options to forward to rio_tiler.utils.raster_get_stats

        Returns
        -------
            metadata : dict

        """
        meta = dict(url=self.src_path, dtype=self.src_dst.dtypes[0])
        if self._colormap:
            meta.update(dict(colormap=self._colormap))
            histogram_bins = [
                k for k, v in self._colormap.items() if v != (0, 0, 0, 255)
            ]

        meta.update(
            raster_get_stats(
                self.src_dst,
                percentiles=(pmin, pmax),
                histogram_bins=histogram_bins,
                **kwargs,
            ))

        return meta