Skip to content

Commit

Permalink
RasterDataset: calculate res after changing crs (#2193)
Browse files Browse the repository at this point in the history
* RasterDataset: recalculate res after crs changes

* Add unit tests

* Fix style

* One less line of code
  • Loading branch information
adamjstewart authored Aug 1, 2024
1 parent ecb07ee commit ac46bbb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 7 additions & 0 deletions tests/datasets/test_geo.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.

import math
import os
import pickle
import sys
Expand Down Expand Up @@ -277,6 +279,11 @@ def test_getitem_separate_files(self, sentinel: Sentinel2) -> None:
assert isinstance(x['image'], torch.Tensor)
assert len(sentinel.bands) == x['image'].shape[0]

def test_reprojection(self, naip: NAIP) -> None:
naip2 = NAIP(naip.paths, crs='EPSG:4326')
assert naip.crs != naip2.crs
assert not math.isclose(naip.res, naip2.res)

@pytest.mark.parametrize('dtype', ['uint16', 'uint32'])
def test_getitem_uint_dtype(self, dtype: str) -> None:
root = os.path.join('tests', 'data', 'raster', dtype)
Expand Down
4 changes: 2 additions & 2 deletions torchgeo/datasets/geo.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,11 +461,11 @@ def __init__(

if crs is None:
crs = src.crs
if res is None:
res = src.res[0]

with WarpedVRT(src, crs=crs) as vrt:
minx, miny, maxx, maxy = vrt.bounds
if res is None:
res = vrt.res[0]
except rasterio.errors.RasterioIOError:
# Skip files that rasterio is unable to read
continue
Expand Down

0 comments on commit ac46bbb

Please sign in to comment.