Skip to content

Instantly share code, notes, and snippets.

@pepijn-devries
Last active August 29, 2015 14:26
Show Gist options
  • Save pepijn-devries/e88c5a30d3cccc629fc3 to your computer and use it in GitHub Desktop.
Save pepijn-devries/e88c5a30d3cccc629fc3 to your computer and use it in GitHub Desktop.
ContourLines to SpatialPolygons
#required packages:
require(raster)
require(maptools)
require(Grid2Polygons)
#define breaks for the contour lines:
breaks <- seq(80, 200, length.out = 15)
#first convert the volcano matrix to a raster
poly_volcano <- raster(volcano)
#convert the raster to contour lines
poly_volcano <- rasterToContour(poly_volcano, levels = breaks)
#convert the SpatialLines into a PolySet
poly_volcano <- SpatialLines2PolySet(poly_volcano)
#convert the Polyset into SpatialPolygons
poly_volcano <- PolySet2SpatialPolygons(poly_volcano)
#plot the resulting SpatialPolygons
png("volcano01.png", 200, 200)
par(oma = c(0,0,0,0), mar = c(0,0,0,0))
plot(poly_volcano, col = terrain.colors(length(poly_volcano)))
dev.off()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment