Skip to content

Commit

Permalink
remove duplicated code and update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Jan 16, 2024
1 parent 5cc79a7 commit 3fe0298
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

# 6.3.0 (unreleased)
# 6.3.0 (2024-01-16)

* do not use `warpedVRT` when overwriting the dataset nodata value

Expand Down
3 changes: 1 addition & 2 deletions docs/src/advanced/statistics.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,10 @@ with Reader("cog.tif") as src:
stats = data.statistics(coverage=coverage_array)
```

When passing `align_bounds_with_dataset=True` to the `reader.part()` method (forwared from `.feature` or `.part` reader's method), rio-tiler will adjust the input geometry bounds to math the input dataset resolution/transform and avoid unnecessary resampling.
When passing `align_bounds_with_dataset=True` to the `reader.part()` method (forwarded from `.feature` or `.part` reader methods), rio-tiler will adjust the input geometry bounds to match the input dataset resolution/transform and avoid unnecessary resampling.

<img src="https://github.com/cogeotiff/rio-tiler/assets/10407788/0e340d3d-e5d9-4558-93f7-3f307c017510" style="max-width: 800px;">


### Zonal Statistics method

You can easily extend the rio-tiler's reader to add a `.zonal_statistics()` method as:
Expand Down
17 changes: 7 additions & 10 deletions rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,12 @@
from rio_tiler.models import ImageData, PointData
from rio_tiler.types import BBox, Indexes, NoData, RIOResampling, WarpResampling
from rio_tiler.utils import _requested_tile_aligned_with_internal_tile as is_aligned
from rio_tiler.utils import get_vrt_transform, has_alpha_band, non_alpha_indexes
from rio_tiler.utils import (
_round_window,
get_vrt_transform,
has_alpha_band,
non_alpha_indexes,
)


class Options(TypedDict, total=False):
Expand Down Expand Up @@ -423,15 +428,7 @@ def part(
# else no re-projection needed
window = windows.from_bounds(*bounds, transform=src_dst.transform)
if align_bounds_with_dataset:
(row_start, row_stop), (col_start, col_stop) = window.toranges()
row_start, row_stop = int(math.floor(row_start)), int(math.ceil(row_stop))
col_start, col_stop = int(math.floor(col_start)), int(math.ceil(col_stop))
window = windows.Window(
col_off=col_start,
row_off=row_start,
width=max(col_stop - col_start, 0.0),
height=max(row_stop - row_start, 0.0),
)
window = _round_window(window)
bounds = windows.bounds(window, src_dst.transform)

if max_size and not (width and height):
Expand Down
23 changes: 13 additions & 10 deletions rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ def _div_round_up(a: int, b: int) -> int:
return (a // b) if (a % b) == 0 else (a // b) + 1


def _round_window(window: windows.Window) -> windows.Window:
(row_start, row_stop), (col_start, col_stop) = window.toranges()
row_start, row_stop = int(math.floor(row_start)), int(math.ceil(row_stop))
col_start, col_stop = int(math.floor(col_start)), int(math.ceil(col_stop))
return windows.Window(
col_off=col_start,
row_off=row_start,
width=max(col_stop - col_start, 0.0),
height=max(row_stop - row_start, 0.0),
)


def get_overview_level(
src_dst: Union[DatasetReader, DatasetWriter, WarpedVRT],
bounds: BBox,
Expand Down Expand Up @@ -303,16 +315,7 @@ def get_vrt_transform(
bounds = src_dst.window_bounds(w)

elif align_bounds_with_dataset:
window = windows.from_bounds(*bounds, transform=dst_transform)
(row_start, row_stop), (col_start, col_stop) = window.toranges()
row_start, row_stop = int(math.floor(row_start)), int(math.ceil(row_stop))
col_start, col_stop = int(math.floor(col_start)), int(math.ceil(col_stop))
window = windows.Window(
col_off=col_start,
row_off=row_start,
width=max(col_stop - col_start, 0.0),
height=max(row_stop - row_start, 0.0),
)
window = _round_window(windows.from_bounds(*bounds, transform=dst_transform))
bounds = windows.bounds(window, dst_transform)

w, s, e, n = bounds
Expand Down

0 comments on commit 3fe0298

Please sign in to comment.