[R-sig-Geo] Extract points from SpatialLines objects along with the corresponding Lines' ID

Bastien Tran bastien.tran at gmail.com
Fri Dec 4 16:49:33 CET 2015


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



More information about the R-sig-Geo mailing list