Skip to contents

This function enhances a spatial layer (either sf or sfc object) with visual parameters, typically colors or other attributes, using a merge operation (sf) or direct attribute addition (sfc).

Usage

add_visual_param(layer, visual_param, junction = NULL)

Arguments

layer

spatial object, either an sf object or an sfc object. This is the layer to which visual parameters will be added.

visual_param

data.frame containing the visual parameters to add. This should have a common identifier with the layerobject for merging.

junction

character vector specifying the column name(s) to use for merging the layer and visual_param objects. For sf objects, this defines the join key. If missing for sf objects, an error is thrown.

Value

spatial object (sf) with the visual parameters added as new attributes.

Details

For sf objects, the function merges the layer and visual_param data frames based on the specified junction column. If a single junction is provided, a standard merge is performed. If two junction values are provided, the merge is done using by.x and by.y. If more than two junction values are provided, the function stops with an error. For sfc objects, the visual parameters are directly added as new attributes to the sfc object.

Examples


enhanced_sf = add_visual_param(
  layer = so_ii_hydro,
  visual_param = data.frame(
     id = 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")
)

enhanced_sf = add_visual_param(
  layer = so_ii_hydro,
  visual_param = data.frame(
     type = 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.8
  ),
  junction = "type"
)

# For sfc objects
enhanced_sfc = add_visual_param(
  layer = so_ii_limit,
  visual_param = data.frame(
     id = "perimeter",
     label = "perimeter",
     color = "white",
     fill = "transparent",
     opacity = 1
  )
)