From da602aa34bbdfc04b8fff523fc88800c0cce4132 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Thu, 25 Jul 2024 17:33:40 +0200 Subject: [PATCH 1/4] RasterDataset: recalculate res after crs changes --- torchgeo/datasets/geo.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/torchgeo/datasets/geo.py b/torchgeo/datasets/geo.py index 57718fcb4c8..22140399537 100644 --- a/torchgeo/datasets/geo.py +++ b/torchgeo/datasets/geo.py @@ -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 From 47a3b74fb497185436b2b0805a8c254da5f0d315 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Fri, 26 Jul 2024 11:38:09 +0200 Subject: [PATCH 2/4] Add unit tests --- tests/datasets/test_geo.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/datasets/test_geo.py b/tests/datasets/test_geo.py index 25aee445359..a510957ecfb 100644 --- a/tests/datasets/test_geo.py +++ b/tests/datasets/test_geo.py @@ -1,5 +1,7 @@ # Copyright (c) Microsoft Corporation. All rights reserved. # Licensed under the MIT License. + +import math import os import pickle import sys @@ -277,6 +279,12 @@ 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: + ds1 = naip + ds2 = NAIP(naip.paths, crs="EPSG:4326") + assert ds1.crs != ds2.crs + assert not math.isclose(ds1.res, ds2.res) + @pytest.mark.parametrize('dtype', ['uint16', 'uint32']) def test_getitem_uint_dtype(self, dtype: str) -> None: root = os.path.join('tests', 'data', 'raster', dtype) From dc7f9664c88a0e20e22acc4bac7731ad3910afa0 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Fri, 26 Jul 2024 11:40:50 +0200 Subject: [PATCH 3/4] Fix style --- tests/datasets/test_geo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/datasets/test_geo.py b/tests/datasets/test_geo.py index a510957ecfb..267909f14f4 100644 --- a/tests/datasets/test_geo.py +++ b/tests/datasets/test_geo.py @@ -281,7 +281,7 @@ def test_getitem_separate_files(self, sentinel: Sentinel2) -> None: def test_reprojection(self, naip: NAIP) -> None: ds1 = naip - ds2 = NAIP(naip.paths, crs="EPSG:4326") + ds2 = NAIP(naip.paths, crs='EPSG:4326') assert ds1.crs != ds2.crs assert not math.isclose(ds1.res, ds2.res) From f1e652d0be51d1089ba3daf7a53fae6178a683f1 Mon Sep 17 00:00:00 2001 From: "Adam J. Stewart" Date: Fri, 26 Jul 2024 12:51:44 +0200 Subject: [PATCH 4/4] One less line of code --- tests/datasets/test_geo.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/tests/datasets/test_geo.py b/tests/datasets/test_geo.py index 267909f14f4..398821bfbb1 100644 --- a/tests/datasets/test_geo.py +++ b/tests/datasets/test_geo.py @@ -280,10 +280,9 @@ def test_getitem_separate_files(self, sentinel: Sentinel2) -> None: assert len(sentinel.bands) == x['image'].shape[0] def test_reprojection(self, naip: NAIP) -> None: - ds1 = naip - ds2 = NAIP(naip.paths, crs='EPSG:4326') - assert ds1.crs != ds2.crs - assert not math.isclose(ds1.res, ds2.res) + 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: