
Add a Spatial Layer to a Leaflet Map
add_leaflet_layer.RdThis function adds a spatial layer (e.g., polygons or polylines, or points) to an existing Leaflet map, customizing its appearance with colors, weights, opacity, and fill properties. It dynamically handles different geometry types within the layer.
Arguments
- map
leaflet map object (
leafletobject) to which the layer will be added.- layer
sfobject representing the spatial layer to add to the map. This object should contain attributes for color, opacity, and fill.- group
character string representing the group name for the layer. This is used for the layers control in the Leaflet map, allowing users to toggle the layer's visibility.
- 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.
Details
The function differentiates between polygon, polyline, and point geometries
within the layer object.
Polygons are added using leaflet::addPolygons, polylines are added using
leaflet::addPolylines, and points are added using either
leaflet::addMarkers, leaflet::addCircleMarkers, or leaflet::addPopups,
based on the point_marker parameter.
The function also adds the layer's legend 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
See also
Other interactive mapping:
add_visual_param(),
map_leaflet_commune(),
map_leaflet_multilayer(),
set_provider()
Examples
if (FALSE) { # \dontrun{
# Example usage:
map_bg = set_provider("ign_3d")
map = leaflet::leaflet() |>
leaflet::addTiles(
urlTemplate = map_bg[["provider"]],
attribution = map_bg[["attribution"]],
options = leaflet::tileOptions(opacity = 0.2)
)
map
map = add_leaflet_layer(
map = map,
layer = add_visual_param(
layer = so_ii_limit,
visual_param = data.frame(
id = "perimeter",
label = "perimeter",
popup = "so-ii perimeter",
color = "white",
fill = "transparent",
opacity = 1
)
),
group = "perimeter"
)
map
map = add_leaflet_layer(
map = map,
layer = add_visual_param(
layer = so_ii_hydro,
visual_param = 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
),
junction = c("type", "id")
),
group = "perimeter"
)
map
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 = add_leaflet_layer(
map = map,
layer = add_visual_param(
layer = random_points,
visual_param = 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"]])
),
junction = "label"
),
group = "random_points"
)
map
} # }