Learn R Programming

inlmisc (version 0.4.3)

Grid2Polygons: Convert Spatial Grids to Polygons

Description

This function converts gridded spatial data to spatial polygons. Image files created with spatial polygons are reduced in size, can easily be transformed from one coordinate reference system to another, and result in much "cleaner" images when plotted.

Usage

Grid2Polygons(grd, zcol = 1, level = FALSE, at = NULL, cuts = 20,
  pretty = FALSE, xlim = NULL, ylim = NULL, zlim = NULL,
  ply = NULL)

Arguments

grd

'SpatialGridDataFrame', 'SpatialPixelsDataFrame', or 'Raster*'. Spatial grid

zcol

'character' or 'integer'. Layer to extract from a multi-layer spatial grid.

level

'logical'. If true, a set of levels is used to partition the range of attribute values, its default is false.

at

'numeric'. Vector giving breakpoints along the range of attribute values.

cuts

'integer'. Number of levels the range of attribute values would be divided into.

pretty

'logical'. Whether to use pretty cut locations.

xlim

'numeric'. Vector of length 2 giving left and right limits of the spatial grid, data outside these limits is excluded.

ylim

'numeric'. Vector of length 2 giving lower and upper limits of the spatial grid, data outside these limits is excluded.

zlim

'numeric'. Vector of length 2 giving minimum and maximum limits of the attribute variable, data outside these limits is excluded.

ply

'SpatialPolygons', or 'SpatialGridDataFrame'. Cropping polygon

Value

Returns an object of 'SpatialPolygonsDataFrame'. The objects data slot is a data frame, number of rows equal to the number of Polygons objects and a single column containing attribute values. If level is true, attribute values are set equal to the midpoint between breakpoints. The status of the polygon as a hole or an island is taken from the ring direction, with clockwise meaning island, and counter-clockwise meaning hole.

References

A general explanation of the algorithm provided here; inspiration provided here.

Examples

Run this code
# NOT RUN {
# Example 1
z <- c(1.1,  1.5,  4.2,  4.1,  4.3,  4.7,
       1.2,  1.4,  4.8,  4.8,   NA,  4.1,
       1.7,  4.2,  1.4,  4.8,  4.0,  4.4,
       1.1,  1.3,  1.2,  4.8,  1.6,   NA,
       3.3,  2.9,   NA,  4.1,  1.0,  4.0)
m <- 5
n <- 6
x <- rep(0:n, m + 1)
y <- rep(0:m, each = n + 1)
xc <- c(rep(seq(0.5, n - 0.5, by = 1), m))
yc <- rep(rev(seq(0.5, m - 0.5, by = 1)), each = n)
grd <- data.frame(z = z, xc = xc, yc = yc)
sp::coordinates(grd) <- ~ xc + yc
sp::gridded(grd) <- TRUE
grd <- as(grd, "SpatialGridDataFrame")
image(grd, col = gray.colors(30), axes = TRUE)
grid(col = "black", lty = 1)
points(x = x, y = y, pch = 16)
text(cbind(xc, yc), labels = z)
text(cbind(x = x + 0.1, y = rev(y + 0.1)),
     labels = 1:((m + 1) * (n + 1)), cex = 0.6)
at <- 1:ceiling(max(z, na.rm = TRUE))
plys <- Grid2Polygons(grd, level = TRUE, at = at)
cols <- GetTolColors(length(plys), scheme = "bright", alpha = 0.3)
sp::plot(plys, add = TRUE, col = cols)
zz <- plys[[1]]
legend("top", legend = zz, fill = cols, bty = "n", xpd = TRUE,
       inset = c(0, -0.1), ncol = length(plys))

v1 <- rbind(c( 1.2, 0.5), c(5.8, 1.7), c( 2.5, 5.1), c( 1.2, 0.5))
v2 <- rbind(c( 2.5, 2.5), c(3.4, 1.8), c( 3.7, 3.1), c( 2.5, 2.5))
v3 <- rbind(c(-0.3, 3.3), c(1.7, 5.1), c(-1.0, 7.0), c(-0.3, 3.3))
p1 <- sp::Polygon(v1, hole = FALSE)
p2 <- sp::Polygon(v2, hole = TRUE)
p3 <- sp::Polygon(v3, hole = FALSE)
p <- sp::SpatialPolygons(list(sp::Polygons(list(p1, p2, p3), 1)))
plys <- Grid2Polygons(grd, level = TRUE, at = at, ply = p)
cols <- GetTolColors(length(zz), scheme = "bright", alpha = 0.6)
cols <- cols[zz %in% plys[[1]]]
sp::plot(plys, col = cols, add = TRUE)
text(cbind(xc, yc), labels = z)

# Example 2
data(meuse.grid, package = "sp")
sp::coordinates(meuse.grid) <- ~ x + y
sp::gridded(meuse.grid) <- TRUE
meuse.grid <- as(meuse.grid, "SpatialGridDataFrame")
meuse.plys <- Grid2Polygons(meuse.grid, "dist", level = FALSE)
op <- par(mfrow = c(1, 2), oma = rep(0, 4), mar = rep(0, 4))
sp::plot(meuse.plys, col = heat.colors(length(meuse.plys)))
title("level = FALSE", line = -7)
meuse.plys.lev <- Grid2Polygons(meuse.grid, "dist", level = TRUE)
sp::plot(meuse.plys.lev, col = heat.colors(length(meuse.plys.lev)))
title("level = TRUE", line = -7)
par(op)

# Example 3
m <- datasets::volcano
m <- m[nrow(m):1, ncol(m):1]
x <- seq(from = 2667405, length.out = ncol(m), by = 10)
y <- seq(from = 6478705, length.out = nrow(m), by = 10)
r <- raster::raster(m, xmn = min(x), xmx = max(x), ymn = min(y), ymx = max(y),
                    crs = "+init=epsg:27200")
plys <- Grid2Polygons(r, level = TRUE)
sp::plot(plys, col = terrain.colors(length(plys)), border = "#515151")

# }

Run the code above in your browser using DataLab