run <- FALSE
if (require("spatstat.geom", quietly=TRUE)) run <- TRUE
if (run) {
## Extract an example SpatialGridDataFrame and plot it
data(meuse.grid) # A data.frame
gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame
fullgrid(meuse.grid) <- TRUE # Finally a SpatialGridDataFrame
mg_dist <- meuse.grid["dist"] # A SpatialGridDataFrame with a single column
image(mg_dist, axes=TRUE)
}
if (run) {
## Convert to im format and plot it
mg_im <- as(mg_dist, "im")
if (FALSE) {
plot(mg_im)
}
}
if (run) {
## Convert back to SpatialGridDataFrame and plot it again
mg2 <- as.SpatialGridDataFrame.im(mg_im)
if (FALSE) {
image(mg2, axes=TRUE)
}
}
if (FALSE) {
run <- run && require(raster, quietly=TRUE)
if (run) {
## Convert SpatialGridDataFrame -> RasterLayer -> im and plot it
r <- as(mg2, "RasterLayer")
r_im <- as.im.RasterLayer(r)
plot(r_im)
}
if (run) {
rr <- raster(nrow=2, ncol=3)
values(rr) <- 1:6
rr <- as.factor(rr)
rrr <- rr
f <- levels(rrr)[[1]]
f$name <- c("vector", "of", "values")
f$name2 <- letters[1:6]
levels(rrr) <- f
print(levels(rrr))
}
if (run) {
iii <- as.im.RasterLayer(rrr)
plot(iii)
}
if (run) {
iv <- as.im.RasterLayer(rrr, factor.col.name = "name")
plot(iv)
}
}
Run the code above in your browser using DataLab