knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "README-" )
R package starsExtra
provides several miscellaneous functions for working with stars
objects, mainly single-band rasters. Currently includes functions for:
CRAN version:
install.packages("starsExtra")
GitHub version:
install.packages("remotes") remotes::install_github("michaeldorman/starsExtra")
Once installed, the library can be loaded as follows.
library(starsExtra)
The complete documentation can be found at https://michaeldorman.github.io/starsExtra/.
The following code applied a 15*15 mean focal filter on a 533*627 stars
Digital Elevation Model (DEM):
data(carmel) carmel_mean15 = focal2( x = carmel, # Input 'stars' raster w = matrix(1, 15, 15), # Weights fun = "mean", # Aggregation function na.rm = TRUE, # 'NA' in neighborhood are removed mask = TRUE # Areas that were 'NA' in 'x' are masked from result )
data(carmel) start = Sys.time() carmel_mean15 = focal2(carmel, w = matrix(1, 15, 15), fun = "mean", na.rm = TRUE, mask = TRUE) end = Sys.time() d = end - start
The calculation takes: r format(d)
.
The original DEM and the filtered DEM can be combined and plotted with the following expressions:
r = c(carmel, carmel_mean15, along = 3) r = st_set_dimensions(r, 3, values = c("input", "15*15 mean filter")) plot(r, breaks = "equal", col = terrain.colors(10), key.pos = 4)
The following code section compares the calculation time of focal2
in the above example with raster::focal
(both using C/C++) and the reference method focal2r
(using R code only).
library(microbenchmark) library(starsExtra) library(raster) data(carmel) carmelr = as(carmel, "Raster") res = microbenchmark( focal2 = focal2(carmel, w = matrix(1, 15, 15), fun = "mean", na.rm = FALSE), focal = focal(carmelr, w = matrix(1, 15, 15), fun = mean, na.rm = FALSE), focal2r = focal2r(carmel, w = matrix(1, 15, 15), mean), times = 10 ) res ```r boxplot(res)
x = list.files(pattern = "^README-.*\\.png$") file.copy(x, "docs/", overwrite = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.