From 54444f1dc4015ddedd4b7ba259e4441182356667 Mon Sep 17 00:00:00 2001 From: Stefan Kuethe Date: Fri, 4 Oct 2024 12:46:55 +0200 Subject: [PATCH] Add func to build basemap style --- NAMESPACE | 1 + R/basemaps.R | 16 +++++++++ R/types.R | 6 ++++ examples/pmtiles.R | 5 +-- man/Layer.Rd | 7 ++-- man/Marker.Rd | 10 +++--- man/MarkerOptions.Rd | 10 +++--- man/add_layer.Rd | 7 ++-- man/add_marker.Rd | 10 +++--- man/add_popup.Rd | 7 ++-- man/add_tooltip.Rd | 7 ++-- man/construct_basemap_style.Rd | 60 ++++++++++++++++++++++++++++++++++ man/maplibre.Rd | 1 - 13 files changed, 109 insertions(+), 38 deletions(-) create mode 100644 R/basemaps.R create mode 100644 R/types.R create mode 100644 man/construct_basemap_style.Rd diff --git a/NAMESPACE b/NAMESPACE index 369fe48..fd2d53c 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -10,6 +10,7 @@ export(add_layer) export(add_marker) export(add_popup) export(add_tooltip) +export(construct_basemap_style) export(mapOptions) export(maplibre) export(maplibreOutput) diff --git a/R/basemaps.R b/R/basemaps.R new file mode 100644 index 0000000..cecc70c --- /dev/null +++ b/R/basemaps.R @@ -0,0 +1,16 @@ +#' Create a basemap style definition +#' @param sources The sources to be used for the basemap style. +#' @param layers The layers to be used for the basemap style. +#' @param name The name of the basemap style. +#' @returns list +#' @example examples/pmtiles.R +#' @export +construct_basemap_style <- function(sources, layers, name = "custom-basemap") { + basemap_style <- list( + name = name, + version = 8L, + sources = sources, + layers = layers + ) + return(rdantic(basemap_style, TYPES_BASEMAP_STYLE)) +} diff --git a/R/types.R b/R/types.R new file mode 100644 index 0000000..4865ab5 --- /dev/null +++ b/R/types.R @@ -0,0 +1,6 @@ +TYPES_BASEMAP_STYLE <- list( + name = is.character, + version = is.integer, + sources = is.list, + layers = is.list +) diff --git a/examples/pmtiles.R b/examples/pmtiles.R index d2684b4..a118591 100644 --- a/examples/pmtiles.R +++ b/examples/pmtiles.R @@ -24,8 +24,9 @@ roads <- Layer( paint = list("line-color" = "black"), ) -custom_basemap <- list( - name = "custom-style", version = 8, sources = sources, layers = list(landuse, roads) +custom_basemap <- construct_basemap_style( + sources, + layers = list(landuse, roads) ) setup <- mapOptions( diff --git a/man/Layer.Rd b/man/Layer.Rd index c22faff..0402319 100644 --- a/man/Layer.Rd +++ b/man/Layer.Rd @@ -28,9 +28,7 @@ earthquakes_source <- list( data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" ) - - - earthquakes_layer <- Layer( +earthquakes_layer <- Layer( id = "earthquakes", type = "circle", source = earthquakes_source, @@ -39,7 +37,7 @@ earthquakes_source <- list( # Adds a tooltip that appears when hovering over it -maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> +maplibre() |> add_layer(earthquakes_layer) |> add_tooltip("earthquakes", prop = "mag") @@ -48,5 +46,4 @@ maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> maplibre() |> add_layer(earthquakes_layer) |> add_popup("earthquakes", prop = "mag") - } diff --git a/man/Marker.Rd b/man/Marker.Rd index 56785be..806a8c5 100644 --- a/man/Marker.Rd +++ b/man/Marker.Rd @@ -11,12 +11,12 @@ Create a Marker } \examples{ library(maplibre) -marker = Marker( - lngLat = c(9.5,51.31667), - popup = list(text = "This is a marker",options = list(closeButton = F)), - color= "darkred" +marker <- Marker( + lngLat = c(9.5, 51.31667), + popup = list(text = "This is a marker", options = list(closeButton = F)), + color = "darkred" ) -maplibre(mapOptions(center = c(9.5,51.31667)),zoom = 4) |> +maplibre(mapOptions(center = c(9.5, 51.31667)), zoom = 4) |> add_marker(marker) } diff --git a/man/MarkerOptions.Rd b/man/MarkerOptions.Rd index 1ecdbaa..9df2408 100644 --- a/man/MarkerOptions.Rd +++ b/man/MarkerOptions.Rd @@ -23,12 +23,12 @@ Title } \examples{ library(maplibre) -marker = Marker( - lngLat = c(9.5,51.31667), - popup = list(text = "This is a marker",options = list(closeButton = F)), - color= "darkred" +marker <- Marker( + lngLat = c(9.5, 51.31667), + popup = list(text = "This is a marker", options = list(closeButton = F)), + color = "darkred" ) -maplibre(mapOptions(center = c(9.5,51.31667)),zoom = 4) |> +maplibre(mapOptions(center = c(9.5, 51.31667)), zoom = 4) |> add_marker(marker) } diff --git a/man/add_layer.Rd b/man/add_layer.Rd index e6de454..cf56202 100644 --- a/man/add_layer.Rd +++ b/man/add_layer.Rd @@ -20,9 +20,7 @@ earthquakes_source <- list( data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" ) - - - earthquakes_layer <- Layer( +earthquakes_layer <- Layer( id = "earthquakes", type = "circle", source = earthquakes_source, @@ -31,7 +29,7 @@ earthquakes_source <- list( # Adds a tooltip that appears when hovering over it -maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> +maplibre() |> add_layer(earthquakes_layer) |> add_tooltip("earthquakes", prop = "mag") @@ -40,5 +38,4 @@ maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> maplibre() |> add_layer(earthquakes_layer) |> add_popup("earthquakes", prop = "mag") - } diff --git a/man/add_marker.Rd b/man/add_marker.Rd index cf4a312..7f9a04b 100644 --- a/man/add_marker.Rd +++ b/man/add_marker.Rd @@ -14,12 +14,12 @@ Add a marker to map } \examples{ library(maplibre) -marker = Marker( - lngLat = c(9.5,51.31667), - popup = list(text = "This is a marker",options = list(closeButton = F)), - color= "darkred" +marker <- Marker( + lngLat = c(9.5, 51.31667), + popup = list(text = "This is a marker", options = list(closeButton = F)), + color = "darkred" ) -maplibre(mapOptions(center = c(9.5,51.31667)),zoom = 4) |> +maplibre(mapOptions(center = c(9.5, 51.31667)), zoom = 4) |> add_marker(marker) } diff --git a/man/add_popup.Rd b/man/add_popup.Rd index c727b96..ac1964f 100644 --- a/man/add_popup.Rd +++ b/man/add_popup.Rd @@ -20,9 +20,7 @@ earthquakes_source <- list( data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" ) - - - earthquakes_layer <- Layer( +earthquakes_layer <- Layer( id = "earthquakes", type = "circle", source = earthquakes_source, @@ -31,7 +29,7 @@ earthquakes_source <- list( # Adds a tooltip that appears when hovering over it -maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> +maplibre() |> add_layer(earthquakes_layer) |> add_tooltip("earthquakes", prop = "mag") @@ -40,5 +38,4 @@ maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> maplibre() |> add_layer(earthquakes_layer) |> add_popup("earthquakes", prop = "mag") - } diff --git a/man/add_tooltip.Rd b/man/add_tooltip.Rd index a7e0202..e330a26 100644 --- a/man/add_tooltip.Rd +++ b/man/add_tooltip.Rd @@ -20,9 +20,7 @@ earthquakes_source <- list( data = "https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson" ) - - - earthquakes_layer <- Layer( +earthquakes_layer <- Layer( id = "earthquakes", type = "circle", source = earthquakes_source, @@ -31,7 +29,7 @@ earthquakes_source <- list( # Adds a tooltip that appears when hovering over it -maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> +maplibre() |> add_layer(earthquakes_layer) |> add_tooltip("earthquakes", prop = "mag") @@ -40,5 +38,4 @@ maplibre(map_options = mapOptions(minZoom=1, maxZoom = 2)) |> maplibre() |> add_layer(earthquakes_layer) |> add_popup("earthquakes", prop = "mag") - } diff --git a/man/construct_basemap_style.Rd b/man/construct_basemap_style.Rd new file mode 100644 index 0000000..f7ccd61 --- /dev/null +++ b/man/construct_basemap_style.Rd @@ -0,0 +1,60 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/basemaps.R +\name{construct_basemap_style} +\alias{construct_basemap_style} +\title{Create a basemap style definition} +\usage{ +construct_basemap_style(sources, layers, name = "custom-basemap") +} +\arguments{ +\item{sources}{The sources to be used for the basemap style.} + +\item{layers}{The layers to be used for the basemap style.} + +\item{name}{The name of the basemap style.} +} +\value{ +list +} +\description{ +Create a basemap style definition +} +\examples{ +PMTILES_URL <- "https://pmtiles.io/protomaps(vector)ODbL_firenze.pmtiles" + +pmtiles_source <- list( + type = "vector", + url = glue::glue("pmtiles://{PMTILES_URL}"), + attribution = '© OpenStreetMap' +) + +sources <- list(pmtiles = pmtiles_source) + +landuse <- Layer( + id = "buildings", + source = "pmtiles", + "source-layer" = "landuse", + type = LayerType$FILL, + paint = list("fill-color" = "steelblue") +) + +roads <- Layer( + id = "roads", + source = "pmtiles", + "source-layer" = "roads", + type = LayerType$LINE, + paint = list("line-color" = "black"), +) + +custom_basemap <- construct_basemap_style( + sources, + layers = list(landuse, roads) +) + +setup <- mapOptions( + style = custom_basemap, + bounds = list(11.154026, 43.7270125, 11.3289395, 43.8325455) +) + +maplibre(setup) +} diff --git a/man/maplibre.Rd b/man/maplibre.Rd index 75e4a94..e016c35 100644 --- a/man/maplibre.Rd +++ b/man/maplibre.Rd @@ -25,5 +25,4 @@ map_options <- mapOptions( ) maplibre(map_options, zoom = 12) |> add_control("NavigationControl", "top-left", showCompass = F) - }