Skip to content
Prev 5809 / 29559 Next

contour data

On Tue, 26 May 2009, John Callahan wrote:

            
Just roughly:

download.file("http://geomorphometry.org/data/DEM25m.zip",
   destfile="DEM25m.zip")
fname <- zip.file.extract(file="DEM25m.asc", zipname="DEM25m.zip")
file.copy(fname, "DEM25m.asc")
# to give some data to play with
library(rgdal)
dem <- readGDAL("DEM25m.asc")
# GDAL has many formats, IMG isn't very descriptive, try and see which 
# driver suits for reading to a SpatialGridDataFrame - I'm assuming one
# band only, using the first and only band next
im <- as.image.SpatialGridDataFrame(dem)
cl <- contourLines(im)
# contourLines() takes the same interval arguments as contour()
library(maptools)
SLDF <- ContourLines2SLDF(cl)
# convert to a SpatialLinesDataFrame with the contour labels as 
# attributes and export
writeOGR(SLDF, ".", "my_contours", driver="ESRI Shapefile")
mc <- readOGR(".", "my_contours")
summary(dem)
summary(mc)
image(dem, col=gray.colors(20))
plot(mc, col=terrain.colors(8), add=TRUE)

The colours in the last line are rather sleight of hand, but for 
demonstration they work this time.

Hope this helps,

Roger