Skip to content

Commit

Permalink
Merge pull request #160 from dieghernan/stretch
Browse files Browse the repository at this point in the history
Add stretch and zlim to geom_spatraster_rgb
  • Loading branch information
dieghernan authored Jan 5, 2025
2 parents aba8cc5 + b47ffee commit 5beb9d1
Show file tree
Hide file tree
Showing 23 changed files with 635 additions and 6 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -300,3 +300,4 @@ importFrom(tidyr,fill)
importFrom(tidyr,pivot_longer)
importFrom(tidyr,pivot_wider)
importFrom(tidyr,replace_na)
importFrom(utils,packageVersion)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# tidyterra (development version)

- Add (limited) support for `SpatGraticule` (see `terra::graticule()`) #155.
- New arguments in `geom_spatraster_rgb()`: `stretch` and `zlim`. See
`terra::plotRGB()` for clarification.
- `geom_spatraster()` suggests `geom_spatraster_rgb()` when a `SpatRaster`
with RGB specification is detected (`terra::has.RGB()` is `TRUE`)

# tidyterra 0.6.1

Expand Down
8 changes: 8 additions & 0 deletions R/geom_spatraster.R
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ geom_spatraster <- function(mapping = aes(),
))
}

# Kindly warn in RGB
if (terra::has.RGB(data)) {
cli::cli_alert_warning(paste(
"RGB specification detected. Maybe use",
cli::style_bold("{.fun tidyterra::geom_spatraster_rgb}:"),
"instead?"
))
}

# 1. Work with aes ----

Expand Down
36 changes: 35 additions & 1 deletion R/geom_spatraster_rgb.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#'
#' @inheritParams geom_spatraster
#' @inheritParams scale_terrain
#' @inheritParams terra::plotRGB
#' @param mapping Ignored.
#' @param r,g,b Integer representing the number of layer of `data` to be
#' considered as the red (`r`), green (`g`) and blue (`b`) channel.
Expand Down Expand Up @@ -82,7 +83,9 @@ geom_spatraster_rgb <- function(mapping = aes(),
alpha = 1,
maxcell = 500000,
max_col_value = 255,
...) {
...,
stretch = NULL,
zlim = NULL) {
if (!inherits(data, "SpatRaster")) {
cli::cli_abort(paste(
"{.fun tidyterra::geom_spatraster_rgb} only works with",
Expand All @@ -107,6 +110,7 @@ geom_spatraster_rgb <- function(mapping = aes(),
))
}


# 1. Work with aes ----
mapping <- override_aesthetics(
mapping,
Expand All @@ -125,6 +129,13 @@ geom_spatraster_rgb <- function(mapping = aes(),
# 2. Check if resample is needed----
data <- resample_spat(data, maxcell)

# stretch and clamp

data <- zlim_strecth(data,
zlim = zlim, stretch = stretch,
max_col_value = max_col_value
)

# 3. Build layer ----
crs_terra <- pull_crs(data)

Expand Down Expand Up @@ -290,3 +301,26 @@ make_hexcol <- function(data, max_col_value = 255) {
df <- dplyr::left_join(xy, full[c("hexcol", "index")], by = "index")
return(df[c("x", "y", "hexcol")])
}

# Strecth and clamp
zlim_strecth <- function(x, zlim = NULL, stretch = NULL, max_col_value = 255) {
if (all(is.null(zlim), is.null(stretch))) {
return(x)
}

if (all(!is.null(zlim), length(zlim) >= 2)) {
zlim <- sort(zlim)[1:2]
x <- terra::clamp(x, zlim[1], zlim[2], values = TRUE)
}

if (!is.null(stretch)) {
if (all(stretch == "lin", length(zlim) == 2)) {
x <- terra::stretch(x, smin = zlim[1], smax = zlim[2])
} else if (stretch == "lin") {
x <- terra::stretch(x, minq = 0.02, maxq = 0.98)
} else if (stretch == "hist") {
x <- terra::stretch(x, histeq = TRUE, scale = max_col_value)
}
}
x
}
1 change: 1 addition & 0 deletions R/tidyterra-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#' @importFrom grDevices terrain.colors rgb col2rgb grey
#' @importFrom tibble as_tibble
#' @importFrom cli qty
#' @importFrom utils packageVersion
NULL

#' @export
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@
"SystemRequirements": null
},
"keywords": ["r", "terra", "ggplot-extension", "r-spatial", "rspatial", "cran", "cran-r", "r-package", "rstats", "rstats-package"],
"fileSize": "2399.205KB",
"fileSize": "2401.527KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down
8 changes: 7 additions & 1 deletion man/geom_spatraster_rgb.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions revdep/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
|field |value |
|:--------|:------------------------------------------------------|
|version |R version 4.4.2 (2024-10-31) |
|os |Ubuntu 22.04.5 LTS |
|os |Ubuntu 24.04.1 LTS |
|system |x86_64, linux-gnu |
|ui |X11 |
|language |(EN) |
|collate |C.UTF-8 |
|ctype |C.UTF-8 |
|tz |UTC |
|date |2024-12-13 |
|date |2025-01-05 |
|pandoc |3.1.11 @ /opt/hostedtoolcache/pandoc/3.1.11/x64/pandoc |

# Dependencies
Expand All @@ -37,7 +37,7 @@
|lifecycle |1.0.4 |1.0.4 | |
|magrittr |2.0.3 |2.0.3 | |
|munsell |0.5.1 |0.5.1 | |
|pillar |1.9.0 |1.9.0 | |
|pillar |1.10.0 |1.10.0 | |
|pkgconfig |2.0.3 |2.0.3 | |
|proxy |0.4-27 |0.4-27 | |
|purrr |1.0.2 |1.0.2 | |
Expand Down
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/geom_spatraster_3lyr.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,3 +111,10 @@
Message
<SpatRaster> resampled to 24 cells.

# Warn in RGB tile

Code
p <- ggplot() + geom_spatraster(data = tile)
Message
! RGB specification detected. Maybe use `tidyterra::geom_spatraster_rgb()`: instead?

101 changes: 101 additions & 0 deletions tests/testthat/_snaps/geom_spatraster_rgb/crsstretch-01-regular.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 5beb9d1

Please sign in to comment.