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

Roger Bivand Roger.Bivand at nhh.no
Wed Dec 9 17:40:33 CET 2015


On Wed, 9 Dec 2015, Bastien Tran wrote:

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

Please use row.names(), so here:

row.names(L1) <- c("c", "d")

If L1 was a SpatialLinesDataFrame, the row.names of the data slot would 
also be updated.

Roger

>
> 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
>
> _______________________________________________
> 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, Norwegian School of Economics,
Helleveien 30, N-5045 Bergen, Norway.
voice: +47 55 95 93 55; fax +47 55 95 91 00
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list