Skip to content
Prev 25354 / 29559 Next

Extract mean pixels values using circular samples

Dear R-sig Member

      I'd like to extract mean pixels values using x,y coordinates by 
defining a buffer or area surrounding individual point coordinate. My 
circular sample represents the radius of a circular region around each 
point.  I try unsuccessfully this using extract function with raster 
package, in my hypothetical example:

require(raster)
require(sp)
require(plotrix)##Only for draw circular areas

## Create a raster
r <- raster(nc=30, nr=30)
r <- setValues(r, round(runif(ncell(r))* 255))
plot(r)

#Selection of center of coordinates of my circular samples
xd     <- c(-24.99270,45.12069,99.40321,73.64419)
yd  <- c(-45.435267,-88.369745,-7.086949,44.174530)
pts <- data.frame(xd,yd)
pts_s<- SpatialPoints(pts)

#Representation of target areas for sampling
for (i in 1:length(xd)) {
draw.circle(pts$xd[i],pts$yd[i],20,border="blue",col="grey")
}
points(pts_s, col="red", pch=16)


#Extract mean pixel values in 4 circular sample with radius=20
r_sample <- extract(x = r, y = pts_s, buffer=20, fun=mean, df=TRUE)
r_sample
# It doesn,t work!!!!!!


     Could someone please help me?

Thanks,

Alexandre