polygonize: Create a polygon layer from a raster.

Description Usage Arguments Details Value Warning Examples

View source: R/qm_rasterToPolygons.R

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
## S3 method for class 'RasterLayer'
polygonize(x, na.rm = TRUE, ...)

polygonize(x, ...)

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

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

## S3 method for class '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

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.

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.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
#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

Example output

Loading required package: sp

spex documentation built on July 2, 2020, 2:22 a.m.