[R-sig-Geo] Coercing spatstat::psp to sp:SpatialLines

Adrian.Baddeley at csiro.au Adrian.Baddeley at csiro.au
Thu Nov 17 04:32:28 CET 2011


Mathieu Rajerison <mathieu.rajerison at gmail.com> asks how to convert an object of class 'psp' in spatstat to an object of class SpatialLines in sp. 

Here is a function to do that. (It requires 'spatstat'.)


as.SpatialLines.psp <- local({

     ends2line <- function(x) Line(matrix(x, ncol=2, byrow=TRUE))
     munch <- function(z) { Lines(ends2line(as.numeric(z[1:4])), ID=z[5]) }
  
     convert <- function(x) {
        ends <- as.data.frame(x)[,1:4]
        ends[,5] <- row.names(ends)
        y <- apply(ends, 1, munch)
        SpatialLines(y)
     }
     convert
})


====

Prof Adrian Baddeley (UWA/CSIRO)
CSIRO Mathematics, Informatics & Statistics
Leeuwin Centre, 65 Brockway Road, Floreat WA 6014, Australia
Tel: 08 9333 6177 | Fax: 08 9333 6121 | Mob: 0410 447 821
Skype: adrian.baddeley



More information about the R-sig-Geo mailing list