The example data from Jeffrey
library(raster)
r <- raster(ncols=10, nrows=10)
set.seed(0)
x <- runif(ncell(r))
x[round(runif(25,1,100),digits=0)] <- NA
r[] <- x
cds1 <- rbind(c(-180,-20), c(-160,5), c(-60, 0), c(-160,-60), c(-180,-20))
cds2 <- rbind(c(80,0), c(100,60), c(120,0), c(120,-55), c(80,0))
polys <- SpatialPolygons(list(Polygons(list(Polygon(cds1)), 1), Polygons(list(Polygon(cds2)), 2)))
polys <- SpatialPolygonsDataFrame(polys, data.frame(ID=sapply(slot(polys, "polygons"), function(x) slot(x, "ID"))))
Now use extract
extract(r, polys, fun=function(x, ...) length(na.omit(x))/length(x))
#[1] 0.8333333 0.6666667
If you have many rasters, first use stack to combine them (if they have the same extent and resolution)
To get the actual polygon area you should not use the slot(i, 'area') approach.
For planar data you can use rgeos::gArea(polys, byid=TRUE)
For spherical data (lon/lat) you can use geosphere::areaPolygon
The Euclidean Allocation tool can accept your polygon as input, but you are right you need to set the values inside the polygon to NoData for this tool to work, otherwise it will pick up the values that are already there.
First to feature to polygon, this will give values (any value, it doesn't matter, we're only interested in value or NoData) inside the polygon and NoData outside.
Now do IsNull on the polygon raster, this will give you a raster with 1 outside the polygon and 0 inside the polygon. Hereafter called IsNullRaster. This can be done in a single statement but I want to use it twice so it's worth persisting it to disc.
Con with the IsNullRaster: Con(IsNullRaster,ValueRaster)
- now you have a raster that is null inside the polygon and contains values outside the polygon. I'll call this one ValueRasterHole. If this is giving you grief do something like Con(IsNullRaster,ValueRaster,255)
and then use Set Raster Properties to enforce 255 as the nodata value.
Set your cell size, extent and snap raster to the ValueRaster and do Euclidean Allocation tool against the ValueRaster with the hole. This one is AllocatedRaster and will have values outside the polygon.
To merge the AllocatedRaster and the ValueRaster together use another Con with the IsNullRaster: Con(IsNullRaster,ValueRaster,AllocatedRaster)
will fill in the hole in the polygon with the allocated raster.
Best Answer
You do not specify the software you are using, so I assume it is R. Here is how you can do it:
Some example data
Solution