Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: sertit.rasters.slope cannot access the read method of raster dataset #22

Closed
qpoterek opened this issue Sep 23, 2024 · 1 comment
Closed
Labels
bug Something isn't working

Comments

@qpoterek
Copy link

qpoterek commented Sep 23, 2024

Describe the bug
The sertit.rasters.slope function no longer works when passing a path, dataarray, etc. Behind the scenes, the sertit.rasters_rio.slope function tries to access the read method of the datarray generated by the @any_raster_to_xr_ds decorator, which throws an error.

Code To Reproduce

from sertit import rasters
rasters.slope(r"D:\tmp\dtm.tif")

Output
Compete output obtained with maximal verbosity.

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters_rio.py:230, in any_raster_to_rio_ds.<locals>.wrapper(any_raster_type, *args, **kwargs)
    [228](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:228) nodata = get_nodata_value_from_xr(any_raster_type)
--> [230](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:230) if isinstance(any_raster_type, AnyXrDataStructure):
    [231](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:231)     meta = {
    [232](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:232)         "driver": "GTiff",
    [233](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:233)         "dtype": any_raster_type.dtype,
   (...)
    [239](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:239)         "transform": any_raster_type.rio.transform(),
    [240](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:240)     }

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\typing.py:720, in _BaseGenericAlias.__instancecheck__(self, obj)
    [719](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/typing.py:719) def __instancecheck__(self, obj):
--> [720](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/typing.py:720)     return self.__subclasscheck__(type(obj))

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\typing.py:723, in _BaseGenericAlias.__subclasscheck__(self, cls)
    [722](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/typing.py:722) def __subclasscheck__(self, cls):
--> [723](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/typing.py:723)     raise TypeError("Subscripted generics cannot be used with"
    [724](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/typing.py:724)                     " class and instance checks")

TypeError: Subscripted generics cannot be used with class and instance checks

During handling of the above exception, another exception occurred:

AttributeError                            Traceback (most recent call last)
Cell In[2], [line 1](vscode-notebook-cell:?execution_count=2&line=1)
----> [1](vscode-notebook-cell:?execution_count=2&line=1) rasters.slope(r"D:\tmp\cls_flood_depth\03MAPS\dtm_aoi.tif")

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters.py:247, in any_raster_to_xr_ds.<locals>.wrapper(any_raster_type, *args, **kwargs)
    [240](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:240)         # Convert path or rasterio.dataset to xr.dataset
    [241](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:241)         with rioxarray.open_rasterio(
    [242](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:242)             any_raster_type,
    [243](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:243)             masked=True,
    [244](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:244)             default_name=name,
    [245](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:245)             chunks=kwargs.pop("chunks", True),
    [246](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:246)         ) as xds:
--> [247](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:247)             out = function(xds, *args, **kwargs)
    [249](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:249) return out

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters.py:1652, in slope(xds, in_pct, in_rad)
   [1638](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1638) """
   [1639](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1639) Compute the slope of a DEM (in degrees).
   [1640](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1640) 
   (...)
   [1649](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1649)     AnyXrDataStructure: Slope
   [1650](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1650) """
   [1651](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1651) # Use classic option
-> [1652](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1652) arr, meta = rasters_rio.slope(xds, in_pct=in_pct, in_rad=in_rad)
   [1654](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters.py:1654) return xds.copy(data=arr)

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters_rio.py:254, in any_raster_to_rio_ds.<locals>.wrapper(any_raster_type, *args, **kwargs)
    [252](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:252)                 raise ex
    [253](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:253)         except Exception:
--> [254](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:254)             raise ex
    [255](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:255) return out

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters_rio.py:212, in any_raster_to_rio_ds.<locals>.wrapper(any_raster_type, *args, **kwargs)
    [201](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:201) """
    [202](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:202) any_raster_to_rio_ds wrapper
    [203](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:203) Args:
   (...)
    [209](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:209)     Any: regular output
    [210](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:210) """
    [211](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:211) try:
--> [212](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:212)     out = function(any_raster_type, *args, **kwargs)
    [213](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:213) except Exception as ex:
    [214](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:214)     if path.is_path(any_raster_type):

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\sertit\rasters_rio.py:1753, in slope(ds, in_pct, in_rad)
   [1726](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1726) @any_raster_to_rio_ds
   [1727](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1727) def slope(
   [1728](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1728)     ds: AnyRasterType,
   [1729](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1729)     in_pct: bool = False,
   [1730](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1730)     in_rad: bool = False,
   [1731](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1731) ) -> (np.ma.masked_array, dict):
   [1732](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1732)     """
   [1733](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1733)      Compute the slope of a DEM (in degrees).
   [1734](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1734) 
   (...)
   [1751](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1751)          (np.ma.masked_array, dict): Slope and its metadata
   [1752](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1752)     """
-> [1753](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1753)     array = ds.read(masked=True)
   [1755](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1755)     # Squeeze if needed
   [1756](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/sertit/rasters_rio.py:1756)     expand = False

File c:\Users\qpoterek\AppData\Local\anaconda3\envs\arcgispro-eo\lib\site-packages\xarray\core\common.py:280, in AttrAccessMixin.__getattr__(self, name)
    [278](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/xarray/core/common.py:278)         with suppress(KeyError):
    [279](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/xarray/core/common.py:279)             return source[name]
--> [280](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/xarray/core/common.py:280) raise AttributeError(
    [281](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/xarray/core/common.py:281)     f"{type(self).__name__!r} object has no attribute {name!r}"
    [282](file:///C:/Users/qpoterek/AppData/Local/anaconda3/envs/arcgispro-eo/lib/site-packages/xarray/core/common.py:282) )

AttributeError: 'DataArray' object has no attribute 'read'

Environment:

  • Python version: 3.9.18
  • sertit-utils version: 1.41.0

Changes related to the abovementioned decorator are available in sertit-utils==1.42.0, which is not installed by default on our internal ArcGIS Pro environment. I tested it in a toy environment with sertit-utils==1.42.0 and the same issue arises.

Additional context
At the moment, the following fugly workaround is being used, by calling sertit.rasters_rio.slope on a rasterio dataset.

import rasterio
from sertit import rasters_rio

dtm = rasterio.open(r"D:\tmp\dtm.tif")
data, meta = rasters_rio.slope(dtm)
dtm = rasters.read(r"D:\tmp\dtm.tif")
slope = dtm.copy(data=data)
@qpoterek qpoterek added the bug Something isn't working label Sep 23, 2024
@qpoterek qpoterek changed the title Bug: Bug: sertit.rasters.slope cannot access the read method of raster dataset Sep 23, 2024
@remi-braun
Copy link
Member

Solved according to @qpoterek

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants