[R-sig-Geo] convert spatstat owin-object to splancs

Adrian Baddeley adrian at maths.uwa.edu.au
Thu Apr 9 07:05:16 CEST 2009


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

> I have the following problem: my dataset contains some events (points) and a 
> studyarea, I used some spatstat functions and now I want to use the 
> bandwidth estimation mse2d() in the splancs package. My problem is to 
> convert the spatstat owin-object (studyarea) to use it in the splancs 
> package. This is the structure of my studyarea:
>
> str(studyarea)
> Formal class 'SpatialPolygonsDataFrame' [package "sp"] with 5 slots
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



More information about the R-sig-Geo mailing list