Search code examples
rr-raster

Bathymetry raster in R (negative value raster) not read in correctly


I have a bathymetry tif showing water depth (i.e., distance from surface in negative values). In arcMap the values range from -1 to -114. However, when I import to R, the values range from 128 to 255 with the smallest values (i.e, near 128) corresponding to the deepest water areas (i.e., near -114). I imagine that Raster package in R converts negative values, but it's not clear how as it's not just the absolute value.


Solution

  • It appears that the software that created the file stored the values as "signed byte", i.e. values ranging from -128 to 127.

    The raster package used the GDAL library to read these files. GDAL only recognizes "unsigned byte" values, i.e, ranging from 0 to 255. That explains why all values are shifted with 128.

    If you create the file in ArcMap you may be able to save it using a different datatype.

    I think you can also use these workarounds.

    Use an offset:

    library(raster)
    r <- raster('file.tif')
    offs(r) <- -128
    

    Or compute the correct values (and perhaps write that to a new file):

    r <- raster('file.tif')
    r <- r - 128
    r <- writeRaster(r, 'file2.tif', datatype='INT2S')