2

I am working with R and use the following libraries: raster, sp, rgeos, rgdal

I created a RasterLayer from a SpatialPointsDataFrame. This raster can easily be exported using

raster::writeRaster(ras8b,
                filename="filepath", format="GTiff" ,
                datatype='INT1U')

The problem which I run in is the following: I need a 8BitsPerPixel GeoTiff. But the code I use saves the raster to an 64 BitsPerPixel Raster.

I tried to solve the problem already following several options I found on StackOverflow. For example: RasterLayer 16-bits into a RasterLayer 8-bits

This first option gave me a 8 BitsPerPixel raster, but no values in it, just NAs.

Or I converted the RasterLayer in R to integer following this post: https://gis.stackexchange.com/questions/175383/round-does-not-return-an-integer-raster-in-r/175384

But R continues to store the Raster as 64 BitsPerPixel Raster.

Somebody has a solution? If you need more information just let me know. Thanks a lot. Best regards.

Li Ku
  • 21
  • 2
  • In the end I solved the problem not within R but on the command-line as follows: gdal_translate -ot Byte -of GTiff path\Input.tif path\output.tif – Li Ku Nov 06 '17 at 12:21

1 Answers1

2

Seems to work:

library(raster)
r <- raster(ncol=10,nrow=10)
r[] <- 1:100
x <- writeRaster(r, 'test.tif', datatype='INT1U', overwrite=TRUE)
dataType(x)
#[1] "INT1U"

library(rgdal)
GDALinfo('test.tif')
#  GDType hasNoDataValue NoDataValue blockSize1 blockSize2
#1   Byte           TRUE         255         10         10

But note that this format will only store values from 0 - 255. All other values become NA

Robert Hijmans
  • 40,301
  • 4
  • 55
  • 63
  • Thank you Robert for your answer. I figured out my problem: Had to many libraries loaded, which hindered the export as 8bit per pixel image. – Li Ku Nov 17 '17 at 10:43