hyriver / py3dep

A part of HyRiver software stack for getting topography data within the US through 3D Elevation Program (3DEP)
https://docs.hyriver.io
Other
42 stars 7 forks source link

Error Saving Raster Using Rioxarray #30

Closed GregoryPetrochenkov-NOAA closed 3 years ago

GregoryPetrochenkov-NOAA commented 3 years ago

I was using the following code in the README and was unable to save a raster of elevation:

import py3dep
from pynhd import NLDI

geom = NLDI().get_basins("01031500").geometry[0]
dem = py3dep.get_map("DEM", geom, resolution=30, geo_crs="epsg:4326", crs="epsg:3857")
slope = py3dep.get_map("Slope Degrees", geom, resolution=30)
slope = py3dep.deg2mpm(slope)

import rioxarray

dem.rio.to_raster("dem_01031500.tif")

I got the following error:

/home/gerund/miniconda3/envs/rapidfim/lib/python3.9/site-packages/rioxarray/rioxarray.py:541: UserWarning: Non-rectilinear transform found. Unable to recalculate.
  warnings.warn("Non-rectilinear transform found. Unable to recalculate.")

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_20372/64869872.py in <module>
      1 import rioxarray
      2 
----> 3 dem.rio.to_raster("dem_01031500.tif")

~/miniconda3/envs/rapidfim/lib/python3.9/site-packages/rioxarray/raster_array.py in to_raster(self, raster_path, driver, dtype, tags, windowed, recalc_transform, lock, compute, **profile_kwargs)
    965         )
    966 
--> 967         return RasterioWriter(raster_path=raster_path).to_raster(
    968             xarray_dataarray=self._obj,
    969             tags=tags,

~/miniconda3/envs/rapidfim/lib/python3.9/site-packages/rioxarray/raster_writer.py in to_raster(self, xarray_dataarray, tags, windowed, lock, compute, **kwargs)
    236 
    237         with rasterio.open(self.raster_path, "w", **kwargs) as rds:
--> 238             _write_metatata_to_raster(rds, xarray_dataarray, tags)
    239             if not (lock and is_dask_collection(xarray_dataarray.data)):
    240                 # write data to raster immmediately if not dask array

~/miniconda3/envs/rapidfim/lib/python3.9/site-packages/rioxarray/raster_writer.py in _write_metatata_to_raster(raster_handle, xarray_dataset, tags)
     43     # write scales and offsets
     44     try:
---> 45         raster_handle.scales = tags["scales"]
     46     except KeyError:
     47         scale_factor = tags.get(

rasterio/_base.pyx in rasterio._base.DatasetBase.scales.__set__()

rasterio/_io.pyx in rasterio._io.DatasetWriterBase._set_all_scales()

TypeError: object of type 'numpy.float64' has no len()

Environment: Python 3.9.6

Output of py3dep.show_versions() ``` INSTALLED VERSIONS ------------------ commit: None python: 3.9.6 (default, Aug 18 2021, 19:38:01) [GCC 7.5.0] python-bits: 64 OS: Linux OS-release: 5.11.0-34-generic machine: x86_64 processor: x86_64 byteorder: little LC_ALL: None LANG: en_US.UTF-8 LOCALE: en_US.UTF-8 affine: 2.3.0 aiohttp: 3.7.4.post0 aiohttp-client-cache>=0.5.1: None aiosqlite: 0.17.0 async-retriever: 0.2.4 async-retriever>=0.2: None async-retriever>=0.2.4: None click: 7.1.2 cytoolz: 0.11.0 dask: 2021.09.0 defusedxml: 0.7.1 geopandas>=0.7: None lxml: 4.6.3 nest-asyncio: installed netCDF4: 1.5.7 networkx: 2.6.3 numpy>=1.17: None owslib: 0.25.0 pandas>=1.0: None pip: 21.2.4 py3dep: 0.11.1 pyarrow: 5.0.0 pydantic: 1.8.2 pydaymet: 0.11.1 pygeohydro: None pygeoogc: 0.11.5 pygeoogc>=0.11: None pygeoogc>=0.11.5: None pygeoutils: 0.11.4 pygeoutils>=0.11: None pygeoutils>=0.11.4: None pynhd: 0.11.3 pyproj>=2.2: None pytest: None rasterio: 1.2.8 requests: 2.26.0 requests-cache>=0.8: None scipy: 1.7.1 setuptools: 52.0.0.post20210125 shapely>=1.6: None simplejson: 3.17.5 ujson: 4.1.0 urllib3: 1.26.6 ward: None xarray>=0.17: None yaml: 5.4.1 ```
cheginit commented 3 years ago

Thanks for reporting this issue. It's related to rioxarray expecting tuples for scales and offsets attributes, rather than float numbers. I will push a fix shortly.

cheginit commented 3 years ago

Please install pygeoutils from its Github repository. The issue is fixed now. I will release the fix for pygeoutils in a couple of hours:

pip install git+https://github.com/cheginit/pygeoutils
cheginit commented 3 years ago

I already released the fix to pypi so you should now be able to install pygeoutils using pip install -U pygeoutils. However, adding it conda-forge repository is going to take a couple hours, since that is automated. Please give it a try and let me know if it fixes your issue.

cheginit commented 3 years ago

I released a new version for py3dep as well that fixes a couple of issues, you might want to install that as well.

GregoryPetrochenkov-NOAA commented 3 years ago

Thanks so much for the fixes! I can confirm that py3dep is working properly now.