Title: | Fast Raster Summary and Manipulation |
---|---|
Description: | Fast alternatives to several relatively slow 'raster' package functions. For large rasters, the functions run from 5 to approximately 100 times faster than the 'raster' package functions they replace. The 'fasterize' package, on which one function in this package depends, includes an implementation of the scan line algorithm attributed to Wylie et al. (1967) <doi:10.1145/1465611.1465619>. |
Authors: | Joshua O'Brien |
Maintainer: | Joshua O'Brien <[email protected]> |
License: | GPL (>= 2) |
Version: | 0.3.2 |
Built: | 2024-10-31 03:49:23 UTC |
Source: | https://github.com/joshobrien/rasterdt |
Fast alternatives to several relatively slow 'raster' package functions. For large rasters, the functions run from 5 to approximately 100 times faster than the 'raster' package functions they replace. The 'fasterize' package, on which one function in this package depends, includes an implementation of the scan line algorithm attributed to Wylie et al. (1967) <doi:10.1145/1465611.1465619>.
The DESCRIPTION file:
Package: | rasterDT |
Type: | Package |
Title: | Fast Raster Summary and Manipulation |
Version: | 0.3.2 |
Date: | 2022-12-25 |
Author: | Joshua O'Brien |
Maintainer: | Joshua O'Brien <[email protected]> |
Description: | Fast alternatives to several relatively slow 'raster' package functions. For large rasters, the functions run from 5 to approximately 100 times faster than the 'raster' package functions they replace. The 'fasterize' package, on which one function in this package depends, includes an implementation of the scan line algorithm attributed to Wylie et al. (1967) <doi:10.1145/1465611.1465619>. |
License: | GPL (>= 2) |
URL: | https://github.com/JoshOBrien/rasterDT/, https://joshobrien.github.io/rasterDT/ |
BugReports: | https://github.com/JoshOBrien/rasterDT/issues/ |
Depends: | methods, raster (>= 3.6-3), data.table |
Imports: | fasterize, sf |
Suggests: | rasterVis |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.3 |
Repository: | https://joshobrien.r-universe.dev |
RemoteUrl: | https://github.com/joshobrien/rasterdt |
RemoteRef: | HEAD |
RemoteSha: | dfa6eb419232aa5617204f7f1f2ebdaed3a3ab28 |
Index of help topics:
cat_to_val Convert a Categorical Raster to a Value Raster crosstabDT Speedy Raster Cross-tabulation fasterizeDT Polygon Rasterization Using Numeric, Factor, or Character Fields freqDT Speedy Raster Value Frequency Tabulation rasterDT-package Fast Raster Summary and Manipulation subsDT Speedy Raster Value Substitution zonalDT Speedy Zonal Statistics
Fast alternatives to several relatively slow raster package functions. For large rasters, the functions run from 5 to approximately 100 times faster than the raster package functions they replace.
Joshua O'Brien
Maintainer: Joshua O'Brien <[email protected]>
Use a categorical raster's RAT to convert it to a continuous raster
cat_to_val(r, which = 2)
cat_to_val(r, which = 2)
r |
A categorical raster with a RAT (returned by
|
which |
An integer or character string giving the index or
name of the column in |
A continuous raster with each category level in r
replaced by its corresponding value.
Joshua O'Brien
r_cat <- raster(matrix(c(2, 2, 2, 1), ncol = 2)) levels(r_cat) <- data.frame(ID = c(1, 2), VAL1 = c(0.1, 200), VAL2 = c(33, 44)) ## Second column of RAT is used by default r_con1 <- cat_to_val(r_cat) as.matrix(r_con1) ## Use 'which=' argument for conversion to another RAT column r_con2 <- cat_to_val(r_cat, which = "VAL2") as.matrix(r_con2)
r_cat <- raster(matrix(c(2, 2, 2, 1), ncol = 2)) levels(r_cat) <- data.frame(ID = c(1, 2), VAL1 = c(0.1, 200), VAL2 = c(33, 44)) ## Second column of RAT is used by default r_con1 <- cat_to_val(r_cat) as.matrix(r_con1) ## Use 'which=' argument for conversion to another RAT column r_con2 <- cat_to_val(r_cat, which = "VAL2") as.matrix(r_con2)
A fast data.table
-based alternative to
raster::crosstab()
.
crosstabDT(x, y, digits = 0, long = FALSE, useNA = FALSE)
crosstabDT(x, y, digits = 0, long = FALSE, useNA = FALSE)
x |
A |
y |
If |
digits |
Integer. The number of digits for rounding the
values before cross-tabulation. Default is |
long |
Logical. If |
useNA |
Logical. Should the returned table or
|
Either a table or a data.table
recording the
frequency of each combination of raster values.
Joshua O'Brien
r <- raster(nc = 5, nr = 5) r[] <- runif(ncell(r)) * 2 s <- setValues(r, runif(ncell(r)) * 3) crosstabDT(r, s) rs <- r/s r[1:5] <- NA s[20:25] <- NA x <- stack(r, s, rs) crosstabDT(x, useNA = TRUE, long = TRUE)
r <- raster(nc = 5, nr = 5) r[] <- runif(ncell(r)) * 2 s <- setValues(r, runif(ncell(r)) * 3) crosstabDT(r, s) rs <- r/s r[1:5] <- NA s[20:25] <- NA x <- stack(r, s, rs) crosstabDT(x, useNA = TRUE, long = TRUE)
A front end for
fasterize::fasterize()
, fixing
several of its infelicities.
fasterizeDT( x, raster, field = NULL, fun = "last", background = NA_real_, by = NULL )
fasterizeDT( x, raster, field = NULL, fun = "last", background = NA_real_, by = NULL )
x |
Either an |
raster |
A |
field |
Character. The name of a column in |
fun |
Character. The name of a function by which to combine
overlapping polygons. Currently takes |
background |
Value to put in the cells that are not covered
by any of the features of |
by |
Character string giving the name of a column in |
Unlike other functions in this package, fasterizeDT()
does
not use data.table
to speed up its computations. Instead,
it is a wrapper for fasterize::fasterize()
, intended to
address several of that function's limitations.
Most importantly, fasterizeDT()
takes care to properly
handle rasterization operations in which either the template
RasterLayer
or the selected polygon feature field is a
factor. Specifically, it always returns a raster whose type
(numeric or factor) and levels (if a factor) match that of the
spatial polygon attribute indicated by its field
argument.
Second, when field
specifies an attribute of class
"character"
, fasterizeDT()
automatically converts it
to a factor and returns a factor raster. In this, it is unlike
both fasterize::fasterize()
and
raster::rasterize()
. Finally, unlike
fasterize::fasterize()
, fasterizeDT()
accepts as
inputs either sf::sf()
objects or
sp::SpatialPolygonsDataFrame
objects.
A raster of the same size, extent, resolution and
projection as the supplied raster template. Unlike
fasterize::fasterize()
,
fasterizeDT
returns a raster of the same type as the
data in the column of x
selected by the field
argument.
Joshua O'Brien
## Load example polygons and prepare a template raster if (require(raster)) { SPDF <- shapefile(system.file("external/lux.shp", package = "raster")) llratio <- 1/cos(pi * mean(coordinates(SPDF)[, 2])/180) rr <- raster(extent(SPDF), resolution = c(llratio * 0.01, 0.01), crs = proj4string(SPDF)) ## An integer-valued field produces a numeric raster rInt <- fasterizeDT(SPDF, rr, field = "ID_2") plot(rInt, col = colorRampPalette(blues9)(12)) ## A character-valued field returns a factor raster rFac <- fasterizeDT(SPDF, rr, field = "NAME_2") if (require(rasterVis)) { levelplot(rFac) } }
## Load example polygons and prepare a template raster if (require(raster)) { SPDF <- shapefile(system.file("external/lux.shp", package = "raster")) llratio <- 1/cos(pi * mean(coordinates(SPDF)[, 2])/180) rr <- raster(extent(SPDF), resolution = c(llratio * 0.01, 0.01), crs = proj4string(SPDF)) ## An integer-valued field produces a numeric raster rInt <- fasterizeDT(SPDF, rr, field = "ID_2") plot(rInt, col = colorRampPalette(blues9)(12)) ## A character-valued field returns a factor raster rFac <- fasterizeDT(SPDF, rr, field = "NAME_2") if (require(rasterVis)) { levelplot(rFac) } }
A fast data.table
-based alternative to
raster::freq()
.
freqDT(x, ...) ## S4 method for signature 'RasterLayer' freqDT(x, digits = 0, value = NULL, useNA = c("ifany", "no", "always"), ...) ## S4 method for signature 'RasterStackBrick' freqDT( x, digits = 0, value = NULL, useNA = c("ifany", "no", "always"), merge = FALSE, ... )
freqDT(x, ...) ## S4 method for signature 'RasterLayer' freqDT(x, digits = 0, value = NULL, useNA = c("ifany", "no", "always"), ...) ## S4 method for signature 'RasterStackBrick' freqDT( x, digits = 0, value = NULL, useNA = c("ifany", "no", "always"), merge = FALSE, ... )
x |
A |
... |
Additional arguments as for
|
digits |
Integer for rounding the cell values. Argument is
passed to |
value |
A single numeric, logical, or NA value. If supplied,
|
useNA |
Character (one of "no", "ifany", or "always"). What
to do with NA values? See |
merge |
Logical. If |
Joshua O'Brien
r <- raster(nrow = 18, ncol = 36) r[] <- runif(ncell(r)) r[1:5] <- NA r <- r * r * r * 5 freqDT(r) freqDT(r, value = 2) s <- stack(r, r*2, r*3) freqDT(s, merge = TRUE)
r <- raster(nrow = 18, ncol = 36) r[] <- runif(ncell(r)) r[1:5] <- NA r <- r * r * r * 5 freqDT(r) freqDT(r, value = 2) s <- stack(r, r*2, r*3) freqDT(s, merge = TRUE)
A fast data.table
-based alternative to
raster::subs()
.
subsDT(x, dict, by = 1, which = 2, subsWithNA = TRUE, filename = "", ...)
subsDT(x, dict, by = 1, which = 2, subsWithNA = TRUE, filename = "", ...)
x |
Categorical |
dict |
A |
by |
Vector of one or possibly more integers or character
strings giving the indices or names of the column in
|
which |
Vector of one or possibly more integers or character
strings giving the indices or names of the column in
|
subsWithNA |
Logical. If |
filename |
Character string giving (optional) file name to which the resultant raster should be written. |
... |
Additional arguments as for
|
A RasterLayer
object.
Joshua O'Brien
r <- raster(ncol = 10, nrow = 10) r[] <- round(runif(ncell(r)) * 10) df <- data.frame(id = 2:8, v = c(10, 10, 11, 11, 12:14)) x <- subsDT(r, df) x2 <- subsDT(r, df, subsWithNA = FALSE) df$v2 <- df$v * 10 x3 <- subsDT(r, df, which = 2:3) s <- stack(r, r*3) names(s) <- c("first", "second") x4 <- subsDT(s, df) x5 <- subsDT(s, df, which = 2:3)
r <- raster(ncol = 10, nrow = 10) r[] <- round(runif(ncell(r)) * 10) df <- data.frame(id = 2:8, v = c(10, 10, 11, 11, 12:14)) x <- subsDT(r, df) x2 <- subsDT(r, df, subsWithNA = FALSE) df$v2 <- df$v * 10 x3 <- subsDT(r, df, which = 2:3) s <- stack(r, r*3) names(s) <- c("first", "second") x4 <- subsDT(s, df) x5 <- subsDT(s, df, which = 2:3)
A fast data.table
-based alternative to
raster::zonal()
.
zonalDT(x, z, fun = sum, na.rm = TRUE)
zonalDT(x, z, fun = sum, na.rm = TRUE)
x |
A |
z |
A categorical |
fun |
A name or character string giving the function to be
applied to summarize the values by zone. It needs to return a
single (or at least a length-one vector). If |
na.rm |
Logical. If |
A data.table
with a summary value for each zone.
Joshua O'Brien
r <- raster(ncols = 10, nrows = 10) r[] <- runif(ncell(r)) * 1:ncell(r) z <- r z[] <- rep(1:5, each = 20) ## for big files, use a character value rather than a function zonalDT(r, z, "sum") ## for smaller files you can also provide a function zonalDT(r, z, mean) zonalDT(r, z, min) ## multiple layers zonalDT(stack(r, r*10), z, "sum")
r <- raster(ncols = 10, nrows = 10) r[] <- runif(ncell(r)) * 1:ncell(r) z <- r z[] <- rep(1:5, each = 20) ## for big files, use a character value rather than a function zonalDT(r, z, "sum") ## for smaller files you can also provide a function zonalDT(r, z, mean) zonalDT(r, z, min) ## multiple layers zonalDT(stack(r, r*10), z, "sum")