Carrie Perkins
Carrie Perkins

Reputation: 139

Convert raster to im object

I would like to convert a raster to an im object but have so far been unable to do so.

Here is a reproducible example:

Create raster layer and add data to it:

r.toy <- raster(ncol=40, nrow=20)
r.toy[] <- rnorm(n=ncell(r.toy))

Plot the raster:

plot(r.toy)

raster

Convert to image:

r.toy.im <- as.im(r.toy)

I receive the following error:

Error in as.im.default(r.toy) : Can't convert X to a pixel image

Upvotes: 0

Views: 1094

Answers (1)

Ege Rubak
Ege Rubak

Reputation: 4507

You just need to load the maptools package which can convert many spatial formats in R:

library(raster)
library(spatstat)
library(maptools)
r.toy <- raster(ncol=40, nrow=20)
r.toy[] <- rnorm(n=ncell(r.toy))
r.toy.im <- as.im(r.toy)
plot(r.toy.im)

Created on 2018-11-11 by the reprex package (v0.2.1)

Upvotes: 4

Related Questions