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

Roger Bivand Roger.Bivand at nhh.no
Thu Dec 10 11:36:33 CET 2015


On Thu, 10 Dec 2015, Bastien Tran wrote:

> Thank you very much Roger, it is really helpful.
>
> I had no idea SpatialLines (or SpatialPolygons) objects could be
> manipulated this way. If I may ask, to which extent these objects can
> be treated as data frames (I understand they are not?)? I may have
> missed it but I haven't seen such thing in the documentation.

Yes, in principle Spatial*DataFrames should "behave like" data.frame 
objects, although not do for all methods that work for data.frame objects. 
Crucially, they can almost always be used in the data= argument to 
model fitting functions.

Roger

>
> Incidentally I think your answer made me understand what
> SpatialLinesDataFrame are (I used to think it was a different way to
> store the shape's geometry but it is more about linking
> attributes/variables to shapes ID's if I am correct).  It will serve
> my purpose quite aptly.
>
> Thanks a lot again, I already had more answers than I asked for.
>
> Best regards,
> Bastien
>
> 2015-12-09 17:40 GMT+01:00 Roger Bivand <Roger.Bivand at nhh.no>:
>> 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
>>
>

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