I try to compute the proportion of 0 in an area of 30km around a given pixel. The raster has a resolution of 100m and it is the map of France.
For this, I use the function focal()
of the raster
package (package version: 2.9.23). The analyse works well on a subset of the raster but not on the full raster. The error message is the following:
> Error in .local(x, row, nrows, ...) : validRow(x, row) is not TRUE
Looking for the .local
function:
getMethod("focal", signature = "RasterLayer")
I find .local()
but did not find its source on
github or in the source code.
validRow function
The function validRow()
checks that a number of rows is valid regarding the
raster.
r <- raster()
validRow(r, c(-1, 1, 100, 10000))
# -1 is obviously not valid and 10000 neither because the nb of rows of the
#raster is 180
I did not succeed to identify what operation in the focal()
function can lead
to this error.
The problem could be related to this open issue in github.
## Trying to reproduce the error
The most annoying part of the problem is that I do not succeed to reproduce the error with a minimal reproducible example:
library(raster)
# Get France raster at 100m resolution
p <- getData(name = "GADM", country = "france", level = 0)
p <- spTransform(p, CRSobj = CRS("+init=epsg:2154"))
r_total <- raster(p, resolution = 100)
# Crop and put dumb data
r <- crop(r_total, extent(98900, 150000, 6100000, 6150000))
nbcell <- ncell(r)
# Set values 0 or 1:
r <- setValues(r, rbinom(n = nbcell, size = 1, prob = .9))
# Put some NAs:
values(r)[sample(1:nbcell, size = .9*nbcell)] <- NA
# Compute the focal
fw <- focalWeight(r, 30000,"circle")
test <- focal(r,w = fw, fun = "sum", na.rm = TRUE, pad = TRUE, padValue = NA)
If I could reproduce the error, I hope that It will help to identify the problem with the real dataset.