[R-sig-Geo] Overlay-ing two grids
Robert J. Hijmans
r.hijmans at gmail.com
Tue Dec 22 19:56:59 CET 2009
Renaud,
Here another way:
library(raster)
r <- raster(sgdf1, 1)
ra <- aggregate(r, 10, mean)
sgdf3 <- as(ra, 'SpatialGridDataFrame')
> values(ra)
[1] 0.43 0.45 0.57 0.50 0.49 0.47 0.58 0.50 0.61 0.49 0.49 0.53 0.54
0.51 0.53 0.42 0.48 0.51 0.54 0.53 0.50
[22] 0.52 0.39 0.52 0.52 0.62 0.53 0.48 0.52 0.48 0.52 0.45 0.50 0.48
0.43 0.47 0.52 0.49 0.43 0.59 0.54 0.52
[43] 0.53 0.53 0.48 0.57 0.46 0.54 0.49 0.48 0.47 0.48 0.47 0.50 0.48
0.49 0.47 0.45 0.50 0.52 0.54 0.46 0.49
[64] 0.52 0.54 0.50 0.46 0.43 0.49 0.46 0.55 0.52 0.52 0.47 0.41 0.51
0.56 0.40 0.56 0.50 0.47 0.48 0.55 0.44
[85] 0.57 0.44 0.51 0.49 0.50 0.49 0.56 0.54 0.46 0.50 0.52 0.49 0.52
0.42 0.57 0.54
>
Robert
On Tue, Dec 22, 2009 at 1:36 AM, Renaud Lancelot
<renaud.lancelot at cirad.fr> wrote:
> Dear all,
>
> I have two "nested" grids (SpatialGridDataFrame) looking like this:
>
> prj <- "+proj=utm +zone=28 +ellps=WGS84 +datum=WGS84 +units=m +no_defs"
> gt1 <- GridTopology(c(5, 5), c(10, 10), c(100, 100))
> gt2 <- GridTopology(c(50, 50), c(100, 100), c(10, 10))
> band1 <- sample(c(0, 1), replace = TRUE, size = 10000)
> sgdf1 <- SpatialGridDataFrame(gt1, data.frame(band1), CRS(prj))
> sgdf2 <- SpatialGridDataFrame(gt2, data.frame(band1=rep(0, 100)), CRS(prj))
> ## Graphic display
> spplot(sgdf1, scales = list(draw = TRUE),
> panel = function(...){
> panel.gridplot(...)
> sp.grid(sgdf2, col = 0)
> })
>
> I want to compute the mean (and other statistics) of sgdf1 at data$band1
> according to the cells defined in sgdf2. How can I do that ? I tried to use
> overlay, without success:
>
>> sgdf3 <- overlay(sgdf1, sgdf2, fn = mean)
>> sgdf3 at data$band1
> [1] 1 0 0 0 0 0 1 1 1 0 0 1 0 0 0 0 0 1 1 0 1 0 1 1 0 1 0 0 0 0
> [31] 0 0 1 0 1 1 1 0 0 0 0 0 0 1 0 1 1 0 0 0 1 0 1 0 1 0 1 1 0 0
> [61] 1 1 0 1 1 1 1 0 0 0 1 1 0 0 1 1 1 1 1 1 1 1 1 1 0 0 0 0 0 0
> [91] 1 0 1 1 1 0 1 0 1 0
>
> All the best,
>
> Renaud
>
>
>
> --
> Renaud Lancelot
> EDEN Project, coordinator
> http://www.eden-fp6project.net/
> << EDEN International Conference, Montpellier, 10-12 May 2010 >>
> << http://international-conference2010.eden-fp6project.net/ >>
>
> UMR CIRAD-INRA "Contrôle des maladies animales exotiques et émergentes"
> Joint research unit "Control of emerging and exotic animal diseases"
>
> CIRAD, Campus International de Baillarguet TA A-DIR / B
> F34398 Montpellier
> http://www.cirad.fr http://bluetongue.cirad.fr/
>
> Tel. +33 4 67 59 37 17 - Fax +33 4 67 59 37 95
> Secr. +33 4 67 59 37 37 - Cell. +33 6 77 52 08 69
>
> _______________________________________________
> R-sig-Geo mailing list
> R-sig-Geo at stat.math.ethz.ch
> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>
More information about the R-sig-Geo
mailing list