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

make sure rio-tiler works with files with Inverted Lat (transform) #696

Merged
merged 4 commits into from
Apr 18, 2024
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
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@

# 6.4.7 (2024-04-17)

* Better handle dataset with inverted origin
* make sure datatype is forwarded to the WarpedVRT

# 6.4.6 (2024-04-09)

* Ignore STAC statistics object when they contain invalid type (author @emmanuelmathot, https://github.com/cogeotiff/rio-tiler/pull/695)
Expand Down
12 changes: 12 additions & 0 deletions rio_tiler/io/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ class SpatialMixin:
def geographic_bounds(self) -> BBox:
"""Return dataset bounds in geographic_crs."""
if self.crs == self.geographic_crs:
if self.bounds[1] > self.bounds[3]:
warnings.warn(
"BoundingBox of the dataset is inverted (minLat > maxLat).",
UserWarning,
)
return (
self.bounds[0],
self.bounds[3],
self.bounds[2],
self.bounds[1],
)

return self.bounds

try:
Expand Down
2 changes: 1 addition & 1 deletion rio_tiler/io/xarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ def point(

y, x = rowcol(ds.rio.transform(), ds_lon, ds_lat)

arr = ds[:, y[0], x[0]].to_masked_array()
arr = ds[:, int(y[0]), int(x[0])].to_masked_array()
arr.mask |= arr.data == ds.rio.nodata

return PointData(
Expand Down
9 changes: 8 additions & 1 deletion rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,11 +142,17 @@ def read(
"crs": dst_crs,
"add_alpha": True,
"resampling": warp_resampling,
"dtype": src_dst.dtypes[0],
}

if nodata is not None:
vrt_params.update(
{"nodata": nodata, "add_alpha": False, "src_nodata": nodata}
{
"nodata": nodata,
"add_alpha": False,
"src_nodata": nodata,
"dtype": src_dst.dtypes[0],
}
)

if has_alpha_band(src_dst):
Expand Down Expand Up @@ -396,6 +402,7 @@ def part(
"transform": vrt_transform,
"width": vrt_width,
"height": vrt_height,
"dtype": src_dst.dtypes[0],
}
if vrt_options:
vrt_params.update(**vrt_options)
Expand Down
17 changes: 17 additions & 0 deletions rio_tiler/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,23 @@ def get_vrt_transform(
src_height = round(w.height)
src_width = round(w.width)

# Specific FIX when bounds and transform are inverted
# See: https://github.com/US-GHG-Center/veda-config-ghg/pull/333
elif (
src_dst.crs == WGS84_CRS
and dst_crs == WEB_MERCATOR_CRS
and (src_bounds[1] > 85.06 or src_bounds[3] < -85.06)
):
warnings.warn(
"Adjusting dataset latitudes to avoid re-projection overflow",
UserWarning,
)
src_bounds[1] = min(src_bounds[1], 85.06)
src_bounds[3] = max(src_bounds[3], -85.06)
w = windows.from_bounds(*src_bounds, transform=src_dst.transform)
src_height = round(w.height)
src_width = round(w.width)

dst_transform, _, _ = calculate_default_transform(
src_dst.crs, dst_crs, src_width, src_height, *src_bounds
)
Expand Down
6 changes: 6 additions & 0 deletions tests/benchmarks/requests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
"""Test HTTP Requests."""

import pytest
from rasterio.env import GDALVersion
from tilebench import profile

from rio_tiler.io import Reader

dataset_url = "https://sentinel-cogs.s3.us-west-2.amazonaws.com/sentinel-s2-l2a-cogs/15/T/VK/2023/10/S2B_15TVK_20231008_0_L2A/TCI.tif"

gdal_version = GDALVersion.runtime()


def test_info():
"""Info should only GET the header."""
Expand All @@ -32,6 +36,7 @@ def info(src_path: str):
assert not stats["WarpKernels"]


@pytest.mark.xfail
def test_tile_read():
"""Tile Read tests."""

Expand All @@ -41,6 +46,7 @@ def test_tile_read():
"GDAL_HTTP_MERGE_CONSECUTIVE_RANGES": "YES",
"GDAL_DISABLE_READDIR_ON_OPEN": "EMPTY_DIR",
"GDAL_INGESTED_BYTES_AT_OPEN": 32768,
"CPL_VSIL_CURL_ALLOWED_EXTENSIONS": ".tif",
"CPL_VSIL_CURL_NON_CACHED": f"/vsicurl/{dataset_url}",
},
quiet=True,
Expand Down
6 changes: 5 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ def _dataset(
arr[:, 0:128, 0:128] = 0

# Mask/Alpha
mask = numpy.zeros((1, height, width), dtype=dtype) + 255
if dtype == "int8":
mask = numpy.zeros((1, height, width), dtype=dtype) + 127
else:
mask = numpy.zeros((1, height, width), dtype=dtype) + 255

mask[:, 0:128, 0:128] = 0

# Input Profile
Expand Down
Binary file added tests/fixtures/inverted_lat.tif
Binary file not shown.
12 changes: 12 additions & 0 deletions tests/test_io_rasterio.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
GEOTIFF = os.path.join(PREFIX, "nocog.tif")
COG_EUROPA = os.path.join(PREFIX, "cog_nonearth.tif")
COG_MARS = os.path.join(PREFIX, "cog_hirise_mars.tif")
COG_INVERTED = os.path.join(PREFIX, "inverted_lat.tif")

KEY_ALPHA = "hro_sources/colorado/201404_13SED190110_201404_0x1500m_CL_1_alpha.tif"
COG_ALPHA = os.path.join(PREFIX, "my-bucket", KEY_ALPHA)
Expand Down Expand Up @@ -1111,3 +1112,14 @@ def test_feature_statistics():
assert not numpy.unique(coverage_array).tolist() == [1.0]

assert stats["b1"].mean != stats_align["b1"].mean


def test_inverted_latitude():
"""Test working with inverted Latitude."""
with pytest.warns(UserWarning):
with Reader(COG_INVERTED) as src:
assert src.geographic_bounds[1] < src.geographic_bounds[3]

with pytest.warns(UserWarning):
with Reader(COG_INVERTED) as src:
_ = src.tile(0, 0, 0)
Loading