maptools (version 0.9-8)

as.owin: Coercion between sp objects and spatstat owin objects

Description

Functions to convert between spatstats observation window (owin) format and various sp formats. S4-style as() coercion can be used as well.

Usage

as.owin.SpatialPolygons(W, …, fatal)
as.owin.SpatialGridDataFrame(W, …, fatal)
as.owin.SpatialPixelsDataFrame(W, …, fatal)
as.SpatialPolygons.owin(x)

Arguments

W

SpatialPolygons object to coerce to owin

x

owin object to coerce to SpatialPolygons format

ignored

fatal

formal coercion argument; ignored

Methods

coerce

signature(from = "SpatialPolygons", to = "owin")

coerce

signature(from = "SpatialPixelsDataFrame", to = "owin")

coerce

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

coerce

signature(from = "owin", to = "SpatialPolygons")

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 an empty string, proj4string(x) <- "", which will fool the system to think that the data is in local planar coordinates. This is probably not a good idea!

Details

An observation window in spatstat defines a planar region. It is typically used to represent a sampling region. It comes in three different formats: a simple rectangle, a polygon (vector format) or a binary mask (TRUE/FALSE grid; raster format). These can all be coerced to polygonal form internally in spatstat and then converted to SpatialPolygons, which is what as.SpatialPolygons.owin() does. For objects of class SpatialPolygons (and SpatialPolygonsDataFrame) the sp polygons are simply extracted and cast into spatstats polygon format inside the owin object. For SpatialPixelsDataFrame and SpatialGridDataFrame the grid is extracted and cast into spatstats mask format inside the owin object. In all cases any data apart from the spatial region itself are discarded.

Examples

Run this code
# NOT RUN {
run <- FALSE
if (require(spatstat, quietly=TRUE)) run <- TRUE
if (run) {
## SpatialPixelsDataFrame -> owin
data(meuse.grid) # A data.frame
gridded(meuse.grid) = ~x+y # Now a SpatialPixelsDataFrame
mg_owin <- as(meuse.grid, "owin")
mg_owin
}
if (run) {
## SpatialGridDataFrame -> owin
fullgrid(meuse.grid) <- TRUE # Now a SpatialGridDataFrame
mg_owin2 <- as(meuse.grid, "owin")
}
if (run) {
## SpatialPolygons region with a hole
ho_sp <- SpatialPolygons(list(Polygons(list(Polygon(cbind(c(0,1,1,0,0),
  c(0,0,1,1,0))), Polygon(cbind(c(0.6,0.4,0.4,0.6,0.6), 
  c(0.2,0.2,0.4,0.4,0.2)), hole=TRUE)), ID="ho")))
plot(ho_sp, col="red", pbg="pink")
}
if (run) {
## SpatialPolygons -> owin
ho <- as(ho_sp, "owin")
plot(ho)
}
if (run) {
## Define owin directly and check they are identical
ho_orig <- owin(poly=list(list(x=c(0,1,1,0), y=c(0,0,1,1)),
  list(x=c(0.6,0.4,0.4,0.6), y=c(0.2,0.2,0.4,0.4))))
identical(ho, ho_orig)
}
if (run) {
## owin -> SpatialPolygons
ho_sp1 <- as(ho, "SpatialPolygons")
all.equal(ho_sp, ho_sp1, check.attributes=FALSE)
}
# }

Run the code above in your browser using DataCamp Workspace