Skip to content

Commit 9605e7d

Browse files
authored
BUG: Fix reproject with geoloc arrays not named xc|yc (#841)
1 parent 4745aec commit 9605e7d

3 files changed

Lines changed: 29 additions & 6 deletions

File tree

docs/history.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ History
33

44
Latest
55
------
6-
6+
- BUG: Fix reproject with geoloc arrays not named xc|yc (#840)
77

88
0.18.1
99
-------

rioxarray/rioxarray.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,9 +143,13 @@ def _get_nonspatial_coords(
143143
src_data_array.rio.x_dim,
144144
src_data_array.rio.y_dim,
145145
DEFAULT_GRID_MAP,
146-
"xc",
147-
"yc",
148146
}:
147+
# skip 2D spatial coords
148+
if (
149+
src_data_array.rio.x_dim in src_data_array[coord].dims
150+
and src_data_array.rio.y_dim in src_data_array[coord].dims
151+
):
152+
continue
149153
if src_data_array[coord].ndim == 1:
150154
coords[coord] = xarray.IndexVariable(
151155
src_data_array[coord].dims,

test/integration/test_integration_rioxarray.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3226,17 +3226,36 @@ def test_bounds__ordered__dataset():
32263226

32273227

32283228
@pytest.mark.skipif(not RASTERIO_GE_14, reason="Requires rasterio 1.4+")
3229-
def test_non_rectilinear__reproject(open_rasterio):
3229+
@pytest.mark.parametrize(
3230+
"rename",
3231+
[
3232+
None,
3233+
{"xc": "longitude", "yc": "latitude"},
3234+
{"y": "lines", "x": "pixels", "xc": "longitude", "yc": "latitude"},
3235+
],
3236+
)
3237+
def test_non_rectilinear__reproject(rename, open_rasterio):
32303238
test_file = os.path.join(TEST_INPUT_DATA_DIR, "2d_test.tif")
32313239
with open_rasterio(test_file) as xds:
3240+
x_2d_name = "xc"
3241+
y_2d_name = "yc"
3242+
if rename:
3243+
xds = xds.rename(rename)
3244+
x_2d_name = rename["xc"]
3245+
y_2d_name = rename["yc"]
3246+
if "x" in rename:
3247+
xds.rio.set_spatial_dims(rename["x"], rename["y"], inplace=True)
3248+
xds.rio.write_coordinate_system(inplace=True)
32323249
xds_1d = xds.rio.reproject(
32333250
"EPSG:4326",
32343251
src_geoloc_array=(
3235-
xds.coords["xc"].values,
3236-
xds.coords["yc"].values,
3252+
xds.coords[x_2d_name].values,
3253+
xds.coords[y_2d_name].values,
32373254
),
32383255
georeferencing_convention="PIXEL_CENTER",
32393256
)
3257+
assert x_2d_name not in xds_1d.coords
3258+
assert y_2d_name not in xds_1d.coords
32403259
assert xds_1d.coords["x"].shape == (14,)
32413260
assert xds_1d.coords["y"].shape == (10,)
32423261
xds_1d.rio.transform().almost_equals(

0 commit comments

Comments
 (0)