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

Edzer Pebesma edzer.pebesma at uni-muenster.de
Wed Jul 28 21:31:21 CEST 2010


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