Closed fdetsch closed 6 years ago
I don't think the issue is not about NA values: just put NAs in example of velox$extract()
, will work!). I think it is because your polygon is actually too small.
So as a workaround, you could just resample the data. Make first your example much smaller:
bio_easy <- crop(bio$bio1, extent(aut[1:3,]))
bio_easy2 <- mask(bio_easy, aut)
plot(bio_easy2)
plot(aut[1:3,], add=TRUE)
Resample and check:
bio_easy3 <- bio_easy2
nrow(bio_easy3) <- nrow(bio_easy2)*10
bio_easy3b <- raster::resample(bio_easy2, bio_easy3, method="ngb")
xtr <- velox(bio_easy3b)$extract(aut, fun = function(x) mean(x, na.rm = TRUE))
xtr[1:5,1]
What happens is that raster::extract()
returns values even for small with very small coverage (check with: raster::extract(bio_easy2, aut, df=TRUE, weights=TRUE, normalizeWeights=FALSE)
). On the other side, it seems like velox$extract() does not return these same cells. But this appears still to be an issue in itself?
As @MatthieuStigler has pointed out correctly, the initial issue was that the polygons didn't intersect with any cell centroid, thus the NA values.
As of version 0.1.0.9007 VeloxRaster_extract
features a small
argument that avoids this type of problem. See ?VeloxRaster_extract
for more information.
Great, thanks – exactly what I was looking for!
I have a 30mx30m pixel resolution raster I would like to sample with a 1kmX1km cell sized polygon grid (fun=mean). It appears that in any cell where a single pixel is a missing value, Velox returns an NA for the entire cell. The number of NAs remains constant for the extract whether small is TRUE or FALSE. Am I understanding correctly that there is no way to have Velox ignore the missing pixels and return the mean value of the pixels that do reside within each grid cell?
@hsrobinson try using fun = function(x) mean(x, na.rm = TRUE))
instead of fun= mean
That worked perfectly. Thank-you.
It would be nice to include a
na.rm
argument for the velox extraction method (see?VeloxRaster_extract
). Although the latter performs considerably faster as compared toraster::extract
, I consider the lack of a proper handling of missing values a major drawback.For example, the below code taken from gis-stackexchange works fine using a raster-based approach (i.e.,
extract
) along withna.rm = TRUE
:Unfortunately, NAs are not omitted using
velox
(regardless of the fact that it performs amazingly fast):Here is my
sessionInfo
: