spex (version 0.7.1)

polygonize: Create a polygon layer from a raster.

Description

This method uses the quadmesh to generate the coordinates, and creates a simple features layer. It's faster by turning off the checking done in the simple features package, but it's also faster than raster because it uses a dense mesh to generate the coordinates.

Usage

# S3 method for RasterLayer
polygonize(x, na.rm = TRUE, ...)

polygonize(x, ...)

qm_rasterToPolygons(x, na.rm = TRUE, ...)

# S3 method for RasterStack polygonize(x, na.rm = TRUE, ...)

# S3 method for RasterBrick polygonize(x, na.rm = TRUE, ...)

qm_rasterToPolygons_sp(x, na.rm = TRUE, ...)

Arguments

x

raster, brick or stack

na.rm

defaults to TRUE and will polygonize all the cells that are non-NA in any layer, set to FALSE to not remove any cells

...

arguments passed to methods, currently unused

Value

simple features POLYGON layer, or SpatialPolygonsDataFrame

Warning

Please don't try this on large rasters (> ~1e5 cells), use quadmesh itself for efficient vector based use of a raster's coordinates. It will work reasonably on largish grids, but you won't want to try plotting them or perform operations on them, simple features is incredibly wasteful for objects like this.

Details

If na.rm is TRUE (the default) only cells that are not-NA across all layers are created. An exception to this is the empty raster raster::hasValues(x) is FALSE and all the cells will be turned into polygons - since this is what the whole scene is really for, easily creating polygons from a grid.

Examples

Run this code
# NOT RUN {
#library(raadtools)
library(raster)
r <- raster(volcano)
r[sample(ncell(r), 3000)] <- NA
b <- brick(r, r*1.5)
psf <- qm_rasterToPolygons(r, na.rm = TRUE)
#psp <- qm_rasterToPolygons_sp(r)
#pspr <- rasterToPolygons(r)
#library(rbenchmark)
#benchmark(qm_rasterToPolygons(r), qm_rasterToPolygons_sp(r), rasterToPolygons(r), replications = 2)
#                        test replications elapsed relative user.self sys.self user.child sys.child
# 1    qm_rasterToPolygons(r)            2   0.476    1.000     0.476    0.000          0         0
# 2 qm_rasterToPolygons_sp(r)            2   4.012    8.429     3.964    0.048          0         0
# 3       rasterToPolygons(r)            2   2.274    4.777     2.268    0.008          0         0
# }

Run the code above in your browser using DataLab