I want to do weighted area mean of raster cells included in buffered points,spatialpointsdataframe but from what I have noticed the extract function only includes raster cells having their cell centers inside the 7 meter buffer. Raster cell size is 12.5 meter. It would be great if the function could calculate a weighted area mean of all raster cells not just cells having their centers inside the buffer zone. Anyone have an idea how to solve this? Code looking like this:

means <- extract(Raster, SpatialPointsDataframe, method='simple', buffer=7,weights=TRUE, normalizeWeights=T, small=T, fun=mean, df=T)

As an example this spatial point gets the value of one pixel with weighted mean 21

enter image description here

1 Answer 1


I have solved this problem thanks to https://gis.stackexchange.com/users/8520/jeffrey-evans

Using his example

results <- vector()  
  for(j in 1:nrow(x)) {
       lsub <- x[j, ]
       cr <- raster::crop(y, raster::extent(lsub), snap = "out") 
       r <- as(cr, "SpatialPolygonsDataFrame")
         names(r@data) <- "raster.value"
       r <- intersect(lsub, r)
       na.idx <- which(is.na(r$raster.value))
         if(length(na.idx) > 0) { r <- r[-na.idx,] }
       if (nrow(r) < 1) {
         results <- append(results, NA)
       } else {
         results <- append(results, weighted.mean(r@data[,"raster.value"], gArea(r, byid=TRUE)))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.