[R-sig-Geo] change in sp::Lines() breaks example in my package

Edzer Pebesma edzer.pebesma at uni-muenster.de
Thu Jul 29 00:22:02 CEST 2010


Thanks for the suggestion.

There was an example in the sp vignette. The documentation didn't
mention that ID should be a character vector of length one. I think
we've implicitly assumed users would understand this, as even the sp
code before 0.9-66 allowed longer vectors:

> l1 = cbind(c(1, 2, 3), c(3, 2, 2))
> Sl1 = Line(l1)
> S1 = Lines(list(Sl1), ID = c("a","b"))
> S1
An object of class "Lines"
Slot "Lines":
[[1]]
An object of class "Line"
Slot "coords":
     [,1] [,2]
[1,]    1    3
[2,]    2    2
[3,]    3    2

Slot "ID":
[1] "a" "b"

The idea of having IDs however is that when Lines get attributes, as in
SpatialLinesDataFrames, a match of lines and attribute table row.names
is done based on IDs. This rules out the possibility of IDs having
length larger than one.

On 07/28/2010 11:22 PM, Michael Friendly wrote:
> Thanks, Edzer
> 
> What was not clear from either the documentation or the error message
> was what you just
> said:  "each Lines *colllection* only has a single ID" . The .Rd just says
> 
> |ID| a unique character identifier
> 
> but 'unique' in what context?  help(Lines) gives no examples, so there
> was no way for me to figure this out.
> It might help to revise the documentation, and you are welcome to use
> any part of this example if
> it helps.  At a minimum, if I understand correctly, I suggest something
> like
> 
> |ID| a single character identifier, unique among other SpatialLines
> objects,
>     applying to the collection of lines in \code{slinelist}
> 
> Your 'short way out' works for me, and I have no need to index
> particular streets.
> 
> best,
> -Michael
> 
> Edzer Pebesma wrote:
>> Michael, indeed a check was added recently to make sure that each Lines
>> colllection only has a single ID; I don't believe the array of IDs you
>> passed it had the effect you desired.
>>
>> The short way out would be to have a simple
>>
>> Lsl1 <- Lines(Ll1,"Streets")
>>
>> but the nicer way assigns street IDs to individual streets:
>>
>> i = 0
>> Lsl1 <- lapply(slist, function(x) {
>>     i <<- i+1
>>     Lines(Line(x), as.character(i))
>> })
>> rm(i)
>> Snow.streets.sp <- SpatialLines(Lsl1)
>>
>> which now lets you index Snow.streets.sp, as in
>>
>> Snow.streets.sp[100] # or
>> Snow.streets.sp["100"]
>>
>> BTW it's the first time I advice someone to use <<- and I'd be happy for
>> a  suggestion to get rid of it!
>>
>> On 07/27/2010 04:22 PM, Michael Friendly wrote:
>>  
>>> [Running R.2.11.1, Win XP]
>>>
>>> I just updated sp from sp_0.9-65 to sp_0.9-66 after receiving a report
>>> from the CRAN team that my HistData
>>> package now generates errors in at least one documentation example.  It
>>> occurs in example(Snow), which
>>> worked successfully under the previous versions of sp.  I don't
>>> understand the nature of the error, or how to
>>> correct it.  Can someone point me in a useful direction?
>>>
>>> Here is the complete portion of the example (to draw Snow's cholera map
>>> of London), suitable for cut/paste to R:
>>>
>>> #install.packages("HistData")    # if necessary
>>> library(HistData)
>>> library(sp)
>>>
>>> # streets
>>> slist <-
>>> split(Snow.streets[,c("x","y")],as.factor(Snow.streets[,"street"]))
>>> Ll1 <- lapply(slist,Line)
>>> Lsl1 <- Lines(Ll1,paste("Street",1:length(Ll1)))
>>> Snow.streets.sp <- SpatialLines(list(Lsl1))
>>> plot(Snow.streets.sp, col="gray")
>>> title(main="Snow's Cholera Map of London")
>>>
>>> # deaths
>>> Snow.deaths.sp = SpatialPoints(Snow.deaths[,c("x","y")])
>>> plot(Snow.deaths.sp, add=TRUE, col ='red', pch=15, cex=0.6)
>>>
>>> # pumps
>>> spp <- SpatialPoints(Snow.pumps[,c("x","y")])
>>> Snow.pumps.sp <- SpatialPointsDataFrame(spp,Snow.pumps[,c("x","y")])
>>> plot(Snow.pumps.sp, add=TRUE, col='blue', pch=17, cex=1.5)
>>> text(Snow.pumps[,c("x","y")], labels=Snow.pumps$label, pos=1, cex=0.8)
>>>
>>> Here is the transcript showing the error with Lines:
>>>
>>>    
>>>> # streets
>>>> slist <-
>>>>       
>>> split(Snow.streets[,c("x","y")],as.factor(Snow.streets[,"street"]))
>>>    
>>>> Ll1 <- lapply(slist,Line)
>>>> Lsl1 <- Lines(Ll1,paste("Street",1:length(Ll1)))
>>>>       
>>> Error in Lines(Ll1, paste("Street", 1:length(Ll1))) : Single ID required
>>>    
>>>> Snow.streets.sp <- SpatialLines(list(Lsl1))
>>>>       
>>> Error in is.vector(X) : object 'Lsl1' not found
>>>
>>> Some tests:
>>>    
>>>> length(Ll1)
>>>>       
>>> [1] 528
>>>    
>>>> length(unique(paste("Street",1:length(Ll1))))
>>>>       
>>> [1] 528
>>>     Below is my sessionInfo():
>>>
>>>    
>>>> sessionInfo()
>>>>       
>>> R version 2.11.1 (2010-05-31)
>>> i386-pc-mingw32
>>>
>>> locale:
>>> [1] LC_COLLATE=English_United States.1252
>>> [2] LC_CTYPE=English_United States.1252  [3] LC_MONETARY=English_United
>>> States.1252
>>> [4] LC_NUMERIC=C                         [5] LC_TIME=English_United
>>> States.1252  attached base packages:
>>> [1] stats     graphics  grDevices utils     datasets  methods  
>>> base   other attached packages:
>>> [1] sp_0.9-66         KernSmooth_2.23-3 HistData_0.6-8 loaded via a
>>> namespace (and not attached):
>>> [1] grid_2.11.1    lattice_0.18-8 tools_2.11.1 >
>>>
>>>     
>>
>>   
> 
> 

-- 
Edzer Pebesma
Institute for Geoinformatics (ifgi), University of Münster
Weseler Straße 253, 48151 Münster, Germany. Phone: +49 251
8333081, Fax: +49 251 8339763  http://ifgi.uni-muenster.de
http://www.52north.org/geostatistics      e.pebesma at wwu.de



More information about the R-sig-Geo mailing list