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

Roger Bivand Roger.Bivand at nhh.no
Thu Nov 17 10:05:52 CET 2011


Thanks to contributions by Adrian Baddeley, Rolf Turner and Mathieu 
Rajerison, an S4 coercion method as(<psp-object>, "SpatialLines") and 
function as.SpatialLines.psp() are committed to R-forge SVN in the 
maptools project, and will be included in the next release of the 
maptools package.

Roger

On Thu, 17 Nov 2011, Adrian.Baddeley at csiro.au wrote:

>
> 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
>

-- 
Roger Bivand
Department of Economics, NHH Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list