diff --git a/CHANGES.md b/CHANGES.md index da7564f3..ab1f6263 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,4 +1,8 @@ +# 6.2.1 (2023-09-28) + +* allow GeoJSON `Feature` in `ImageData.get_coverage_array` method + # 6.2.0 (2023-09-27) * allow area-weighted statistics by adding `coverage` option in `rio_tiler.utils.get_array_statistics` diff --git a/rio_tiler/models.py b/rio_tiler/models.py index f151b0fa..d74068c1 100644 --- a/rio_tiler/models.py +++ b/rio_tiler/models.py @@ -798,19 +798,26 @@ def get_coverage_array( shape_crs: CRS = WGS84_CRS, cover_scale: int = 10, ) -> NDArray[numpy.floating]: + """Post-process image data. - """ - cover_scale: int, optional - Scale used when generating coverage estimates of each - raster cell by vector feature. Coverage is generated by - rasterizing the feature at a finer resolution than the raster then using a summation to aggregate - to the raster resolution and dividing by the square of cover_scale - to get coverage value for each cell. Increasing cover_scale - will increase the accuracy of coverage values; three orders - magnitude finer resolution (cover_scale=1000) is usually enough to - get coverage estimates with <1% error in individual edge cells coverage - estimates, though much smaller values (e.g., cover_scale=10) are often - sufficient (<10% error) and require less memory. + Args: + in_range (tuple): input min/max bounds value to rescale from. + out_dtype (str, optional): output datatype after rescaling. Defaults to `uint8`. + color_formula (str, optional): color-ops formula (see: https://github.com/vincentsarago/color-ops). + cover_scale (int, optional): + Scale used when generating coverage estimates of each + raster cell by vector feature. Coverage is generated by + rasterizing the feature at a finer resolution than the raster then using a summation to aggregate + to the raster resolution and dividing by the square of cover_scale + to get coverage value for each cell. Increasing cover_scale + will increase the accuracy of coverage values; three orders + magnitude finer resolution (cover_scale=1000) is usually enough to + get coverage estimates with <1% error in individual edge cells coverage + estimates, though much smaller values (e.g., cover_scale=10) are often + sufficient (<10% error) and require less memory. + + Returns: + numpy.array: percent coverage. Note: code adapted from https://github.com/perrygeo/python-rasterstats/pull/136 by @sgoodm @@ -818,6 +825,7 @@ def get_coverage_array( if self.crs != shape_crs: shape = transform_geom(shape_crs, self.crs, shape) + shape = shape.get("geometry", shape) cover_array = rasterize( [(shape, 1)], out_shape=(self.height * cover_scale, self.width * cover_scale), diff --git a/tests/test_models.py b/tests/test_models.py index aa02d761..2e200fa3 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -456,3 +456,6 @@ def test_imagedata_coverage(): } coverage = im.get_coverage_array(poly) assert numpy.unique(coverage).tolist() == [0.25] + + coverage = im.get_coverage_array({"type": "Feature", "geometry": poly}) + assert numpy.unique(coverage).tolist() == [0.25]