3

During script execution I need to convert 'RasterBrick' to 'im' object. Package 'geostatsp' is supposed to do this job:

library(geostatsp)
myraster = raster(matrix(1:100,10,10), xmn=0,xmx=10,ymn=0,ymx=10)
class(myraster)
>[1] "RasterLayer"
>attr(,"package")
>[1] "raster"

myIm = as.im(myraster)
class(myIm)
>[1] "im"

But when I try it with real data I catch an error:

library(geostatsp)

b <- brick(some_file)
r <- raster(b)
class(r)
>[1] "RasterLayer"
>attr(,"package")
>[1] "raster"

bim <- as.im(r)
>Error in .local(x, ...) : 'x' has no values

What causes this?

0
3

The problem is that the default method for raster("RasterBrick") gives an empty layer. So, (as the error message states) your "r" has no data.

Here's a reproducible example that needs no knowledge of your file system, though it has nothing to do with the as.im method really.

library(geostatsp)
myraster = raster(matrix(1:100,10,10), xmn=0,xmx=10,ymn=0,ymx=10)

some_file <- "some_file.grd"

writeRaster(myraster, some_file)

b <- brick(some_file)
## here's the "fix"
r <- raster(b, layer = 1)

as.im(r)

See ?raster::raster for the "RasterBrick" method.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.