[R-sig-Geo] Overlaying a grid onto a set of points

Edzer Pebesma edzer.pebesma at uni-muenster.de
Tue May 29 11:58:54 CEST 2012



On 05/28/2012 11:00 PM, Gavin Simpson wrote:
> Dear List,
> 
> I have a SpatialGrid object - a regular 5km by 5km over the entire land
> mass of Great Britain & NI - and a SpatialPointsDataFrame containing a
> @data slot with several attributes.
> 
> I want to identify which points are in each grid cell and average the
> values of the individual attributes from the points within each cell.
> Not all cells will contain a point (for those an `NA` returned value is
> fine) though some cells will contain many points. Later I may wish to
> calculate some other summary (min or max) rather than mean.
> 
> If I understand correctly, this gives the desired result:
> 
> aggregate(fabResults.sp, by = frameGrid, FUN = mean)
> 
> where `fabResults.sp` is my SpatialPointsDataFrame and `frameGrid` is my
> Spatial Grid. This takes a while to compute however.
> 
> I was unable to come up with an `over()` call that worked.
> 
> Have I got the `aggregate()` step right? Is there a way to use `over()`
> to do the same operation? Am I missing a better alternative?

aggregate.Spatial uses over() under the hood:

> sp::aggregate.Spatial
function (x, by, FUN = mean, ...)
{
    by0 = by
    if (gridded(by))
        by = as(by, "SpatialPolygons")
    df = over(by, x, fn = FUN, ...)
    addAttrToGeom(by0, df, match.ID = FALSE)
}

but returns the Spatial* object, rather than a vector, data.frame or
list. As you can see, it coerces the grid to polygons first, which may
be very inefficient.

Did it work?
-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list