[R-sig-Geo] Change Lines'IDs within a SpatialLines object

Bastien Tran bastien.tran at gmail.com
Wed Dec 9 15:53:01 CET 2015


Dear all,

I intend to make "copies" of SpatialLines object meaning I would like
to get the same geometry but with different IDs for the Lines.

For instance using L1 from the SpatialLines example I would like to go from:

> str(L1)
Formal class 'SpatialLines' [package "sp"] with 3 slots
  ..@ lines      :List of 2
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1 2 3 3 2 2
  .. .. .. ..@ ID   : chr "a"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1 2 3 3 1.5 1
  .. .. .. ..@ ID   : chr "b"
  ..@ bbox       : num [1:2, 1:2] 1 1 3 3
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA

to

> str(L1)
Formal class 'SpatialLines' [package "sp"] with 3 slots
  ..@ lines      :List of 2
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1 2 3 3 2 2
  .. .. .. ..@ ID   : chr "c"
  .. ..$ :Formal class 'Lines' [package "sp"] with 2 slots
  .. .. .. ..@ Lines:List of 1
  .. .. .. .. ..$ :Formal class 'Line' [package "sp"] with 1 slot
  .. .. .. .. .. .. ..@ coords: num [1:3, 1:2] 1 2 3 3 1.5 1
  .. .. .. ..@ ID   : chr "d"
  ..@ bbox       : num [1:2, 1:2] 1 1 3 3
  .. ..- attr(*, "dimnames")=List of 2
  .. .. ..$ : chr [1:2] "x" "y"
  .. .. ..$ : chr [1:2] "min" "max"
  ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot
  .. .. ..@ projargs: chr NA


Is there a way to do so without rebuilding the SpatialLine object with
updated Lines objects (same Lines new IDs)? I assume it is possible to
update each ID slot directly in L1 somehow.

I expected something like

sapply(slot(L1, "lines"), function(x) assign(slot(x, "ID"),
paste0("copyof",slot(x, "ID")))

to do so but I am certainly missing something about assignments in S4 objects.

Thank you for any help.

Best regards,

Bastien Tran



More information about the R-sig-Geo mailing list