Skip to content

Sampling SpatialPolygonDataFrame attributes from points in a grid..

2 messages · Rick Reeves, Roger Bivand

#
Greetings:

I'd like to get some advice on performing this task:


Given a SpatialPolygonsDataFrame, and a regularly-spaced point grid
created 'over' the polygons using spsample() or similar method, how
to extract one attribute from the polygon 'beneath' the location of
each point in the grid, and then assign the computed attribute to
the point? Best end product would be a SpatialPointsDataFrame.

The next step would be to replace the attribute of all points corresponding
to each polygon with a weighted average of all the point subset's attribute
value.

I am reviewing the spsample() and overlay() methods, which look like
good candidates for this task. But perhaps there is a better way.

Can someone direct me to a sample solution to a similar task,
for example, a vector polygon to raster grid conversion?

Thanks in advance,
Rick Reeves
NCEAS
#
On Wed, 21 May 2008, reeves at nceas.ucsb.edu wrote:

            
I have a feeling of uncertainty about the relative resolutions of the grid 
and the polygons, how many points per polygon do you expect to see?

library(maptools)
fn <- system.file("shapes/sids.shp", package="maptools")[1]
p4s <- CRS("+proj=longlat +ellps=clrk66")
xx <- readShapePoly(fn, IDvar="FIPSNO", proj4string=p4s)
xxG <- Sobj_SpatialGrid(xx, n=10000)
o <- overlay(xxG, xx)
zz <- as(xx, "data.frame")[o,]
xxSGDF <- SpatialGridDataFrame(slot(xxG, "grid"),
   proj4string=CRS(proj4string(xxG)), data=zz)
summary(xxSGDF)

Going the other way, you can use tapply() using o to form ragged arrays, 
for example, say to take median grid values by polygon. If you could 
illustrate using this or another available case, it would help.

Roger