R/meta_leaflet.R

Defines functions meta_leaflet

Documented in meta_leaflet

#' @export
#' @importFrom leaflet leaflet addProviderTiles addCircleMarkers
#'
#' @title Create a map of RAWS stations
#'
#' @param meta Station metadata created by \code{wrcc_createMeta()} or \code{cefa_createMeta()}.
#' @param radius Circle radius.
#' @param stroke Logical specifying whether to draw stroke along the path.
#' @param color Stroke color.
#' @param opacity Stroke opacity.
#' @param fillColor Fill color.
#' @param fillOpacity Fill opacity.
#' @param weight Stroke width in pixels.
#'
#' @return Leaflet map of stations.
#'
#' @description Generates a map of RAWS stations given station metadata. This
#' station metadata can be generated by \code{wrcc_createMeta()} or \code{cefa_createMeta()}. Parameters
#' such as \code{radius}, \code{stroke}, \code{color}, \code{opacity}, \code{fillColor}
#' \code{fillOpacity}, and \code{weight} can be passed in to adjust features of
#' the map.
#'
#' @examples
#' \donttest{
#' library(RAWSmet)
#'
#' wa_meta <- example_wrcc_meta
#' meta_leaflet(wa_meta)

#' }
#'
#' @rdname meta_leaflet
#' @seealso \code{\link{wrcc_createMeta}}
#' @seealso \code{\link{cefa_createMeta}}
#' @references \href{https://raws.dri.edu/}{RAWS USA Climate Archive}

meta_leaflet <- function(
  meta = NULL,
  radius = 3.5,
  stroke = TRUE,
  color = 'blue',
  opacity = 0.5,
  fillColor = 'blue',
  fillOpacity = 0.5,
  weight = 1
) {

  # ----- Validate parameters --------------------------------------------------

  MazamaCoreUtils::stopIfNull(meta)

  requiredNames <- c('nwsID', 'wrccID', 'locationName',
                     'longitude', 'latitude', 'elevation',
                     'agencyName', 'countryCode', 'stateCode', 'timezone')

  missingNames <- setdiff(requiredNames, names(meta))

  if ( length(missingNames) > 0 ) {
    stop(sprintf(
      "Parameter 'meta' is missing the following columns: %s.",
      paste0(missingNames, collapse = ", ")
    ))
  }

  # ----- Create map -----------------------------------------------------------

  # Create popup text
  meta$popupText <- paste(
    "<strong>", meta$locationName, "</strong><br>",
    "NWS ID:", meta$nwsID, "<br>",
    "WRCC ID:", meta$wrccID, "<br>",
    "Agency:", meta$agencyName, "<br>",
    "Elevation:", round(meta$elevation), "m<br>",
    "Country Code:", meta$countryCode, "<br>",
    "State Code:", meta$stateCode, "<br>",
    "Timezone:", meta$timezone
  )

  # Generate the map
  leafletMap <-
    leaflet::leaflet(meta) %>%
    leaflet::addProviderTiles("Esri.WorldTopoMap") %>%
    leaflet::addCircleMarkers(
      lat = ~meta$latitude,
      lng = ~meta$longitude,
      popup = ~meta$popupText,
      radius = ~radius,
      stroke = ~stroke,
      color = ~color,
      opacity = ~opacity,
      fillColor = ~fillColor,
      fillOpacity = ~fillOpacity,
      weight = ~weight
    )

  # ----- Return  --------------------------------------------------------------

  return(leafletMap)
}
MazamaScience/RAWSmet documentation built on May 6, 2023, 6:57 a.m.