rspatial/raster

Changes to handling NA and NaN in file read/write in v3.6.0

waternumbers opened this issue · 1 comments

With the current versions of raster (3.2.58) and terra (1.6.7) the behaviour look like

r <-  raster::raster(ncols=3, nrows=3)
r[] <- NA; r[1] = NaN
raster::writeRaster(r,"na.tif")

is.nan(raster::raster("na.tif"))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 120, 60  (x, y)
## extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 0, 0  (min, max)

is.na(raster::raster("na.tif"))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 120, 60  (x, y)
## extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 1, 1  (min, max)

is.nan(terra::rast("na.tif"))
## class       : SpatRaster 
## dimensions  : 3, 3, 1  (nrow, ncol, nlyr)
## resolution  : 120, 60  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326) 
## source      : memory 
## name        :   na 
## min value   : TRUE 
## max value   : TRUE 

is.na(terra::rast("na.tif"))
## class       : SpatRaster 
## dimensions  : 3, 3, 1  (nrow, ncol, nlyr)
## resolution  : 120, 60  (x, y)
## extent      : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## coord. ref. : lon/lat WGS 84 (EPSG:4326) 
## source      : memory 
## name        :   na 
## min value   : TRUE 
## max value   : TRUE 

The logical values for testing for NaN are different. With the current development versions of raster v3.6.0 and terra I get

is.nan(raster::raster("na.tif"))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 120, 60  (x, y)
## extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 1, 1  (min, max)

is.na(raster::raster("na.tif"))
## class      : RasterLayer 
## dimensions : 3, 3, 9  (nrow, ncol, ncell)
## resolution : 120, 60  (x, y)
## extent     : -180, 180, -90, 90  (xmin, xmax, ymin, ymax)
## crs        : +proj=longlat +datum=WGS84 +no_defs 
## source     : memory
## names      : layer 
## values     : 1, 1  (min, max)

That is the behaviour of raster has changed to that of terra - missing values treated as NaN not NA
This has impacts - for example raster::zonal ignores NA but not NaN.

Is this an intentional change?

I think this now works again the way it used to. Thanks for reporting.