[R-sig-Geo] contourLines() to SpatialLineDataFrame

Roger Bivand Roger.Bivand at nhh.no
Wed Aug 12 20:11:48 CEST 2009


On Tue, 4 Aug 2009, Ervan Rutishauser wrote:

> Dear all ,
>
> I have a problem in converting some interpolated contourLines into SLDF, 
> using the function ContourLines2SLDF. I tried with the example given in 
> "Applied Spatial Data Analysis wit R", but for similar input data, the 
> function returns a strange SLDF with my data... I am then unable to 
> check for equality between first and last coordinates (suitable to 
> create polygons).. Does someone know what's wrong with my code??

You have not examined the documentation of the function carefully enough, 
because it says: "the data frame is just the contour levels, with one 
Lines object made up of at least one Line object per level". Since you 
have only given it one level, you get one Lines object containing 28 Line 
objects, all at the same level value. To split them out, you need to make 
28 Lines objects with unique IDs, possibly in this way:

test1 <- SpatialLines(lapply(seq(along=AA), function(i) {
   Lines(
     list(
       Line(cbind(AA[[i]]$x, AA[[i]]$y))
     ),
   ID=as.character(i))
}))

for a SpatialLines object, or for SpatialPolygons:

test2 <- SpatialPolygons(lapply(seq(along=AA), function(i) {
   xy <- cbind(AA[[i]]$x, AA[[i]]$y)
   if (!identical(xy[1,], xy[nrow(xy),])) xy <- rbind(xy, xy[1,])
   Polygons(list(Polygon(xy)), ID=as.character(i))
}))

Hope this helps,

Roger

> Many thanks for your help!
>
> library(maptools)
> load("XXX/interpolated_den.Rdata")  ## the data is called DDD...
> AA<-contourLines(DDD,levels=c(2))    ## I keep only the isolines of level 2
> test<-ContourLines2SLDF(AA)                  ## library(maptools)   transforme les isolignes en SpatialLinesDataFrame
>
> lns<-slot(test,"lines")           # as in the ASDAR p. 48
> table(sapply(lns,function(x) length(slot(x,"Lines"))))
>
> ## return 28/1 rather that 1/28 -> lns is a list 1 object composed of 28 lines, rather than being a list of 28 object of 1 lines....
> ## thus I can't do the next step...
>
> table(sapply(lignes, function(x) {
> coord<-slot(slot(x,"Lines")[[1]],"coords")
> identical(coord[1,],coord[nrow(coord),])}))
>
>
> ___________________________________________________________
>
> Ervan Rutishauser
> PhD student
> CIRAD - UMR AMAP
> BP 701
> 97387 Kourou Cedex
> tel. 00594.594.32.92.96
>
>
>

-- 
Roger Bivand
Economic Geography Section, Department of Economics, Norwegian School of
Economics and Business Administration, Helleveien 30, N-5045 Bergen,
Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43
e-mail: Roger.Bivand at nhh.no



More information about the R-sig-Geo mailing list