Skip to content
Prev 4908 / 29559 Next

calculate raster values based on vector regions

Alexander, below is a reproducible example that uses a polygon data set 
from package maptools. You should read your polygons e.g. through 
readOGR (package rgdal) and your grid through readGDAL.

If your grid comes from readGDAL, then 2 and 3 can be omitted, and 5 
simplifies to:

out$SID79 = nc$SID79[idx]

All in R. Hth,
--
Edzer

library(maptools)
# 1 read a SpatialPolygonsDataFrame
nc <- readShapePoly(system.file("shapes/sids.shp",
        package="maptools")[1], proj4string=CRS("+proj=longlat 
+datum=NAD27"))
# 2 sample points on a regular grid:
grd = spsample(nc, 5000, "regular", offset = c(.5,.5))
# 3 convert points into grid:
gridded(grd) = TRUE
# 4 find index of polygons on grid cell centres:
idx = overlay(grd, nc)
# 5 merge grid points with attributes from polygons:
out = SpatialPixelsDataFrame(grd, data.frame(SIDS79 = nc$SID79[idx]))
# 6 plot:
spplot(out, col.regions=bpy.colors())
Alexander.Herr at csiro.au wrote: