Extract points from SpatialLines objects along with the corresponding Lines' ID
On 04/12/15 16:49, Bastien Tran wrote:
Dear all, I am trying to extract the points that compose a SpatialLines object along with the Line's ID (provided when Lines() is called). Hoping it makes my question clearer this is what I am currently able to get (using the SpatialLines example):
library(sp)
l1 = cbind(c(1,2,3),c(3,2,2))
Sl1 = Line(l1)
S1 = Lines(list(Sl1), ID=c("a"))
l2 = cbind(c(1,2,3),c(3,1.5,1))
Sl2 = Line(l2)
S2 = Lines(list(Sl2), ID="b")
L1 = SpatialLines(list(S1,S2))
Try:
as(as(L1, "SpatialPointsDataFrame"), "data.frame")
Lines.NR Lines.ID Line.NR coords.x1 coords.x2 1 1 a 1 1 3.0 2 1 a 1 2 2.0 3 1 a 1 3 2.0 4 2 b 1 1 3.0 5 2 b 1 2 1.5 6 2 b 1 3 1.0 or alternatively:
as(L1, "SpatialMultiPoints")@coords
$a
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 2
$b
[,1] [,2]
[1,] 1 3.0
[2,] 2 1.5
[3,] 3 1.0
sapply(slot(L1, "lines"), function(x) lapply(slot(x, "Lines"),
function(y) slot(y, "coords")))
[[1]]
[,1] [,2]
[1,] 1 3
[2,] 2 2
[3,] 3 2
[[2]]
[,1] [,2]
[1,] 1 3.0
[2,] 2 1.5
[3,] 3 1.0
But I would rather like to obtain something like this:
[[1]]
[,1] [,2] [,3]
[1,] "1" "3" "a"
[2,] "2" "2" "a"
[3,] "3" "2" "a"
[[2]]
[,1] [,2] [,3]
[1,] "1" "3" "b"
[2,] "2" "1.5" "b"
[3,] "3" "1" "b"
If possible I would like to avoid type conversions and get a data.frame
directly though I could indeed recreate one with these matrices, without
information loss I assume.
Thank you in advance for any tips.
Best regards,
Bastien
[[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
Edzer Pebesma Institute for Geoinformatics (ifgi), University of M?nster, Heisenbergstra?e 2, 48149 M?nster, Germany; +49 251 83 33081 Journal of Statistical Software: http://www.jstatsoft.org/ Computers & Geosciences: http://elsevier.com/locate/cageo/ Spatial Statistics Society http://www.spatialstatistics.info -------------- next part -------------- A non-text attachment was scrubbed... Name: signature.asc Type: application/pgp-signature Size: 490 bytes Desc: OpenPGP digital signature URL: <https://stat.ethz.ch/pipermail/r-sig-geo/attachments/20151205/d0686df4/attachment.bin>