From 3e00283f2f3c926579283c1e3ab6f61cfd5979c2 Mon Sep 17 00:00:00 2001 From: fawda123 Date: Mon, 25 Mar 2024 10:30:22 -0400 Subject: [PATCH] remove prettymapr, raster, add maptile and tidyterra for anlzMWRmap() basemaps, #66 --- DESCRIPTION | 4 ++-- NAMESPACE | 1 - R/anlzMWRmap.R | 30 ++++++++++++++++-------------- man/anlzMWRmap.Rd | 4 ++-- tests/testthat/test-anlzMWRmap.R | 2 +- vignettes/analysis.Rmd | 10 +++++----- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 82332ad8..805f198f 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -38,13 +38,13 @@ Imports: ggspatial, httr, lubridate, - prettymapr, - raster, + maptiles, RColorBrewer, rmarkdown, readxl, sf, tidyr, + tidyterra, tibble, units, writexl diff --git a/NAMESPACE b/NAMESPACE index d3822e8b..ede98a12 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -45,7 +45,6 @@ export(utilMWRvaluerange) export(utilMWRyscale) import(RColorBrewer) import(dplyr) -import(prettymapr) import(tidyr) importFrom(grDevices,dev.off) importFrom(grDevices,png) diff --git a/R/anlzMWRmap.R b/R/anlzMWRmap.R index fc1abb31..1b92eb00 100644 --- a/R/anlzMWRmap.R +++ b/R/anlzMWRmap.R @@ -19,7 +19,7 @@ #' @param zoom numeric indicating resolution of the base map, see details #' @param addwater character string as \code{"low"}, \code{"medium"} (default), \code{"high"}, or \code{NULL} (to suppress) to include water features with varying detail from the National Hydrography dataset, see details #' @param watercol character string of color for water objects if \code{addwater} is not \code{NULL} -#' @param maptype character string as \code{"cartolight"}, \code{"cartodark"}, \code{"osm"}, \code{"hotstyle"}, or \code{NULL} (to suppress, default) indicating the basemap type, see details +#' @param maptype character string indicating the basemap type, see details #' @param buffdist numeric for buffer around the bounding box for the selected sites in kilometers, see details #' @param scaledist character string indicating distance unit for the scale bar, \code{"km"} or \code{"mi"} #' @param northloc character string indicating location of the north arrow, see details @@ -29,8 +29,6 @@ #' @param runchk logical to run data checks with \code{\link{checkMWRresults}}, \code{\link{checkMWRacc}}, or \code{\link{checkMWRsites}}, applies only if \code{res}, \code{acc}, or \code{sit} are file paths #' @param warn logical to return warnings to the console (default) #' -#' @import prettymapr -#' #' @return A \code{\link[ggplot2]{ggplot}} object that can be further modified. #' @export #' @@ -45,7 +43,7 @@ #' #' Using \code{addwater = "medium"} (default) will include lines and polygons of natural water bodies defined using the National Hydrography Dataset (NHD). The level of detail can be changed to low or high using \code{addwater = "low"} or \code{addwater = "high"}, respectively. Use \code{addwater = NULL} to not show any water features. #' -#' A base map can be plotted using the \code{maptype} argument and is obtained from the \code{\link[ggspatial]{annotation_map_tile}} function of ggspatial. The \code{zoom} value specifies the resolution of the map. Use higher values to download map tiles with greater resolution, although this increases the download time. The \code{maptype} argument describes the type of base map to download. Acceptable options include \code{"cartolight"}, \code{"cartodark"}, \code{"osm"}, or \code{"hotstyle"}. Use \code{maptype = NULL} to suppress the base map. +#' A base map can be plotted using the \code{maptype} argument. The \code{zoom} value specifies the resolution of the map. Use higher values to download map tiles with greater resolution, although this increases the download time. The \code{maptype} argument describes the type of base map to download. Acceptable options include \code{"OpenStreetMap"}, \code{"OpenStreetMap.DE"}, \code{"OpenStreetMap.France"}, \code{"OpenStreetMap.HOT"}, \code{"OpenTopoMap"}, \code{"Esri.WorldStreetMap"}, \code{"Esri.DeLorme"}, \code{"Esri.WorldTopoMap"}, \code{"Esri.WorldImagery"}, \code{"Esri.WorldTerrain"}, \code{"Esri.WorldShadedRelief"}, \code{"Esri.OceanBasemap"}, \code{"Esri.NatGeoWorldMap"}, \code{"Esri.WorldGrayCanvas"}, \code{"CartoDB.Positron"}, \code{"CartoDB.PositronNoLabels"}, \code{"CartoDB.PositronOnlyLabels"}, \code{"CartoDB.DarkMatter"}, \code{"CartoDB.DarkMatterNoLabels"}, \code{"CartoDB.DarkMatterOnlyLabels"}, \code{"CartoDB.Voyager"}, \code{"CartoDB.VoyagerNoLabels"}, or \code{"CartoDB.VoyagerOnlyLabels"}. Use \code{maptype = NULL} to suppress the base map. #' #' The area around the summarized points can be increased or decreased using the \code{buffdist} argument. This creates a buffered area around the bounding box for the points, where the units are kilometers. #' @@ -139,15 +137,13 @@ anlzMWRmap<- function(res = NULL, param, acc = NULL, sit = NULL, fset = NULL, si sf::st_as_sfc() %>% sf::st_buffer(dist = units::set_units(buffdist, kilometer)) %>% sf::st_transform(crs = 4326) %>% - sf::st_bbox() %>% - unname + sf::st_bbox() if(nrow(tomap) == 1) dat_ext <- tomap %>% sf::st_as_sfc() %>% sf::st_buffer(dist = units::set_units(buffdist, kilometer)) %>% sf::st_transform(crs = 4326) %>% - sf::st_bbox() %>% - unname + sf::st_bbox() ylab <- unique(resdat$`Result Unit`) ttl <- utilMWRtitle(param = param, accdat = accdat, sumfun = sumfun, site = site, dtrng = dtrng, locgroup = locgroup, resultatt = resultatt) @@ -155,15 +151,21 @@ anlzMWRmap<- function(res = NULL, param, acc = NULL, sit = NULL, fset = NULL, si m <- ggplot2::ggplot() if(!is.null(maptype)){ - - maptype <- match.arg(maptype, c( 'cartolight', 'cartodark', 'osm', 'hotstyle')) - # this is only to satisfy raster in imports, does nothing - # do not import all of raster becaus of namespace conflicts - tmp <- raster::as.data.frame(x = NULL) + maptype <- match.arg(maptype, c("OpenStreetMap", "OpenStreetMap.DE", "OpenStreetMap.France", + "OpenStreetMap.HOT", "OpenTopoMap", + "Esri.WorldStreetMap", "Esri.DeLorme", "Esri.WorldTopoMap", + "Esri.WorldImagery", "Esri.WorldTerrain", "Esri.WorldShadedRelief", + "Esri.OceanBasemap", "Esri.NatGeoWorldMap", "Esri.WorldGrayCanvas", + "CartoDB.Positron", "CartoDB.PositronNoLabels", + "CartoDB.PositronOnlyLabels", "CartoDB.DarkMatter", + "CartoDB.DarkMatterNoLabels", "CartoDB.DarkMatterOnlyLabels", + "CartoDB.Voyager", "CartoDB.VoyagerNoLabels", "CartoDB.VoyagerOnlyLabels")) + + tls <- maptiles::get_tiles(dat_ext, provider = maptype, zoom = zoom, ) m <- m + - ggspatial::annotation_map_tile(zoom = zoom, quiet = TRUE, progress = "none", type = maptype, cachedir = system.file("rosm.cache", package = "ggspatial")) + tidyterra::geom_spatraster_rgb(data = tls, maxcell = 1e8) } diff --git a/man/anlzMWRmap.Rd b/man/anlzMWRmap.Rd index 20039970..fa68e218 100644 --- a/man/anlzMWRmap.Rd +++ b/man/anlzMWRmap.Rd @@ -74,7 +74,7 @@ anlzMWRmap( \item{watercol}{character string of color for water objects if \code{addwater} is not \code{NULL}} -\item{maptype}{character string as \code{"cartolight"}, \code{"cartodark"}, \code{"osm"}, \code{"hotstyle"}, or \code{NULL} (to suppress, default) indicating the basemap type, see details} +\item{maptype}{character string indicating the basemap type, see details} \item{buffdist}{numeric for buffer around the bounding box for the selected sites in kilometers, see details} @@ -109,7 +109,7 @@ The results shown on the map represent the parameter summary for each site withi Using \code{addwater = "medium"} (default) will include lines and polygons of natural water bodies defined using the National Hydrography Dataset (NHD). The level of detail can be changed to low or high using \code{addwater = "low"} or \code{addwater = "high"}, respectively. Use \code{addwater = NULL} to not show any water features. -A base map can be plotted using the \code{maptype} argument and is obtained from the \code{\link[ggspatial]{annotation_map_tile}} function of ggspatial. The \code{zoom} value specifies the resolution of the map. Use higher values to download map tiles with greater resolution, although this increases the download time. The \code{maptype} argument describes the type of base map to download. Acceptable options include \code{"cartolight"}, \code{"cartodark"}, \code{"osm"}, or \code{"hotstyle"}. Use \code{maptype = NULL} to suppress the base map. +A base map can be plotted using the \code{maptype} argument. The \code{zoom} value specifies the resolution of the map. Use higher values to download map tiles with greater resolution, although this increases the download time. The \code{maptype} argument describes the type of base map to download. Acceptable options include \code{"OpenStreetMap"}, \code{"OpenStreetMap.DE"}, \code{"OpenStreetMap.France"}, \code{"OpenStreetMap.HOT"}, \code{"OpenTopoMap"}, \code{"Esri.WorldStreetMap"}, \code{"Esri.DeLorme"}, \code{"Esri.WorldTopoMap"}, \code{"Esri.WorldImagery"}, \code{"Esri.WorldTerrain"}, \code{"Esri.WorldShadedRelief"}, \code{"Esri.OceanBasemap"}, \code{"Esri.NatGeoWorldMap"}, \code{"Esri.WorldGrayCanvas"}, \code{"CartoDB.Positron"}, \code{"CartoDB.PositronNoLabels"}, \code{"CartoDB.PositronOnlyLabels"}, \code{"CartoDB.DarkMatter"}, \code{"CartoDB.DarkMatterNoLabels"}, \code{"CartoDB.DarkMatterOnlyLabels"}, \code{"CartoDB.Voyager"}, \code{"CartoDB.VoyagerNoLabels"}, or \code{"CartoDB.VoyagerOnlyLabels"}. Use \code{maptype = NULL} to suppress the base map. The area around the summarized points can be increased or decreased using the \code{buffdist} argument. This creates a buffered area around the bounding box for the points, where the units are kilometers. diff --git a/tests/testthat/test-anlzMWRmap.R b/tests/testthat/test-anlzMWRmap.R index 222de90a..22498664 100644 --- a/tests/testthat/test-anlzMWRmap.R +++ b/tests/testthat/test-anlzMWRmap.R @@ -6,7 +6,7 @@ test_that("Checking output format", { test_that("Checking output format, one site", { skip_on_cran() - result <- anlzMWRmap(res = resdat, param = 'DO', acc = accdat, sit = sitdat, warn = FALSE, site = 'ABT-026', addwater = NULL, maptype = 'osm', zoom = 5) + result <- anlzMWRmap(res = resdat, param = 'DO', acc = accdat, sit = sitdat, warn = FALSE, site = 'ABT-026', addwater = NULL, maptype = 'CartoDB.Positron', zoom = 5) expect_s3_class(result, 'ggplot') }) diff --git a/vignettes/analysis.Rmd b/vignettes/analysis.Rmd index 7d6ded4e..768ea49e 100644 --- a/vignettes/analysis.Rmd +++ b/vignettes/analysis.Rmd @@ -301,18 +301,18 @@ The buffered distance around the points can be increased using the `buffdist` ar anlzMWRmap(res = resdat, param = "DO", acc = accdat, sit = sitdat, buffdist = 20, warn = F) ``` -A base map can be included as well using the `maptype` argument. Options include `"cartolight"`, `"cartodark"`, `"osm"`, or `"hotstyle"`. The `zoom` argument can be helpful when using a basemap. The default `zoom` argument is set to 11 and decreasing the number will download a base map with lower resolution. This can decrease map processing times for large areas. +A base map can be included as well using the `maptype` argument. Options include `"OpenStreetMap"`, `"OpenStreetMap.DE"`, `"OpenStreetMap.France"`, `"OpenStreetMap.HOT"`, `"OpenTopoMap"`, `"Esri.WorldStreetMap"`, `"Esri.DeLorme"`, `"Esri.WorldTopoMap"`, `"Esri.WorldImagery"`, `"Esri.WorldTerrain"`, `"Esri.WorldShadedRelief"`, `"Esri.OceanBasemap"`, `"Esri.NatGeoWorldMap"`, `"Esri.WorldGrayCanvas"`, `"CartoDB.Positron"`, `"CartoDB.PositronNoLabels"`, `"CartoDB.PositronOnlyLabels"`, `"CartoDB.DarkMatter"`, `"CartoDB.DarkMatterNoLabels"`, `"CartoDB.DarkMatterOnlyLabels"`, `"CartoDB.Voyager"`, `"CartoDB.VoyagerNoLabels"`, or `"CartoDB.VoyagerOnlyLabels"`. The `zoom` argument can be helpful when using a basemap. The default `zoom` argument is set to 11 and decreasing the number will download a base map with lower resolution. This can decrease map processing times for large areas. -A `"cartolight"` base map: +A `"CartoDB.Positron"` base map: ```{r, fig.height = 6, fig.width = 6} -anlzMWRmap(res = resdat, param = "DO", acc = accdat, sit = sitdat, maptype = "cartolight", warn = F, addwater = NULL) +anlzMWRmap(res = resdat, param = "DO", acc = accdat, sit = sitdat, maptype = "CartoDB.Positron", warn = F, addwater = NULL) ``` -A `"osm"` base map: +A `"Esri.WorldImagery"` base map: ```{r, fig.height = 6, fig.width = 6} -anlzMWRmap(res = resdat, param = "DO", acc = accdat, sit = sitdat, maptype = "osm", warn = F, addwater = NULL) +anlzMWRmap(res = resdat, param = "DO", acc = accdat, sit = sitdat, maptype = "Esri.WorldImagery", warn = F, addwater = NULL) ``` A map with no base map or water bodies: