[R-sig-Geo] Set output data type for projectRaster

Loïc Dutrieux loic.dutrieux at wur.nl
Wed Jul 8 09:14:13 CEST 2015


Hi Ned,

dataType with capital T is the function to retrieve the data type of a 
raster object. When used as argument to set the type of a file being 
written, use datatype= (with lower case t).

Cheers,
Loïc

On 07/08/2015 02:32 AM, Ned Horning wrote:
> Hi all -
>
> I am trying to project a raster using the projectRaster() function from
> the raster package and would like the data type for the output image to
> be the same as the data type of the input image (INT2U Landsat 8 image
> stack). For some reason I get an output data type of FLT8S, even when I
> set the resampling method to "ngb". I created some sample code to
> recreate the problem.
> --
> library(rgdal)
> library(raster)
> # create a new (not projected) RasterLayer with cellnumbers as values
> r <- raster(xmn=-110, xmx=-90, ymn=40, ymx=60, ncols=40, nrows=40)
> r <- setValues(r, 1:ncell(r))
> dataType(r)  <- "INT2U"
> dataType(r)
> # proj.4 projection description
> newproj <- "+proj=lcc +lat_1=48 +lat_2=33 +lon_0=-100 +ellps=WGS84"
> pr1 <- projectRaster(r, crs=newproj, res=20000)
> dataType(pr1)
> pr2 <- projectRaster(r, crs=newproj, res=20000, method="ngb",
> filename="test.tif", dataType="INT2U", format='GTiff', overwrite=TRUE)
> dataType(pr2)
> --
> In this example the data types are
> r is INT2U
> pr1 is FLT4S
> pr2 is FLT8S
>
> but I want the data type for pr2 to be "INT2U".
>
> Is there any way to select the output data type for an image created
> using projectRaster()?
>
> I'm using R version 3.2.1 and version 2.3-40 of the raster package.
>
> All the best,
>
> Ned
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at r-project.org
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo



More information about the R-sig-Geo mailing list