Skip to content

Commit

Permalink
[715] Enable handling of inverted latitude in reader.point (#716)
Browse files Browse the repository at this point in the history
* Update point to handle inverted latitude

* add formatting

* add warning and test

* fix test

* update changelog

---------

Co-authored-by: vincentsarago <[email protected]>
  • Loading branch information
georgespill and vincentsarago authored Jul 31, 2024
1 parent fbdaff8 commit 8573dbe
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# unreleased

* better error message for `TileOutsideBounds` errors (author @abarciauskas-bgse, https://github.com/cogeotiff/rio-tiler/pull/712)
* handle of inverted latitude in `reader.point` (author @georgespill, https://github.com/cogeotiff/rio-tiler/pull/716)

# 6.6.1 (2024-05-17)

Expand Down
22 changes: 19 additions & 3 deletions rio_tiler/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,9 @@ def read(
values = dataset.read(
indexes=indexes,
window=window,
out_shape=(len(indexes), height, width) if height and width else None,
out_shape=(
(len(indexes), height, width) if height and width else None
),
resampling=io_resampling,
boundless=boundless,
)
Expand Down Expand Up @@ -559,9 +561,23 @@ def point(
xs, ys = transform_coords(coord_crs, dataset.crs, [lon], [lat])
lon, lat = xs[0], ys[0]

dataset_min_lon, dataset_min_lat, dataset_max_lon, dataset_max_lat = (
dataset.bounds
)
# check if latitude is inverted
if dataset_min_lat > dataset_max_lat:
warnings.warn(
"BoundingBox of the dataset is inverted (minLat > maxLat).",
UserWarning,
)

dataset_min_lat, dataset_max_lat = (
min(dataset_min_lat, dataset_max_lat),
max(dataset_min_lat, dataset_max_lat),
)
if not (
(dataset.bounds[0] < lon < dataset.bounds[2])
and (dataset.bounds[1] < lat < dataset.bounds[3])
(dataset_min_lon < lon < dataset_max_lon)
and (dataset_min_lat < lat < dataset_max_lat)
):
raise PointOutsideBounds("Point is outside dataset bounds")

Expand Down
8 changes: 8 additions & 0 deletions tests/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
COG_NODATA_FLOAT_NAN = os.path.join(
os.path.dirname(__file__), "fixtures", "cog_nodata_float_nan.tif"
)
COG_INVERTED = os.path.join(os.path.dirname(__file__), "fixtures", "inverted_lat.tif")


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -850,3 +851,10 @@ def test_tile_read_nodata_float():
prev = reader.read(src_dst, max_size=100)
assert prev.mask[0, 0] == 0
assert not numpy.all(prev.mask)


def test_inverted_latitude_point():
"""Make sure we can read a point from a file with inverted latitude."""
with rasterio.open(COG_INVERTED) as src_dst:
pt = reader.point(src_dst, [-104.77519499, 38.95367054])
assert pt.data[0] == -9999.0

0 comments on commit 8573dbe

Please sign in to comment.