[R-sig-Geo] Converting Tessellation objects into shape files

Adrian Baddeley adrian at maths.uwa.edu.au
Mon Apr 20 07:44:48 CEST 2009


Adalberto Pineda <apineda at dataquick.com> writes:

> I was trying to convert an object of class tess generated using the spatstat package into a shape file

Source the code below, and then apply the function 'tess2SP' to your tessellation object. This will convert it to a SpatialPolygons object, which you can then output as a shapefile. 

(This only works if the tessellation is made of polygons or rectangles.)

Adrian Baddeley

-----------------------------------------------------------------------------------------

require(spatstat)
require(sp)

# 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)
  nom <- names(y)
  z <- list()
  for(i in seq(y))
    z[[i]] <- owin2Polygons(y[[i]], nom[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