Skip to content

Commit

Permalink
updated gg_raster to use max resolution for all plots by default, add…
Browse files Browse the repository at this point in the history
…ressing #24
  • Loading branch information
16EAGLE committed Apr 5, 2024
1 parent 256b34c commit fe59658
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 9 deletions.
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
Package: basemaps
Type: Package
Title: Accessing Spatial Basemaps in R
Version: 0.0.6
Version: 0.0.7
Depends:
R (>= 3.5.0)
Date: 2024-03-03
Date: 2024-04-05
Authors@R: person("Jakob", "Schwalb-Willmann", email = "[email protected]",
role = c("aut", "cre"), comment = c(ORCID = "0000-0003-2665-1509"))
Description: A lightweight package to access spatial basemaps from open sources such as 'OpenStreetMap', 'Carto', 'Mapbox' and others in R.
Expand All @@ -31,4 +31,4 @@ Suggests:
testthat,
covr
BugReports: https://github.com/16eagle/basemaps/issues
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
***

## basemaps 0.0.6
Minor improvements

**Changes:**

* maps rendered using `basemap_ggplot` or `basemap_gglayer` or objects plotted using `gg_raster` are now displayed with all pixels by default (full resolution, `maxpixels` argument) instead of rendering in a lower resolution (previous default: `500000`).
* switched from using `aes_string` to `aes` in `gg_raster` due to deprectation of the function in `ggplot2`.

<br>


***

## basemaps 0.0.6
Major improvements, adding new map services

Expand Down
1 change: 1 addition & 0 deletions R/basemap.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#' \item \code{browse}, logical, for \code{class = "png"} and interactive sessions only. Whether to open the png file in the system's default PNG viewer or not. Default is \code{TRUE}.
#' \item \code{col}, character vector of colours for transforming single-layer basemaps into RGB, if \code{class = "png"} or \code{class = "magick"}. Default is \code{topo.colors(25)}.
#' \item \code{dpi}, numeric vector of length 1 or 2 specifying the resolution of the image in DPI (dots per inch) for x and y (in that order) - it is recycled to length 2.
#' \item etc. (see \code{?gg_raster} for valid arguments when using \code{class = "gglayer"} or \code{class = "ggplot"}, including \code{maxpixels} to control resolution of ggplot outputs
#' }
#' @param verbose logical, if \code{TRUE}, messages and progress information are displayed on the console (default).
#'
Expand Down
20 changes: 15 additions & 5 deletions R/plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#' @param gglayer logical, if \code{FALSE} (default), a \code{ggplot2} plot is returned, if \code{TRUE}, a \code{ggplot2} layer is returned.
#' @param ... additional arguments, including
#' \itemize{
#' \item \code{maxpixels}, numeric, maximum number of pixels to be plotted (default: 500000)
#' \item \code{maxpixels}, numeric, maximum number of pixels to be plotted (default: number of pixels in r). Use a value lower then ncell(r) to lower resolution for faster plotting.
#' \item \code{alpha}, numeric between 0 and 1, alpha value of the plotted data (transparency).
#' \item \code{maxColorValue}, numeric, the value to use as colour maximum.
#' \item \code{interpolate}, logical, whether to smooth the plot (default is \code{TRUE}).
Expand All @@ -34,12 +34,19 @@
#' @export
gg_raster <- function(r, r_type = "RGB", gglayer = F, ...){

if(!any(grepl("ggplot", rownames(installed.packages())))){
out(paste0("Package 'ggplot2' is not installed, but needed for class='", class, "'. Please install 'ggplot2' using install.packages('ggplot2')."), type = 3)
}

if(inherits(r, "Raster")){
r <- rast(r)
}
if(!inherits(r, "SpatRaster")){
out("Argument r needs to be a raster of class 'SpatRaster', 'RasterLayer', 'RasterBrick' or 'RasterStack'.", type = 3)
}

extras <- list(...)
if(!is.null(extras$maxpixels)) maxpixels <- extras$maxpixels else maxpixels <- 500000
if(!is.null(extras$maxpixels)) maxpixels <- extras$maxpixels else maxpixels <- ncell(r) #500000
if(!is.null(extras$alpha)) alpha <- extras$alpha else alpha <- 1
if(!is.null(extras$maxColorValue)) maxColorValue <- extras$maxColorValue else maxColorValue <- NA
if(!is.null(extras$interpolate)) interpolate <- extras$interpolate else interpolate <- TRUE
Expand All @@ -49,7 +56,7 @@ gg_raster <- function(r, r_type = "RGB", gglayer = F, ...){
if(maxpixels < ncell(r)) r <- aggregate(r, fact = ceiling(ncell(r)/maxpixels))

# transform into data.frame
df <- data.frame(as.data.frame(r, xy = T))
df <- data.frame(as.data.frame(r, xy = T, na.rm = F))
colnames(df) <- c("x", "y", paste0("val", 1:(ncol(df)-2)))

# factor if discrete to show categrocial legend
Expand Down Expand Up @@ -77,11 +84,14 @@ gg_raster <- function(r, r_type = "RGB", gglayer = F, ...){
if(any(na.sel)) df <- df[!na.sel,]
}
# if NA gaps are there, use geom_tile, otherwise make it fast using geom_raster
.data <- ggplot2::.data
if(any(na.sel)){
# remark: is this ever called?
gg <- ggplot2::geom_tile(ggplot2::aes_string(x = "x", y = "y", fill = "fill"), data = df, alpha = alpha)
out(paste0("Using geom_tile() with maxpixels = ", maxpixels, "."))
gg <- ggplot2::geom_tile(ggplot2::aes(x = .data$x, y = .data$y, fill = .data$fill), data = df, alpha = alpha)
} else{
gg <- ggplot2::geom_raster(ggplot2::aes_string(x = "x", y = "y", fill = "fill"), data = df, alpha = alpha, interpolate = interpolate)
out(paste0("Using geom_raster() with maxpixels = ", maxpixels, "."))
gg <- ggplot2::geom_raster(ggplot2::aes(x = .data$x, y = .data$y, fill = .data$fill), data = df, alpha = alpha, interpolate = interpolate)
}

if(isFALSE(gglayer)){
Expand Down
1 change: 1 addition & 0 deletions man/basemap.Rd

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

2 changes: 1 addition & 1 deletion man/plot.Rd

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

3 changes: 3 additions & 0 deletions tests/testthat/test-plot.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ test_that("gg_raster()", {
map <- basemap_raster(ext, map_dir = map_dir, verbose = F)
expect_is(gg_raster(map, r_type = "RGB"), "gg")
expect_is(gg_raster(map, r_type = "RGB", gglayer = F), "gg")

map <- basemap_stars(ext, map_dir = map_dir, verbose = F)
expect_error(gg_raster(map, r_type = "RGB", gglayer = F))
})

0 comments on commit fe59658

Please sign in to comment.