Repository: corteva/rioxarray

Issue:
`reproject_match` raises `MissingSpatialDimensionError` with spatial dims set
#### Code Sample, a copy-pastable example if possible

```python
import numpy as np
import xarray as xr
import rioxarray

lat = [0.1, 0.15, 0.2]
lon = [0.1, 0.15, 0.2]
data = np.arange(1, 10).reshape(3, 3)
ds = xr.Dataset(
        data_vars={
            "foo": (["lat", "lon"], data),
            "bar": (["lat", "lon"], data),
        },
        coords={"lat": lat, "lon": lon},
    )

ds = ds.rio.set_spatial_dims(x_dim="lon", y_dim="lat")
ds = ds.rio.write_crs(4326)

ds = ds.rio.reproject_match(ds)
```

Thanks @bocalml for the MRE.

#### Problem description
This results in `MissingSpatialDimensionError: x dimension (lon) not found.`

This is likely related to the renaming mentioned in https://github.com/corteva/rioxarray/issues/751 but I haven't been able to make sense of this fully and the result is surprising to me.

<details> <summary> Full traceback </summary>

```python
---------------------------------------------------------------------------
MissingSpatialDimensionError              Traceback (most recent call last)
Cell In[31], line 1
----> 1 ds = ds.rio.reproject_match(ds)

File ~/miniconda3/envs/myenv/lib/python3.12/site-packages/rioxarray/raster_dataset.py:191, in RasterDataset.reproject_match(self, match_data_array, resampling, **reproject_kwargs)
    189             raise
    190         resampled_dataset[var] = self._obj[var].copy()
--> 191 return resampled_dataset.rio.set_spatial_dims(
    192     x_dim=self.x_dim, y_dim=self.y_dim, inplace=True
    193 )

File ~/miniconda3/envs/myenv/lib/python3.12/site-packages/rioxarray/rioxarray.py:853, in XRasterBase.set_spatial_dims(self, x_dim, y_dim, inplace)
    851     data_obj.rio._x_dim = x_dim
    852 else:
--> 853     raise MissingSpatialDimensionError(
    854         f”x dimension ({x_dim}) not found.{_get_data_var_message(data_obj)}”
    855     )
    856 if y_dim in data_obj.dims:
    857     data_obj.rio._y_dim = y_dim

MissingSpatialDimensionError: x dimension (lon) not found.
```
</details>

#### Expected Output
The dataset itself with no error.

#### Environment Information
```
rioxarray (0.15.3) deps:
  rasterio: 1.3.9
    xarray: 2024.3.0
      GDAL: 3.8.4
      GEOS: 3.12.1
      PROJ: 9.3.1
 PROJ DATA: [/Users/danieljahn/miniconda3/envs/myenv/share/proj](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/share/proj)
 GDAL DATA: [/Users/danieljahn/miniconda3/envs/myenv/share/gdal](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/share/gdal)

Other python deps:
     scipy: 1.12.0
    pyproj: 3.6.1

System:
    python: 3.12.2 | packaged by conda-forge | (main, Feb 16 2024, 20:54:21) [Clang 16.0.6 ]
executable: [/Users/danieljahn/miniconda3/envs/myenv/bin/python](https://file+.vscode-resource.vscode-cdn.net/Users/danieljahn/miniconda3/envs/myenv/bin/python)
   machine: macOS-14.2.1-arm64-arm-64bit
```

#### Installation method
Conda

#### Conda environment information (if you installed with conda):

<br/>
Environment (<code>conda list</code>):
<details>

```
$ conda list | grep -E "rasterio|xarray|gdal"
cf-xarray                 0.9.0                    pypi_0    pypi
gdal                      3.8.4           py312h56161e1_3    conda-forge
libgdal                   3.8.4                hd76467a_3    conda-forge
rasterio                  1.3.9           py312hd404879_2    conda-forge
rioxarray                 0.15.3             pyhd8ed1ab_0    conda-forge
xarray                    2024.3.0           pyhd8ed1ab_0    conda-forge
```
</details>

<br/>
Details about  <code>conda</code> and system ( <code>conda info</code> ):
<details>

```
$ conda info

     active environment : myenv
    active env location : /Users/danieljahn/miniconda3/envs/myenv
            shell level : 4
       user config file : /Users/danieljahn/.condarc
 populated config files : /Users/danieljahn/.condarc
          conda version : 23.3.1
    conda-build version : 3.23.3
         python version : 3.11.0.final.0
       virtual packages : __archspec=1=arm64
                          __osx=14.2.1=0
                          __unix=0=0
       base environment : /Users/danieljahn/miniconda3  (writable)
      conda av data dir : /Users/danieljahn/miniconda3/etc/conda
  conda av metadata url : None
           channel URLs : 
                          https://conda.anaconda.org/t/<TOKEN>/conda-forge/osx-arm64
                          https://conda.anaconda.org/t/<TOKEN>/conda-forge/noarch
          package cache : /Users/danieljahn/miniconda3/pkgs
                          /Users/danieljahn/.conda/pkgs
       envs directories : /Users/danieljahn/miniconda3/envs
                          /Users/danieljahn/.conda/envs
               platform : osx-arm64
             user-agent : conda/23.3.1 requests/2.31.0 CPython/3.11.0 Darwin/23.2.0 OSX/14.2.1
                UID:GID : 501:20
             netrc file : /Users/danieljahn/.netrc
           offline mode : False
```
</details>


Discussion:

Patch:
diff --git a/docs/history.rst b/docs/history.rst
index ccb14e52..5b33117d 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -3,6 +3,7 @@ History
 
 Latest
 ------
+- BUG:reproject_match: Remove setting spatial dims on output resampled dataset (issue #768)
 
 0.15.3
 ------
diff --git a/rioxarray/raster_dataset.py b/rioxarray/raster_dataset.py
index b96c7434..05d5e49b 100644
--- a/rioxarray/raster_dataset.py
+++ b/rioxarray/raster_dataset.py
@@ -188,9 +188,7 @@ class RasterDataset(XRasterBase):
                 ):
                     raise
                 resampled_dataset[var] = self._obj[var].copy()
-        return resampled_dataset.rio.set_spatial_dims(
-            x_dim=self.x_dim, y_dim=self.y_dim, inplace=True
-        )
+        return resampled_dataset
 
     def pad_box(
         self,


Test patch:
diff --git a/test/integration/test_integration_rioxarray.py b/test/integration/test_integration_rioxarray.py
index 34be4b01..e994fad0 100644
--- a/test/integration/test_integration_rioxarray.py
+++ b/test/integration/test_integration_rioxarray.py
@@ -781,6 +781,28 @@ def test_reproject_match__non_geospatial(dummy_dataset_non_geospatial):
         assert "non_geo_stuff" in ds_reproject.data_vars
 
 
+def test_reproject_match__geographic_dataset():
+    lat = [0.1, 0.15, 0.2]
+    lon = [0.1, 0.15, 0.2]
+    data = numpy.arange(1, 10).reshape(3, 3)
+    ds = xarray.Dataset(
+        data_vars={
+            "foo": (["lat", "lon"], data),
+            "bar": (["lat", "lon"], data),
+        },
+        coords={"lat": lat, "lon": lon},
+    )
+
+    ds = (
+        ds.rio.write_crs(4326, inplace=True)
+        .rio.set_spatial_dims(x_dim="lon", y_dim="lat", inplace=True)
+        .rio.write_coordinate_system(inplace=True)
+    )
+    ds_match = ds.rio.reproject_match(ds)
+    assert_almost_equal(ds_match.x.values, ds.lon.values)
+    assert_almost_equal(ds_match.y.values, ds.lat.values)
+
+
 def test_interpolate_na__non_geospatial(dummy_dataset_non_geospatial):
     pytest.importorskip("scipy")
     ds = dummy_dataset_non_geospatial