If you are open to experimentation and willing to use raster
package, here's a possible solution. Let us know.
The trick is, when overlaying polygons over rasters, to specify small = TRUE
, because centers of polygons don't always include cell center (see ?extract
).
If y represents polygons, a value is also returned for relatively small polygons (e.g. those smaller than a single cell of the Raster* object), or polygons with an odd shape, for which otherwise no values are returned because they do not cover any raster cell centers.
library(raster)
shp.raster <- raster(shp)
depo.sp.raster <- raster(depo.sp)
plot(depo.sp.raster)
plot(shp, add = TRUE)
zoom(depo.sp.raster) # click in the corners around the area to zoom in
plot(shp, add = TRUE)
res.out <- extract(depo.sp.raster, shp, layer = 1, small = TRUE)
table(unlist(lapply(res.out, is.null)))
> head(res.out)
[[1]]
0.0334963
[[2]]
0.2604089
[[3]]
[1] 0.5466648 0.7929624 0.4214012
[[4]]
[1] 0.2604089 0.3149418 0.5343358 0.0334963
[[5]]
0.5561869
[[6]]
0.2604089