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

Robert J. Hijmans r.hijmans at gmail.com
Wed Sep 3 18:50:12 CEST 2014


Jean-Luc,

You can also use sampleRegular with argument asRaster=TRUE. For example:

sampleRegular(MyRaster, 1000, asRaster=TRUE)

Robert

On Wed, Sep 3, 2014 at 5:39 AM, Loïc <loic.dutrieux at wur.nl> wrote:
> 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
>
>
> _______________________________________________
> 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