Skip to content

Commit

Permalink
validate shape in coverage (#653)
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago authored Nov 10, 2023
1 parent f223e22 commit 9f63716
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
4 changes: 3 additions & 1 deletion rio_tiler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
RIOResampling,
)
from rio_tiler.utils import (
_validate_shape_input,
get_array_statistics,
linear_rescale,
non_alpha_indexes,
Expand Down Expand Up @@ -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),
Expand Down
21 changes: 21 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 9f63716

Please sign in to comment.