diff --git a/CHANGES.md b/CHANGES.md index 4542aaae..bf38d13f 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,4 +1,8 @@ +# 6.2.6 (2023-11-10) + +* validate `shape` in `ImageData.get_coverage_array` to avoid rasterio error when re-projecting the geometry + # 6.2.5 (2023-11-06) * avoid `indexes` collision in `MultiBaseReader` diff --git a/rio_tiler/models.py b/rio_tiler/models.py index d74068c1..dcf38363 100644 --- a/rio_tiler/models.py +++ b/rio_tiler/models.py @@ -35,6 +35,7 @@ RIOResampling, ) from rio_tiler.utils import ( + _validate_shape_input, get_array_statistics, linear_rescale, non_alpha_indexes, @@ -822,10 +823,11 @@ def get_coverage_array( Note: code adapted from https://github.com/perrygeo/python-rasterstats/pull/136 by @sgoodm """ + shape = _validate_shape_input(shape) + 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 2e200fa3..1a6afdfa 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -459,3 +459,24 @@ def test_imagedata_coverage(): coverage = im.get_coverage_array({"type": "Feature", "geometry": poly}) assert numpy.unique(coverage).tolist() == [0.25] + + poly = { + "type": "Polygon", + "coordinates": [ + [ + (-10018754.171394622, -5621521.486192066), + (10018754.171394622, -5621521.486192066), + (10018754.171394622, 5621521.486192066), + (-10018754.171394622, 5621521.486192066), + (-10018754.171394622, -5621521.486192066), + ] + ], + } + + coverage = im.get_coverage_array(poly, shape_crs="epsg:3857") + assert numpy.unique(coverage).tolist() == [0.25] + + coverage = im.get_coverage_array( + {"type": "Feature", "geometry": poly}, shape_crs="epsg:3857" + ) + assert numpy.unique(coverage).tolist() == [0.25]