[R-sig-Geo] function contourLines2SLDF

Michael Sumner mdsumner at utas.edu.au
Mon Mar 26 02:43:09 CEST 2007

Karl.Sommer at dpi.vic.gov.au wrote:
> Hello list,
> I used to be able to apply the function contourLines2SLDF for the 
> following operation on a SpatialGridDataFrame object. 
>> CL <- contourLines2SLDF(contourLines(as.image.SpatialGridDataFrame
> +       (as(ht.idw["var1.pred"],"SpatialGridDataFrame"))))
Gee I'd never noticed that function. I just checked and it's now in the 
"maptools" package.

BTW, here's an example I used just the other day, in case that's helpful 
to see what's going on. It depends on how your lines need to be 
organized whether this will match your case.

Cheers, Mike.

"d" is a SpatialGridDataFrame
"tab" is a SpatialPolygonsDataFrame (I'm looping over each polygon using 
"kk", this is one iteration)

## contour it
l <- contourLines(as.image.SpatialGridDataFrame(d), levels = seq(0, 
2000, by = 10))

## create empty list to hold lines
ll <- vector("list", length(l))
## loop over list of contours to create Lines object
for (i in 1:length(l)) ll[[i]] <- Lines(list(Line(cbind(l[[i]]$x, 
l[[i]]$y))), as.character(i))

## make those lines  Spatial, and fill out SpatialLinesDataFrame
ll <- SpatialLines(ll, CRS(proj4string(d)))
nm <- as.character(tab[kk,][["MAP_NAME"]])
num <-as.character(tab[kk,][["MAP_NO"]])
nn <- length(l)
ll <- SpatialLinesDataFrame(ll, data.frame(level = unlist(lapply(l, 
function(x) x$level)),
                                MAP_NAME = rep(nm, nn),
                                MAP_NO = rep(num, nn)))

More information about the R-sig-Geo mailing list