An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20111115/439dcc8d/attachment.pl>
Coercing spatstat::psp to sp:SpatialLines
5 messages · Roger Bivand, Mathieu Rajerison, Rolf Turner
On Tue, 15 Nov 2011, Mathieu Rajerison wrote:
Hi, I've generated random segment patterns with spatstat::rpoisline so as to plot differences of profile lines between two different digital elevation models. Now, I want to use raster::extract function to get the raster values on both rasters for each segment but for this, I have to coerce a psp to a SpatialLines object but I don't know how to do the coercion. (SpatialLines to psp is OK). as(psp, "SpatialLines") doesn't seem to work.
The coercion functions in maptools, contributed by Adrian Baddeley, go from SpatialLines to psp, not the reverse. You need to create the set of coercion methods to go the other way - contributions welcome! Roger
Any help would be appreciated, Mathieu [[alternative HTML version deleted]]
_______________________________________________ R-sig-Geo mailing list R-sig-Geo at r-project.org https://stat.ethz.ch/mailman/listinfo/r-sig-geo
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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20111116/4b82bd8f/attachment.pl>
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20111116/baf83350/attachment.pl>
On 16/11/11 20:49, Mathieu Rajerison wrote:
Finally, I got a SpatialLines from a psp object (here named aix.psp)
Here's how I got it:
coords<- aix.psp$ends
nc<- nrow(coords)
out<- vector(mode="list", length=nc)
for (i in seq(along=out)) {
lLine<-Line(cbind(c(coords[i,1], coords[i,3]), c(coords[i,2],
coords[i,4])))
out[[i]]<- Lines(list(lLine), ID=i)
}
aix.lines<- SpatialLines(out)
I don't find my solution very elegant. I wondered if a simpler code could
be written using lapply function.
For that purpose, I did:
liste<-lapply(coords, function(x) Line(cbind(c(x[1], x[3]), c(x[2], x[4]))))
but I don't know how to create a list of this one, with an incrementating
ID, which is required for constructing a Lines object as
Lines(list(LineList), ID=i)
It's more a general R problem than an R-sig-geo one but if anyone here has
the issue, I would be thankful.
I hacked your code a bit and came up with the following:
# Generic function for S3 method:
as.SpatialLines <- function(from) {
UseMethod("as.SpatialLines")
}
# Method for class "psp".
as.SpatialLines.psp <- function (from) {
foo <- function(i,x){
y <- matrix(unlist(x[i,]),2,2,byrow=TRUE)
Lines(list(Line(y)),ID=i)
}
coords <- from$ends
SpatialLines(lapply(1:nrow(coords),foo,x=coords))
}
After sourcing in these two functions, try:
check <- as.SpatialLines(aix.psp)
and then compare:
all.equal(check,aix.lines) # Should get TRUE!!!
I *thought* that after having defined as.SpatialLines.psp() as above I would
be able to do:
check <- as(aix.psp,"SpatialLines")
but that gave me the error
Error in as(aix.psp, "SpatialLines") :
no method or default for coercing "psp" to "SpatialLines"
even though I had just written such a method! There must be some extra
S4 classes and methods magic required here, that I don't know about.
Roger?
(I never touch S4 classes and methods with a sterilised barge-pole!)
cheers,
Rolf