Repository: corteva/rioxarray

Issue:
Add support for initializing from datacube CRS
https://github.com/opendatacube/datacube-core/blob/develop/datacube/utils/geometry/_base.py

Currently it raises an exception as it cannot initialize from it. Thinking about checking for the `crs_str` attribute.

Discussion:
snowman2: Note - commits will be squashed on merge.

Patch:
diff --git a/rioxarray/rioxarray.py b/rioxarray/rioxarray.py
index 077148da..6309aff5 100644
--- a/rioxarray/rioxarray.py
+++ b/rioxarray/rioxarray.py
@@ -245,6 +245,8 @@ class XRasterBase(object):
         Dataset with crs attribute.
 
         """
+        if hasattr(input_crs, "wkt"):
+            input_crs = input_crs.wkt
         crs = CRS.from_user_input(input_crs)
         if inplace:
             self._crs = crs
@@ -415,11 +417,11 @@ class RasterArray(XRasterBase):
                 crs_wkt = self._obj.coords[grid_mapping_coord].attrs["spatial_ref"]
             except KeyError:
                 crs_wkt = self._obj.coords[grid_mapping_coord].attrs["crs_wkt"]
-            self._crs = CRS.from_user_input(crs_wkt)
+            self.set_crs(crs_wkt, inplace=True)
         except KeyError:
             try:
-                # look in attrs for 'crs' from rasterio xarray
-                self._crs = CRS.from_user_input(self._obj.attrs["crs"])
+                # look in attrs for 'crs'
+                self.set_crs(self._obj.attrs["crs"], inplace=True)
             except KeyError:
                 self._crs = False
                 return None
diff --git a/sphinx/history.rst b/sphinx/history.rst
index b3daa8dc..eac055e2 100644
--- a/sphinx/history.rst
+++ b/sphinx/history.rst
@@ -4,6 +4,7 @@ History
 0.0.10
 -----
 - Add support for opening netcdf/hdf files with `rioxarray.open_rasterio` (issue #32)
+- Added support for custom CRS with wkt attribute for datacube CRS support (issue #35)
 
 0.0.9
 -----


Test patch:
diff --git a/test/integration/test_integration_rioxarray.py b/test/integration/test_integration_rioxarray.py
index e0beae15..840a6855 100644
--- a/test/integration/test_integration_rioxarray.py
+++ b/test/integration/test_integration_rioxarray.py
@@ -975,3 +975,24 @@ def test_crs_writer__missing():
         test_da.rio.write_crs()
     with pytest.raises(MissingCRS):
         test_da.to_dataset(name="test").rio.write_crs()
+
+
+class CustomCRS(object):
+    @property
+    def wkt(self):
+        return CRS.from_epsg(4326).to_wkt()
+
+    def __str__(self):
+        return self.wkt
+
+
+def test_crs_get_custom():
+    test_da = xarray.DataArray(
+        numpy.zeros((5, 5)),
+        dims=("y", "x"),
+        coords={"y": numpy.arange(1, 6), "x": numpy.arange(2, 7)},
+        attrs={"crs": CustomCRS()},
+    )
+    assert test_da.rio.crs.wkt == CustomCRS().wkt
+    test_ds = xarray.Dataset({"test": test_da})
+    assert test_ds.rio.crs.wkt == CustomCRS().wkt