Repository: corteva/rioxarray

Issue:
"At least one of the clipped raster x,y coordinates has only one point" Error when using rio.clip_box
Hi,

I am a beginner using the package to read rasters in a lazy mode. 

 I ran into this error message that seems should only occur when doing the integration test instead of general usage: "At least one of the clipped raster x,y coordinates has only one point." 

Is there any guideline that I can refer to solve this problem? Many thanks!

The function is here:
```
def raster_to_df_rioxarray(file_name, lb, rb, tb, bb):
    # read the raster file and convert to an xarray dataframe
    
    # try to open raster file with rioxarray, if nodata exception then return empty dataframe
    try: #print exception information if error happens and return empty dataframe
        raster  = rxr.open_rasterio(file_name).rio.clip_box( 
        minx = lb,
        miny = tb,
        maxx = rb,
        maxy = bb,
        ) 
    except Exception as e:
        # only print if e does not contain "No data", because these are the unexpected exceptions
        if 'No data' not in str(e):
            print('exception file name:', file_name, e)
        return None

    raster = raster.rename('intensity')

    df = raster.to_dask_dataframe()
    raster.close()
    df = df.where(df['intensity'] > 0)
    df = df.dropna()
    df = df.compute() # tranforming to pandas dataframe
    
    return df
```



Discussion:
codecov[bot]: ## [Codecov](https://app.codecov.io/gh/corteva/rioxarray/pull/796?dropdown=coverage&src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=corteva) Report
All modified and coverable lines are covered by tests :white_check_mark:
> Project coverage is 95.91%. Comparing base [(`ae6635a`)](https://app.codecov.io/gh/corteva/rioxarray/commit/ae6635aaa706cddb25e041726f7dd6c4165a8c85?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=corteva) to head [(`ea96996`)](https://app.codecov.io/gh/corteva/rioxarray/commit/ea96996269d688cd758b636d0a5f2927002d71cf?dropdown=coverage&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=corteva).
> Report is 95 commits behind head on master.

<details><summary>Additional details and impacted files</summary>


```diff
@@           Coverage Diff           @@
##           master     #796   +/-   ##
=======================================
  Coverage   95.90%   95.91%           
=======================================
  Files          12       12           
  Lines        1735     1762   +27     
=======================================
+ Hits         1664     1690   +26     
- Misses         71       72    +1     
```

</details>

[:umbrella: View full report in Codecov by Sentry](https://app.codecov.io/gh/corteva/rioxarray/pull/796?dropdown=coverage&src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=corteva).   
:loudspeaker: Have feedback on the report? [Share it here](https://about.codecov.io/codecov-pr-comment-feedback/?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=corteva).


Patch:
diff --git a/docs/history.rst b/docs/history.rst
index 1dc5c5ac..4b0aa59b 100644
--- a/docs/history.rst
+++ b/docs/history.rst
@@ -1,8 +1,9 @@
 History
 =======
 
-Latest
+0.16.0
 ------
+- ENH: Add `allow_one_dimensional_raster` option to `rio.clip_box` (issue #708)
 
 0.15.7
 ------
diff --git a/rioxarray/raster_array.py b/rioxarray/raster_array.py
index 2d068abe..bfbd6578 100644
--- a/rioxarray/raster_array.py
+++ b/rioxarray/raster_array.py
@@ -713,10 +713,12 @@ class RasterArray(XRasterBase):
         auto_expand: Union[bool, int] = False,
         auto_expand_limit: int = 3,
         crs: Optional[Any] = None,
+        allow_one_dimensional_raster: bool = False,
     ) -> xarray.DataArray:
         """Clip the :obj:`xarray.DataArray` by a bounding box.
 
         .. versionadded:: 0.12 crs
+        .. versionadded:: 0.16 allow_one_dimensional_raster
 
         Parameters
         ----------
@@ -736,16 +738,19 @@ class RasterArray(XRasterBase):
         crs: :obj:`rasterio.crs.CRS`, optional
             The CRS of the bounding box. Default is to assume it is the same
             as the dataset.
+        allow_one_dimensional_raster: bool, optional
+            If True, allow clipping to/from a one dimensional raster.
 
         Returns
         -------
         xarray.DataArray:
             The clipped object.
         """
-        if self.width == 1 or self.height == 1:
+        if not allow_one_dimensional_raster and (self.width == 1 or self.height == 1):
             raise OneDimensionalRaster(
                 "At least one of the raster x,y coordinates has only one point."
-                f"{_get_data_var_message(self._obj)}"
+                f"{_get_data_var_message(self._obj)}. "
+                "Set allow_one_dimensional_raster=True to disable this error."
             )
 
         if crs is not None and self.crs is None:
@@ -819,11 +824,14 @@ class RasterArray(XRasterBase):
                 raise NoDataInBounds(
                     f"No data found in bounds.{_get_data_var_message(self._obj)}"
                 )
-            if cl_array.rio.width == 1 or cl_array.rio.height == 1:
+            if not allow_one_dimensional_raster and (
+                cl_array.rio.width == 1 or cl_array.rio.height == 1
+            ):
                 raise OneDimensionalRaster(
                     "At least one of the clipped raster x,y coordinates"
                     " has only one point."
-                    f"{_get_data_var_message(self._obj)}"
+                    f"{_get_data_var_message(self._obj)}. "
+                    "Set allow_one_dimensional_raster=True to disable this error."
                 )
 
         # make sure correct attributes preserved & projection added
diff --git a/rioxarray/raster_dataset.py b/rioxarray/raster_dataset.py
index 05d5e49b..93c5695e 100644
--- a/rioxarray/raster_dataset.py
+++ b/rioxarray/raster_dataset.py
@@ -256,12 +256,14 @@ class RasterDataset(XRasterBase):
         auto_expand: Union[bool, int] = False,
         auto_expand_limit: int = 3,
         crs: Optional[Any] = None,
+        allow_one_dimensional_raster: bool = False,
     ) -> xarray.Dataset:
         """Clip the :class:`xarray.Dataset` by a bounding box in dimensions 'x'/'y'.
 
         .. warning:: Clips variables that have dimensions 'x'/'y'. Others are appended as is.
 
         .. versionadded:: 0.12 crs
+        .. versionadded:: 0.16 allow_one_dimensional_raster
 
         Parameters
         ----------
@@ -281,6 +283,8 @@ class RasterDataset(XRasterBase):
         crs: :obj:`rasterio.crs.CRS`, optional
             The CRS of the bounding box. Default is to assume it is the same
             as the dataset.
+        allow_one_dimensional_raster: bool, optional
+            If True, allow clipping to/from a one dimensional raster.
 
         Returns
         -------
@@ -302,6 +306,7 @@ class RasterDataset(XRasterBase):
                         auto_expand=auto_expand,
                         auto_expand_limit=auto_expand_limit,
                         crs=crs,
+                        allow_one_dimensional_raster=allow_one_dimensional_raster,
                     )
                 )
             except MissingSpatialDimensionError:


Test patch:
diff --git a/test/integration/test_integration_rioxarray.py b/test/integration/test_integration_rioxarray.py
index e01270c7..a5b54457 100644
--- a/test/integration/test_integration_rioxarray.py
+++ b/test/integration/test_integration_rioxarray.py
@@ -418,6 +418,27 @@ def test_clip_box__one_dimension_error(modis_clip):
             )
 
 
+def test_clip_box__one_dimension_error__allowed(modis_clip):
+    with modis_clip["open"](modis_clip["input"]) as xdi:
+        # test after raster clipped
+        assert xdi.rio.clip_box(
+            minx=-7272735.53951584,  # xdi.x[5].values
+            miny=5048834.500182299,  # xdi.y[5].values
+            maxx=-7272735.53951584,  # xdi.x[5].values
+            maxy=5048834.500182299,  # xdi.y[5].values
+            allow_one_dimensional_raster=True,
+        ).rio.shape == (1, 1)
+        # test before raster clipped
+        if not isinstance(modis_clip["open"], partial):
+            assert xdi.isel(x=slice(5, 6), y=slice(5, 6)).rio.clip_box(
+                minx=-7272735.53951584,  # xdi.x[5].values
+                miny=5048371.187465771,  # xdi.y[7].values
+                maxx=-7272272.226799311,  # xdi.x[7].values
+                maxy=5048834.500182299,  # xdi.y[5].values
+                allow_one_dimensional_raster=True,
+            ).rio.shape == (1, 1)
+
+
 def test_clip_box__nodata_in_bounds():
     with rioxarray.open_rasterio(
         os.path.join(TEST_INPUT_DATA_DIR, "clip_bbox__out_of_bounds.tif")