[R-sig-Geo] regularly subset (subsample) a raster and get a raster

Loïc loic.dutrieux at wur.nl
Wed Sep 3 14:39:20 CEST 2014


Hi Jean-Luc,

Aggregate sounds like a safe option (memory safe and you do not lose the 
spatial information). Give it a try, it may not have such a big overhead.

## R
library(raster)
r <- raster(system.file(package = 'raster', 'external/test.grd'))
fun <- function(x) x[1]
r2 <- aggregate(r, by = 2, FUN = fun)

Best regards,

--
Loïc Dutrieux
Laboratory of Geo-Information Sciences and Remote Sensing
Wageningen University
The Netherlands


On 03/09/2014 13:56, Jean-Luc Dupouey wrote:
> Dear forumites,
>
> Using R, I want to regularly subsample a raster (for example, one point
> over two), and get a raster.
>
> Usual R subsampling works with raster layers:
>
> MyRaster[seq(1,nrow(MyRaster),2),seq(1,ncol(MyRaster),2))]
>
> but it only gives in return a vector of values, not a raster layer
> object. All the raster information is lost (spatial reference, etc.).
>
> How can I subsample my initial raster and still keep a raster?
>
> I thought of using "aggregate" or "crop" functions, but I am working on
> large datasets, and I would prefer to avoid any unnecessary calculations.
>
> Thank you in advance,
>
> Jean-Luc
>
> _______________________________________________
> 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