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

Nodata value type causes error #212

Closed
jackieleng opened this issue Jan 21, 2021 · 2 comments
Closed

Nodata value type causes error #212

jackieleng opened this issue Jan 21, 2021 · 2 comments
Labels
question Further information is requested wontfix This will not be worked on

Comments

@jackieleng
Copy link

In [1]: import rioxarray

In [2]: import xarray as xr

In [3]: d = xr.open_rasterio('./data/ASTGTMV003_N01E103_dem.tif')

In [4]: g = {"type": "Polygon", "coordinates": [[[103.85548, 1.27909], [103.86977, 1.27909], [103.85548, 1.29498], [103.85548, 1.27909]]]}

In [5]: d
Out[5]:
<xarray.DataArray (band: 1, y: 3601, x: 3601)>
[12967201 values with dtype=int16]
Coordinates:
  * band     (band) int64 1
  * y        (y) float64 2.0 2.0 1.999 1.999 1.999 ... 1.001 1.001 1.001 1.0 1.0
  * x        (x) float64 103.0 103.0 103.0 103.0 ... 104.0 104.0 104.0 104.0
Attributes:
    transform:      (0.000277777777777778, 0.0, 102.999861111111, 0.0, -0.000...
    crs:            +init=epsg:4326
    res:            (0.000277777777777778, 0.000277777777777778)
    is_tiled:       1
    nodatavals:     (nan,)
    scales:         (1.0,)
    offsets:        (0.0,)
    descriptions:   ('Band 1',)
    AREA_OR_POINT:  Area
    Band_1:         Band 1

In [6]: d.sel(band=1).rio.clip([g])
/##/.venv/lib/python3.8/site-packages/pyproj/crs/crs.py:280: FutureWarning: '+init=<authority>:<code>' syntax is deprecated. '<authority>:<code>' is the preferred initialization method. When making the change, be mindful of axis order changes: https://pyproj4.github.io/pyproj/stable/gotchas.html#axis-order-changes-in-proj-6
  projstring = _prepare_from_string(projparams)
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-6-49e09f098c16> in <module>
----> 1 d.sel(band=1).rio.clip([g])

~/##/.venv/lib/python3.8/site-packages/rioxarray/rioxarray.py in clip(self, geometries, crs, all_touched, drop, invert)
   1531                 )
   1532             )
-> 1533         if self.nodata is not None and not np.isnan(self.nodata):
   1534             cropped_ds = cropped_ds.fillna(self.nodata)
   1535

~/##/.venv/lib/python3.8/site-packages/rioxarray/rioxarray.py in nodata(self)
   1107             return None
   1108
-> 1109         self._nodata = _ensure_nodata_dtype(self._nodata, self._obj.dtype)
   1110         return self._nodata
   1111

~/##/.venv/lib/python3.8/site-packages/rioxarray/rioxarray.py in _ensure_nodata_dtype(original_nodata, new_dtype)
    265     """
    266     original_nodata = float(original_nodata)
--> 267     nodata = np.dtype(new_dtype).type(original_nodata)
    268     if not np.isnan(nodata) and original_nodata != nodata:
    269         warnings.warn(

ValueError: cannot convert float NaN to integer

Problem description

I'm not sure if this is the correct place to post this, or if this issue belongs to the xarray repo. But I'm getting an error if I do a clipping after a selection (band=1 in this case). If I do not do a .sel, there seem to be no problem.

The problem seems to be because the data has a dtype of int16, but it has a 'nodatavals' attribute that is nan, which has a dtype of float. The nodatavals seems to be set by xarray. The _ensure_nodata_dtype tries to convert the nan into an integer, which causes the error.

Expected Output

No error.

Environment Information

  • python -c "import rioxarray; rioxarray.show_versions()"
(.venv)~/### (master *%)$ python -c "import rioxarray; rioxarray.show_versions()"
rioxarray (0.1.1) deps:
  rasterio: 1.1.8
    xarray: 0.16.2
      GDAL: 2.4.4

Other python deps:
     scipy: 1.5.4
    pyproj: 3.0.0.post1

System:
    python: 3.8.5 (default, Jul 28 2020, 12:59:40)  [GCC 9.3.0]
executable: /###/.venv/bin/python
   machine: Linux-4.19.128-microsoft-standard-x86_64-with-glibc2.29
@jackieleng jackieleng added the bug Something isn't working label Jan 21, 2021
@snowman2
Copy link
Member

I would recommend using rioxarray.open_rasterio.

@snowman2 snowman2 added question Further information is requested and removed bug Something isn't working labels Jan 22, 2021
@snowman2
Copy link
Member

snowman2 commented Jan 22, 2021

A few years ago, I added the nodatavals: pydata/xarray#1740

nan was used because None was not serializable with to_netcdf (not because nan was the nodata value). This is no longer the case when you use rioxarray.open_rasterio as it just doesn't store the no data value when there isn't one. Other benefits of using rioxarray.open_rasterio here.

The current functionality for the data type checking works as expected and so I don't plan on changing anything there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants