Skip to content
Prev 8316 / 29559 Next

overlay spatialgrid on spatial polygons data frame

Or essentially the same with using the polygonValues function in
raster (with weights=TRUE)

library(raster)
r <- raster(test)
r[] <- 1:ncell(r)
# finding out the fraction covered by each polygon
v <- polygonValues(srdf, r, weights=TRUE)

# below could possibly done more elegantly in an sapply
vv <- matrix(nrow=0, ncol=2)
polyvar <- 2  # second variable of the SpPolDF
for (i in 1:length(v)) {
	a <- v[[i]]
	vv <- rbind(vv, cbind(a[,1], a[,2] * srdf at data[i,polyvar]))
}

# sum over cells
vvv <- tapply(vv[,2], vv[,1], sum)
rr <- r
r[] <- NA
# apply cell values to raster
r[as.integer(rownames(vvv))] <- vvv
plot(r)
plot(srdf, add=T)


Robert
On Thu, May 20, 2010 at 12:17 PM, Roger Bivand <Roger.Bivand at nhh.no> wrote: