Skip to contents

This function generates an interactive Leaflet map displaying multiple layers.It uses spatial data from sf objects of type polygon, multipolygon, linestring and multilinestring. The function adds a base map layer using tile provider and then overlays the layers.

Usage

map_leaflet_multilayer(
  base_layer,
  base_color,
  base_junction = NULL,
  overlay_layer,
  overlay_color,
  overlay_junction = NULL,
  map_bg = set_provider("ign_ortho"),
  bg_opacity = 0.2,
  point_marker = "marker",
  show_popup = TRUE
)

Arguments

base_layer

list of named sf objects, where the sf objects are the base layers to add to the map and the names the labels of the layers

base_color

list of named data.frames, where the color vectors contain all colors for all of the geometries of the corresponding sf object in the base_layer parameter. Names should be the same as in base_layer

base_junction

list of vectors containing the column names with which each object in base_layer can be joined to the corresponding object in base_color in a merge operation. It must be the same length as base_layer. Names should be the same as in overlay_layer. If the layer class is "sfc", the junction can be omitted. Defaults to NULL.

overlay_layer

list of named sf objects, where the sf objects are the overlay layers to add to the map and the names the labels of the layers

overlay_color

list of named color vectors, where the color vectors contain all colors for all of the geometries of the corresponding sf object in the overlay_layer parameter. Names should be the same as in overlay_layer

overlay_junction

list of vectors containing the column names with which each object in overlay_layer can be joined to the corresponding object in overlay_color in a merge operation. It must be the same length as overlay_layer. Names should be the same as in overlay_layer. If the layer class is "sfc", the junction can be omitted. Defaults to NULL.

map_bg

named list, containing the background map provider URL and the attribution text, as issued from the function set_provider()

bg_opacity

numeric, background map opacity; values should be between 0 and 1. Defaults to 0.2

point_marker

character string specifying the type of marker to use for point geometries. Valid options are "marker", "circle", and "popup". Defaults to "marker".

show_popup

Logical. It indicates whether to display popups for features in the layer. Defaults to TRUE.

Value

A Leaflet map object (invisibly). The map is displayed in the viewer or within an R Markdown or quarto document.

Details

The function internally calls the function add_leaflet_layer(), enabling it to differentiate between polygon, polyline, and point geometries within the layer object. The layer's legend is also added to the map, displaying the unique fill colors and corresponding labels from the layer's attributes (only for non-point geometries).

The layer (sf object) provided to this function is expected to include the following attributes in its associated data.frame:

  • color: A character or numeric vector specifying the color of the feature (used for polygons and polylines).

  • opacity: A numeric value between 0 and 1 indicating the opacity of the feature (used for polygons and polylines).

  • fill: A character or numeric vector specifying the fill color of the feature (used for polygons).

  • label: A character vector providing a label or description for each feature This label is used as the legend label for polygon and line geometries.

  • popup: If show_popup = TRUE, a character vector containing HTML-formatted text

Examples


if (FALSE) { # \dontrun{
# Example 1
map_leaflet_multilayer(
    base_layer = list(
        perimeter = so.ii::so_ii_limit
    ),
    base_color = list(
        perimeter = data.frame(
            id = "perimeter",
            label = "so-ii perimeter",
            popup = "so-ii",
            color = "white",
            fill = "transparent",
            opacity = 1
        )
    ),
    overlay_layer = list(
        clc = so.ii::so_ii_clc["color"]
    ),
    overlay_color = list(
        clc = data.frame(
            id = so.ii::clc_color[["color"]],
            label = so.ii::clc_color[["label_fr"]],
            popup = so.ii::clc_color[["label_fr"]],
            color = so.ii::clc_color[["color"]], 
            fill = so.ii::clc_color[["color"]],
            opacity = 1
        )
    ),
    overlay_junction = list(
        clc = c("color", "id")
    ),
    map_bg = so.ii::set_provider("ign_3d"),
    bg_opacity = 0.6
)

# Example 2
map_leaflet_multilayer(
    base_layer = list(
        perimeter = so.ii::so_ii_limit
    ),
    base_color = list(
        perimeter = data.frame(
            id = "perimeter",
            label = "so-ii perimeter",
            popup = "so-ii",
            color = "white",
            fill = "transparent",
            opacity = 1
        )
    ),
    overlay_layer = list(
        clc = so.ii::so_ii_clc["color"],
        hydro = so.ii::so_ii_hydro["type"]
    ),
    overlay_color = list(
        clc = data.frame(
            id = so.ii::clc_color[["color"]],
            label = so.ii::clc_color[["label_fr"]],
            popup = so.ii::clc_color[["label_fr"]],
            color = so.ii::clc_color[["color"]], 
            fill = so.ii::clc_color[["color"]],
            opacity = 1
        ),
        hydro = data.frame(
            id = unique(so.ii::so_ii_hydro[["type"]]),
            label = unique(so.ii::so_ii_hydro[["type"]]),
            popup = unique(so.ii::so_ii_hydro[["type"]]),
            color = scales::alpha(c("blue", "red", "dodgerblue"), 0.5), 
            fill = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
            opacity = 0.5
        )
    ),
    overlay_junction = list(
        clc = c("color", "id"),
        hydro = c("type", "id")
    ),
    map_bg = so.ii::set_provider("ign_3d"),
    bg_opacity = 0.6,
    show_popup = FALSE
)

# Example3
library(sf)
random_points = data.frame(
    label = c("point1", "point2", "point3"),
    long = c(3.821011, 3.870760, 3.896732),
    lat = c(43.55250, 43.65147, 43.61877)
)

random_points = sf::st_as_sf(
    x = random_points,
    coords = c("long","lat"),
    crs = sf::st_crs(so_ii_hydro)
)
map_leaflet_multilayer(
    base_layer = list(
        perimeter = so.ii::so_ii_limit
    ),
    base_color = list(
        perimeter = data.frame(
            id = "perimeter",
            label = "perimeter",
            color = "white",
            fill = "transparent",
            opacity = 1
        )
    ),
    overlay_layer = list(
        hydro = so.ii::so_ii_hydro,
        random_points = random_points
    ),
    overlay_color = list(
        hydro = data.frame(
            id = unique(so.ii::so_ii_hydro[["type"]]),
            label = unique(so.ii::so_ii_hydro[["type"]]),
            color = scales::alpha(c("blue", "red", "dodgerblue"), 0.5), 
            fill = scales::alpha(c("blue", "red", "dodgerblue"), 0.5),
            opacity = 0.5
        ),
        random_points = data.frame(
            id = unique(random_points[["label"]]),
            label = unique(random_points[["label"]]),
            color = NA, 
            fill = NA,
            opacity = 0.5,
            popup = sprintf("this is random %s", random_points[["label"]])
        )
    ),
    overlay_junction = list(
        hydro = c("type", "id"),
        random_points = "label"
    ),
    map_bg = so.ii::set_provider("ign_ortho"),
    bg_opacity = 0.6
)
} # }