Rust-Boosted Linear and Spatial Filtering in R.
Currently, the package supports only the Whittaker-Eilers smoother as it is implemented in the whittaker-eilers crate. Based on that filter, a smoothing approach for spatial geometries (only single-part LINESTRING for now) is proposed.
Install from CRAN:
install.packages("filtrs")Or install the development version from GitHub:
# install.packages("remotes")
remotes::install_github("atsyplenkov/filtrs")For equally-spaced data, one can use the fil_wt function as an API to whittaker_eilers::WhittakerSmoother. It has two controls, lambda and order, and interpolates missing values by default. An extremely nice description of the smoother is written by Andrew Bowell.
library(filtrs)
## basic example code
data("airquality")
airquality$Ozone_smooth <-
fil_wt(as.double(airquality$Ozone), 10, 2)
plot(airquality$Ozone, type = "l", xlab = "Days", ylab = "Ozone")
lines(airquality$Ozone_smooth, col = "red", lwd = 2)Spatial data may not be equally spaced; it often has more nodes on the bends of lines and fewer on straight segments. To address this challenge, the distance between nodes—calculated using either Cartesian or Haversine methods, depending on the Coordinate Reference System (CRS)—is used as a positions vector in the background. The function fil_wt_sf is designed to smooth a single LINESTRING, proving particularly beneficial for processing high-frequency data, such as that obtained from satellite imagery.
library(sf)
#> Linking to GEOS 3.12.1, GDAL 3.8.4, PROJ 9.4.0; sf_use_s2() is TRUE
library(smoothr)
#> Error in `library()`:
#> ! there is no package called 'smoothr'
file_path <- system.file("exdata/examples.gpkg", package = "filtrs")
lines <-
sf::st_read(file_path, layer = "gswe", quiet = TRUE) |>
# Increase nodes count for smoother result
smoothr::smooth("densify", max_distance = 10)
#> Error in `loadNamespace()`:
#> ! there is no package called 'smoothr'
lines_wt <-
fil_wt_sf(lines, lamda = 10^-7, order = 3)
#> Error in `UseMethod()`:
#> ! no applicable method for 'st_coordinates' applied to an object of class "function"
lines_smoothr <-
smoothr::smooth(lines, method = "ksmooth",
smoothness = 21)
#> Error in `loadNamespace()`:
#> ! there is no package called 'smoothr'Plot’s code
par(
mar = c(0.5, 0.5, 0.2, 0.2),
mfrow = c(1, 2),
oma = c(0, 0, 0.2, 0.2)
)
plot(
sf::st_geometry(lines),
col = "grey30",
lwd = 3.5
)
#> Error in `UseMethod()`:
#> ! no applicable method for 'st_geometry' applied to an object of class "function"
plot(
sf::st_geometry(lines_wt),
col = 'firebrick3',
lwd = 2,
add = TRUE
)
#> Error:
#> ! object 'lines_wt' not found
# Add the legend
legend(
"bottomleft",
legend = c("Original", "{filtrs}", "{smoothr}"),
col = c("grey30", "firebrick3", "dodgerblue3"),
lwd = c(3, 3)
)
#> Error:
#> ! plot.new has not been called yet
plot(
sf::st_geometry(lines),
col = "grey30",
lwd = 3.5
)
#> Error in `UseMethod()`:
#> ! no applicable method for 'st_geometry' applied to an object of class "function"
plot(
sf::st_geometry(lines_smoothr),
col = 'dodgerblue3',
lwd = 2,
add = TRUE
)
#> Error:
#> ! object 'lines_smoothr' not found