Skip to content

Overlaying a grid onto a set of points

4 messages · Gavin Simpson, Lyndon Estes, Robert J. Hijmans +1 more

#
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?

Thanks in advance,

Gavin
#
On 05/28/2012 11:00 PM, Gavin Simpson wrote:
aggregate.Spatial uses over() under the hood:
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?