Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as.im

Coercion between sp objects and spatstat im objects


Description

Functions to convert between spatstats raster format im and sps SpatialGridDataFrame as well as one-way conversion from rasters rasterLayer to im. S4-style as() coercion can be used between im and SpatialGridDataFrame objects.

Usage

as.im.SpatialGridDataFrame(from)
as.SpatialGridDataFrame.im(from)
as.im.RasterLayer(from, factor.col.name = NULL)

Arguments

from

object to coerce from

factor.col.name

column name of levels(from) to be treated as a factor; if NULL defaults to last column of from. Ignored if from is not a raster with factor values.

Details

A SpatialGridDataFrame object may contain several columns of data such that several values are associated with each grid cell. In contrast an im object can only contain a single variable value for each cell. In as.im.SpatialGridDataFrame() the first data column is used. To convert another column to im format simply extract this column first as shown in the example below.

Methods

coerce

signature(from = "SpatialGridDataFrame", to = "im")

coerce

signature(from = "im", to = "SpatialGridDataFrame")

Warning

In spatstat all spatial objects are assumed to be planar. This means that spatstat is not designed to work directly with geographic (longitude and latitude) coordinates. If a sp object is declared to have geographic (unprojected) coordinates maptools refuses to convert directly to spatstat format. Rather, these should be projected first using e.g. spTransform. If you know what you are doing, and really want to force coercion, you can overwrite the proj4string of the sp object with NA, proj4string(x) <- CRS(NA), which will fool the system to think that the data is in local planar coordinates. This is probably not a good idea!

Author(s)

Edzer Pebesma edzer.pebesma@uni-muenster.de, Roger Bivand

See Also

Examples

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")
plot(mg_im)
}
if (run) {
## Convert back to SpatialGridDataFrame and plot it again
mg2 <- as.SpatialGridDataFrame.im(mg_im)
image(mg2, axes=TRUE)
}
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)
}
if (run) {
}

maptools

Tools for Handling Spatial Objects

v1.1-1
GPL (>= 2)
Authors
Roger Bivand [cre, aut] (<https://orcid.org/0000-0003-2392-6140>), Nicholas Lewin-Koh [aut], Edzer Pebesma [ctb], Eric Archer [ctb], Adrian Baddeley [ctb], Nick Bearman [ctb], Hans-Jörg Bibiko [ctb], Steven Brey [ctb], Jonathan Callahan [ctb], German Carrillo [ctb], Stéphane Dray [ctb], David Forrest [ctb], Michael Friendly [ctb], Patrick Giraudoux [ctb], Duncan Golicher [ctb], Virgilio Gómez Rubio [ctb], Patrick Hausmann [ctb], Karl Ove Hufthammer [ctb], Thomas Jagger [ctb], Kent Johnson [ctb], Matthew Lewis [ctb] (<https://orcid.org/0000-0003-2244-4078>), Sebastian Luque [ctb], Don MacQueen [ctb], Andrew Niccolai [ctb], Edzer Pebesma [ctb], Oscar Perpiñán Lamigueiro [ctb], Ethan Plunkett [ctb], Ege Rubak [ctb] (<https://orcid.org/0000-0002-6675-533X>), Tom Short [ctb], Greg Snow [ctb], Ben Stabler [ctb], Murray Stokely [ctb], Rolf Turner [ctb]
Initial release
2021-03-14

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.