[R-sig-Geo] convert owin object to SpatialPolygons object

Adrian Baddeley adrian at maths.uwa.edu.au
Fri May 22 03:29:14 CEST 2009


Tyler Dean Rudolph writes:

> However the conversion to a Spatial Polygons object does not seem to work

To convert an object of class 'owin' (spatstat) to 'SpatialPolygons' (sp) 
you can use the following code.

Adrian Baddeley

==================================================================================

# convert spatstat objects to sp classes

owin2Polygons <- function(x, id="1") {
  stopifnot(is.owin(x))
  x <- as.polygonal(x)
  closering <- function(df) { df[c(seq(nrow(df)), 1), ] }
  pieces <- lapply(x$bdry,
                   function(p) {
                     Polygon(coords=closering(cbind(p$x,p$y)),
                             hole=is.hole.xypolygon(p))  })
  z <- Polygons(pieces, id)
  return(z)
}

tess2SP <- function(x) {
  stopifnot(is.tess(x))
  y <- tiles(x)
  nam <- names(y)
  z <- list()
  for(i in seq(y))
    z[[i]] <- owin2Polygons(y[[i]], nam[i])
  return(SpatialPolygons(z))
}

owin2SP <- function(x) {
  stopifnot(is.owin(x))
  y <- owin2Polygons(x)
  z <- SpatialPolygons(list(y))
  return(z)
}



More information about the R-sig-Geo mailing list