addFgb {leafem} | R Documentation |
Add a flatgeobuf file to leaflet map
Description
flatgeobuf is a performant binary geo-spatial file format suitable for
serving large data. For more details see
https://github.com/flatgeobuf/flatgeobuf and the respective
documentation for the GDAL/OGR driver at
https://gdal.org/en/latest/drivers/vector/flatgeobuf.html.
In contrast to classical ways of serving data from R onto a leaflet map,
flatgeobuf can stream the data chunk by chunk so that rendering of the map
is more or less instantaneous. The map is responsive while data is still
loading so that popup queries, zooming and panning will work even
though not all data has been rendered yet. This makes for a rather pleasant
user experience as we don't have to wait for all data to be added to the map
before interacting with it.
Usage
addFgb(
map,
file = NULL,
url = NULL,
layerId = NULL,
group = NULL,
popup = NULL,
label = NULL,
radius = 10,
stroke = TRUE,
color = "#03F",
weight = 5,
opacity = 0.5,
fill = FALSE,
fillColor = NULL,
fillOpacity = 0.2,
dashArray = NULL,
options = NULL,
className = NULL,
scale = scaleOptions(),
minZoom = NULL,
maxZoom = 52,
highlightOptions = NULL,
labelOptions = NULL,
...
)
Arguments
map |
a mapview or leaflet object. |
file |
file path to the .fgb file to be added to |
url |
url of the data to be added to |
layerId |
the layer id. |
group |
the group name for the file to be added to |
popup |
either a logical of whether to show the feature properties (fields) in popups or the name of the field to show in popups. |
label |
name of the field to be shown as a tooltip. |
radius |
the size of the circle markers. |
stroke |
whether to draw stroke along the path (e.g. the borders of polygons or circles). |
color |
stroke color. |
weight |
stroke width in pixels. |
opacity |
stroke opacity. |
fill |
whether to fill the path with |
fillColor |
fill color. If set, |
fillOpacity |
fill opacity. |
dashArray |
a string that defines the stroke dash pattern. |
options |
a list of extra options for tile layers, popups, paths (circles, rectangles, polygons, ...), or other map elements. |
className |
optional class name for the popup (table). Can be used to define css for the popup. |
scale |
named list with instructions on how to scale radius, width, opacity, fillOpacity if those are to be mapped to an attribute column. |
minZoom |
minimum zoom level at which data should be rendered. |
maxZoom |
maximum zoom level at which data should be rendered. |
highlightOptions |
Options for highlighting the shape on mouse over. |
labelOptions |
A Vector of |
... |
currently not used. |
Details
Styling options in addFgb
offer flexibility by allowing
users to either specify styles directly as function arguments or define them
as attributes in the data object:
-
Direct Styling: You can pass style arguments (e.g.,
color
,weight
,opacity
) directly to the function. These will apply uniformly to all features in the layer. -
Attribute-based Styling: Alternatively, you can include styling properties (e.g.,
color
,fillColor
,weight
) as columns in your data object before writing it to an FGB file. Set the corresponding arguments inaddFgb
toNULL
, and the function will use these attributes for styling during map rendering.For example:
## using custom `color` data$color <- colorNumeric(palette = "viridis", domain = data$var)(data$var) sf::st_write(obj = data, dsn = "myfile.fgb", driver = "FlatGeobuf") leafem::addFgb(file = "myfile.fgb", color = NULL) ## using custom `fillColor` data$fillColor <- colorNumeric(palette = "viridis", domain = data$var)(data$var) sf::st_write(obj = data, dsn = "myfile.fgb", driver = "FlatGeobuf") leafem::addFgb(file = "myfile.fgb", fill = TRUE, fillColor = NULL)
Examples
if (interactive()) {
library(leaflet)
library(leafem)
# via URL
url = "https://raw.githubusercontent.com/flatgeobuf/flatgeobuf/3.0.1/test/data/UScounties.fgb"
leaflet() %>%
addTiles() %>%
leafem:::addFgb(
url = url
, group = "counties"
, label = "NAME"
, popup = TRUE
, fill = TRUE
, fillColor = "blue"
, fillOpacity = 0.6
, color = "black"
, weight = 1
) %>%
addLayersControl(overlayGroups = c("counties")) %>%
addMouseCoordinates() %>%
setView(lng = -105.644, lat = 51.618, zoom = 3)
}