Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow geojson feature in ImageData.get_coverage_array #642

Merged
merged 1 commit into from
Sep 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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`
Expand Down
32 changes: 20 additions & 12 deletions rio_tiler/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,26 +798,34 @@ 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

"""
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
3 changes: 3 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Loading