Skip to content

Commit ad5da22

Browse files
committed
ENH:reproject: Support geolocation arrays
1 parent 3ca29fb commit ad5da22

File tree

3 files changed

+43
-5
lines changed

3 files changed

+43
-5
lines changed

rioxarray/raster_array.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- https://github.com/opendatacube/datacube-core/blob/1d345f08a10a13c316f81100936b0ad8b1a374eb/LICENSE # noqa: E501
1010
1111
"""
12+
1213
import copy
1314
import os
1415
from collections.abc import Hashable, Iterable, Mapping
@@ -128,7 +129,13 @@ def _make_dst_affine(
128129
**kwargs,
129130
):
130131
"""Determine the affine of the new projected `xarray.DataArray`"""
131-
src_bounds = () if "gcps" in kwargs else src_data_array.rio.bounds()
132+
src_bounds = ()
133+
if (
134+
"gcps" not in kwargs
135+
and "rpcs" not in kwargs
136+
and "src_geoloc_array" not in kwargs
137+
):
138+
src_bounds = src_data_array.rio.bounds()
132139
src_height, src_width = src_data_array.rio.shape
133140
dst_height, dst_width = dst_shape if dst_shape is not None else (None, None)
134141
# pylint: disable=isinstance-second-argument-not-valid-type
@@ -453,8 +460,12 @@ def reproject(
453460
if gcps:
454461
kwargs.setdefault("gcps", gcps)
455462

456-
gcps_or_rpcs = "gcps" in kwargs or "rpcs" in kwargs
457-
src_affine = None if gcps_or_rpcs else self.transform(recalc=True)
463+
use_affine = (
464+
"gcps" not in kwargs
465+
and "rpcs" not in kwargs
466+
and "src_geoloc_array" not in kwargs
467+
)
468+
src_affine = None if not use_affine else self.transform(recalc=True)
458469
if transform is None:
459470
dst_affine, dst_width, dst_height = _make_dst_affine(
460471
src_data_array=self._obj,
@@ -474,7 +485,6 @@ def reproject(
474485
dst_data = self._create_dst_data(dst_height=dst_height, dst_width=dst_width)
475486

476487
dst_nodata = self._get_dst_nodata(nodata)
477-
478488
rasterio.warp.reproject(
479489
source=self._obj.values,
480490
destination=dst_data,
@@ -506,7 +516,7 @@ def reproject(
506516
dst_affine=dst_affine,
507517
dst_width=dst_width,
508518
dst_height=dst_height,
509-
force_generate=gcps_or_rpcs,
519+
force_generate=not use_affine,
510520
),
511521
dims=tuple(dst_dims),
512522
attrs=new_attrs,

test/conftest.py

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
GDAL_GE_36 = version.parse(rasterio.__gdal_version__) >= version.parse("3.6.0")
2020
GDAL_GE_361 = version.parse(rasterio.__gdal_version__) >= version.parse("3.6.1")
2121
GDAL_GE_364 = version.parse(rasterio.__gdal_version__) >= version.parse("3.6.4")
22+
RASTERIO_GTE_1_4 = version.parse(rasterio.__version__) >= version.parse("1.4a1")
2223

2324

2425
# xarray.testing.assert_equal(input_xarray, compare_xarray)

test/integration/test_integration_rioxarray.py

+27
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from rioxarray.rioxarray import _generate_spatial_coords, _make_coords
3232
from test.conftest import (
3333
GDAL_GE_361,
34+
RASTERIO_GTE_1_4,
3435
TEST_COMPARE_DATA_DIR,
3536
TEST_INPUT_DATA_DIR,
3637
_assert_xarrays_equal,
@@ -3165,3 +3166,29 @@ def test_bounds__ordered__dataarray():
31653166
def test_bounds__ordered__dataset():
31663167
xds = xarray.Dataset(None, coords={"x": range(5), "y": range(5)})
31673168
assert xds.rio.bounds() == (-0.5, -0.5, 4.5, 4.5)
3169+
3170+
3171+
@pytest.mark.skipif(not RASTERIO_GTE_1_4, reason="Requires rasterio 1.4+")
3172+
def test_non_rectilinear__reproject(open_rasterio):
3173+
test_file = os.path.join(TEST_INPUT_DATA_DIR, "2d_test.tif")
3174+
with open_rasterio(test_file) as xds:
3175+
xds_1d = xds.rio.reproject(
3176+
"EPSG:4326",
3177+
src_geoloc_array=(
3178+
xds.coords["xc"].values,
3179+
xds.coords["yc"].values,
3180+
),
3181+
georeferencing_convention="PIXEL_CENTER",
3182+
)
3183+
assert xds_1d.coords["x"].shape == (14,)
3184+
assert xds_1d.coords["y"].shape == (10,)
3185+
xds_1d.rio.transform().almost_equals(
3186+
Affine(
3187+
216.8587081056465,
3188+
0.0,
3189+
115698.25,
3190+
0.0,
3191+
-216.8587081056465,
3192+
2818720.0,
3193+
)
3194+
)

0 commit comments

Comments
 (0)