Quiero crear dos polígonos.
Respuesta
¿Demasiados anuncios?He aquí un ejemplo.
library(raster)
# example data
x <- raster(system.file("external/test.grd", package="raster"))
Para obtener la extensión rectangular
e <- extent(x)
# coerce to a SpatialPolygons object
p <- as(e, 'SpatialPolygons')
Para obtener un polígono que rodee celdas que no sean NA
# make all values the same. Either do
r <- x > -Inf
# or alternatively
# r <- reclassify(x, cbind(-Inf, Inf, 1))
# convert to polygons (you need to have package 'rgeos' installed for this to work)
pp <- rasterToPolygons(r, dissolve=TRUE)
# look at the results
plot(x)
plot(p, lwd=5, border='red', add=TRUE)
plot(pp, lwd=3, border='blue', add=TRUE)
Cinco años después: Hoy en día usaría terra
que lo hace mucho más rápido.
library(terra)
z <- rast(system.file("external/test.grd", package="raster"))
pe <- as.polygons(ext(z))
pr <- as.polygons(z > -Inf)
plot(z)
plot(pe, lwd=5, border='red', add=TRUE)
plot(pr, lwd=3, border='blue', add=TRUE)