2

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
  • some_file is your problem, you can see it and we cannot – mdsumner Oct 3 '13 at 13:17
  • What is "geostatp"? I started on an answer but baulked at assuming I could find that on CRAN. Perhaps it's local? The behaviour you seek would be provided by spatstat and maptools in my imagination but perhaps not yet? – mdsumner Oct 3 '13 at 13:23
  • The package name is 'geostatsp', not 'geostatp'. The link is provided in question now. – SS_Rebelious Oct 3 '13 at 13:27
  • @mdsumner, 'some_file' is not a problem because it doesn't work with any file. – SS_Rebelious Oct 3 '13 at 13:32
  • Ah, my typo - sorry. – mdsumner Oct 3 '13 at 21:55
2

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.