You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In [1]: importrioxarrayIn [2]: importxarrayasxrIn [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]: dOut[5]:
<xarray.DataArray (band: 1, y: 3601, x: 3601)>
[12967201valueswithdtype=int16]
Coordinates:
*band (band) int641*y (y) float642.02.01.9991.9991.999 ... 1.0011.0011.0011.01.0*x (x) float64103.0103.0103.0103.0 ... 104.0104.0104.0104.0Attributes:
transform: (0.000277777777777778, 0.0, 102.999861111111, 0.0, -0.000...
crs: +init=epsg:4326res: (0.000277777777777778, 0.000277777777777778)
is_tiled: 1nodatavals: (nan,)
scales: (1.0,)
offsets: (0.0,)
descriptions: ('Band 1',)
AREA_OR_POINT: AreaBand_1: Band1In [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-6projstring=_prepare_from_string(projparams)
---------------------------------------------------------------------------ValueErrorTraceback (mostrecentcalllast)
<ipython-input-6-49e09f098c16>in<module>---->1d.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 )
->1533ifself.nodataisnotNoneandnotnp.isnan(self.nodata):
1534cropped_ds=cropped_ds.fillna(self.nodata)
1535~/##/.venv/lib/python3.8/site-packages/rioxarray/rioxarray.py in nodata(self)1107returnNone1108->1109self._nodata=_ensure_nodata_dtype(self._nodata, self._obj.dtype)
1110returnself._nodata1111~/##/.venv/lib/python3.8/site-packages/rioxarray/rioxarray.py in _ensure_nodata_dtype(original_nodata, new_dtype)265 """
266original_nodata=float(original_nodata)
-->267nodata=np.dtype(new_dtype).type(original_nodata)
268ifnotnp.isnan(nodata) andoriginal_nodata!=nodata:
269warnings.warn(
ValueError: cannotconvertfloatNaNtointeger
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.
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_rasteriohere.
The current functionality for the data type checking works as expected and so I don't plan on changing anything there.
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. Thenodatavals
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()"
The text was updated successfully, but these errors were encountered: