Skip to content

Commit

Permalink
add kw args to add_source
Browse files Browse the repository at this point in the history
  • Loading branch information
walkerke committed Aug 8, 2024
1 parent 409ff48 commit 1f23a7e
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 14 deletions.
8 changes: 6 additions & 2 deletions R/sources.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#' @param map A map object created by the `mapboxgl` or `maplibre` function.
#' @param id A unique ID for the source.
#' @param data An sf object or a URL pointing to a remote GeoJSON file.
#' @param ... Additional arguments to be passed to the JavaScript addSource method.
#'
#' @return The modified map object with the new source added.
#' @export
add_source <- function(map, id, data) {
add_source <- function(map, id, data, ...) {
if (inherits(data, "sf")) {
geojson <- geojsonsf::sf_geojson(sf::st_transform(data, crs = 4326))
} else if (is.character(data) && grepl("^http", data)) {
Expand All @@ -22,13 +23,16 @@ add_source <- function(map, id, data) {
generateId = TRUE
)

# Add additional arguments
extra_args <- list(...)
source <- c(source, extra_args)

if (inherits(map, "mapboxgl_proxy") || inherits(map, "maplibre_proxy")) {
proxy_class <- if (inherits(map, "mapboxgl_proxy")) "mapboxgl-proxy" else "maplibre-proxy"
map$session$sendCustomMessage(proxy_class, list(id = map$id, message = list(type = "add_source", source = source)))
} else {
map$x$sources <- c(map$x$sources, list(source))
}

return(map)
}

Expand Down
19 changes: 14 additions & 5 deletions inst/htmlwidgets/mapboxgl.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,11 +123,20 @@ HTMLWidgets.widget({
});
} else if (source.type === "geojson") {
const geojsonData = source.data;
map.addSource(source.id, {
type: 'geojson',
data: geojsonData,
generateId: true
});
const sourceOptions = {
type: 'geojson',
data: geojsonData,
generateId: source.generateId
};

// Add additional options
for (const [key, value] of Object.entries(source)) {
if (!['id', 'type', 'data', 'generateId'].includes(key)) {
sourceOptions[key] = value;
}
}

map.addSource(source.id, sourceOptions);
} else if (source.type === "raster") {
if (source.url) {
map.addSource(source.id, {
Expand Down
21 changes: 15 additions & 6 deletions inst/htmlwidgets/maplibregl.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,21 @@ HTMLWidgets.widget({
url: source.url
});
} else if (source.type === "geojson") {
const geojsonData = source.geojson;
map.addSource(source.id, {
type: 'geojson',
data: geojsonData,
generateId: true
});
const geojsonData = source.data;
const sourceOptions = {
type: 'geojson',
data: geojsonData,
generateId: source.generateId
};

// Add additional options
for (const [key, value] of Object.entries(source)) {
if (!['id', 'type', 'data', 'generateId'].includes(key)) {
sourceOptions[key] = value;
}
}

map.addSource(source.id, sourceOptions);
} else if (source.type === "raster") {
if (source.url) {
map.addSource(source.id, {
Expand Down
4 changes: 3 additions & 1 deletion man/add_source.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 1f23a7e

Please sign in to comment.