[R-sig-Geo] Overlay-ing two grids

Renaud Lancelot renaud.lancelot at cirad.fr
Tue Dec 22 10:36:17 CET 2009


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



More information about the R-sig-Geo mailing list