Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
vincentsarago committed Sep 24, 2024
1 parent b142f8d commit 0a84561
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 10 deletions.
18 changes: 8 additions & 10 deletions rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,7 @@ def get_vrt_transform(
tuple: VRT transform (affine.Affine), width (int) and height (int)
"""
# 1. Get the Dataset Resolution in the output crs
if src_dst.crs != dst_crs:
src_width = src_dst.width
src_height = src_dst.height
Expand Down Expand Up @@ -358,6 +359,7 @@ def get_vrt_transform(
else:
dst_transform = src_dst.transform

# 2. adjust output bounds if needed
# If bounds window is aligned with the dataset internal tile we align the bounds with the pixels.
# This is to limit the number of internal block fetched.
if _requested_tile_aligned_with_internal_tile(src_dst, bounds, bounds_crs=dst_crs):
Expand All @@ -384,6 +386,7 @@ def get_vrt_transform(

w, s, e, n = bounds

# 3. Calculate the VRT Height/Width
# When no output size (resolution) - Use Dataset Resolution
# NOTE: When we don't `fix` the output width/height, we're using the reprojected dataset resolution
# to calculate what is the size/transform of the VRT
Expand All @@ -399,16 +402,11 @@ def get_vrt_transform(
# NOTE: Here we check if the Output Resolution is higher thant the dataset resolution (OverZoom)
# When not over-zooming we don't want to use the output Width/Height to calculate the transform
# See issues https://github.com/cogeotiff/rio-tiler/pull/648
w_res = (
output_transform.a
if abs(output_transform.a) < abs(dst_transform.a)
else dst_transform.a
)
h_res = (
output_transform.e
if abs(output_transform.e) < abs(dst_transform.e)
else dst_transform.e
)
if abs(dst_transform.a) > abs(output_transform.a):
w_res = output_transform.a

if abs(dst_transform.e) > abs(output_transform.e):
h_res = output_transform.e

vrt_width = max(1, round((e - w) / w_res))
vrt_height = max(1, round((s - n) / h_res))
Expand Down
64 changes: 64 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,70 @@ def test_part_with_buffer():
numpy.array_equal(img_no_buffer.data, img.data[:, 2:-2, 2:-2])


def test_part_with_buffer_overzoom():
"""Make sure buffer works as expected."""
bounds = [
-6574807.42497772,
12210356.646387195,
-6261721.357121638,
12523442.714243278,
]
# Read part at full resolution
with rasterio.open(COG) as src_dst:
img_no_buffer = reader.part(
src_dst,
bounds,
dst_crs=constants.WEB_MERCATOR_CRS,
height=2000,
width=2000,
)

x_size = img_no_buffer.width
y_size = img_no_buffer.height

x_res = (bounds[2] - bounds[0]) / x_size
y_res = (bounds[3] - bounds[1]) / y_size

nx = x_size + 4
ny = y_size + 4

# apply a 2 pixel buffer
bounds_with_buffer = (
bounds[0] - x_res * 2,
bounds[1] - y_res * 2,
bounds[2] + x_res * 2,
bounds[3] + y_res * 2,
)
with rasterio.open(COG) as src_dst:
img = reader.part(
src_dst,
bounds_with_buffer,
height=ny,
width=nx,
dst_crs=constants.WEB_MERCATOR_CRS,
)
assert img.width == nx
assert img.height == ny

with rasterio.open(COG) as src_dst:
imgb = reader.part(
src_dst,
bounds,
buffer=2,
dst_crs=constants.WEB_MERCATOR_CRS,
height=2000,
width=2000,
)
assert imgb.width == nx
assert imgb.height == ny

assert numpy.array_equal(img.data, imgb.data)
assert img.bounds == imgb.bounds

numpy.array_equal(img_no_buffer.data, imgb.data[:, 2:-2, 2:-2])
numpy.array_equal(img_no_buffer.data, img.data[:, 2:-2, 2:-2])


def test_read():
"""Test reader.read function."""
with rasterio.open(COG) as src:
Expand Down

0 comments on commit 0a84561

Please sign in to comment.