Skip to content

Commit 2f0fad3

Browse files
committed
ENH:reproject: Support geolocation arrays
1 parent 94a80fd commit 2f0fad3

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-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

+26
Original file line numberDiff line numberDiff line change
@@ -3184,3 +3184,29 @@ def test_bounds__ordered__dataarray():
31843184
def test_bounds__ordered__dataset():
31853185
xds = xarray.Dataset(None, coords={"x": range(5), "y": range(5)})
31863186
assert xds.rio.bounds() == (-0.5, -0.5, 4.5, 4.5)
3187+
3188+
3189+
@pytest.mark.skipif(not RASTERIO_GE_14, reason="Requires rasterio 1.4+")
3190+
def test_non_rectilinear__reproject(open_rasterio):
3191+
test_file = os.path.join(TEST_INPUT_DATA_DIR, "2d_test.tif")
3192+
with open_rasterio(test_file) as xds:
3193+
xds_1d = xds.rio.reproject(
3194+
"EPSG:4326",
3195+
src_geoloc_array=(
3196+
xds.coords["xc"].values,
3197+
xds.coords["yc"].values,
3198+
),
3199+
georeferencing_convention="PIXEL_CENTER",
3200+
)
3201+
assert xds_1d.coords["x"].shape == (14,)
3202+
assert xds_1d.coords["y"].shape == (10,)
3203+
xds_1d.rio.transform().almost_equals(
3204+
Affine(
3205+
216.8587081056465,
3206+
0.0,
3207+
115698.25,
3208+
0.0,
3209+
-216.8587081056465,
3210+
2818720.0,
3211+
)
3212+
)

0 commit comments

Comments
 (0)