Skip to content

convert spatstat owin-object to splancs

1 message · Adrian Baddeley

#
Marco Helbich <marco.helbich at gmx.at> wrote:

            
The output says that 'studyarea' is an object of class 
'SpatialPolygonsDataFrame', not an object of class 'owin'.

The SpatialPolygonsDataFrame class is part of the 'sp' package. Since 
the 'splancs' package depends on 'sp', I would have expected that 
splancs could cope with this object.

But if you *did* have an object of class 'owin' (from spatstat) that you 
wanted to convert to a SpatialPolygons object, you could just apply the 
following function to it:

owin2SP <- function(x) {
    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))  })
    whole <- Polygons(pieces, "1")
    SpatialPolygons(list(whole))
}


regards
Adrian Baddeley