-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New editOptions= argument for lower-level toolbar customization
The new `editOptions=` argument takes a user-supplied list of named options that are ultimately passed on to either `leafpm::addPmToolbar()` or `leaflet.extras::addDrawToolbar()`, depending on the value of the `editor=` argument. When `editor = "leafpm"`, the list can consist of one or more elements with names `"toolbarOptions"`, `"drawOptions"`, `"editOptions"`, and `"cutOptions"`. For details, see `?leafpm::addPmToolbar`. When `editor = "leaflet.extras"`, allowable names for list elements are `"polylineOptions`, `"polygonOptions"`, `"circleOptions"`, `"rectangleOptions"`, `"makerOptions"`, `"circleMarkerOptions"`, and `"editOptions"`. For details, see `?leaflet.extras::addDrawToolbar`. Currently, there is no checking or validation of the list passed in to `editorOptions=`, so users will need to take particular care that the list's structure (including the names of all of its elements) match with what is expected by the `leafpm::addPmToolbar()` or `leaflet.extras::addDrawToolbar()` functions. Here are few simple examples demonstrating the new argument's usage: ```r library(sf) library(mapedit) x <- list(matrix(c(11,0,11,1,12,1,12,0,11,0), ncol = 2, byrow = TRUE)) pp <- st_sf(geom = st_sfc(st_polygon(x)), crs = 4326) optsA <- list(drawOptions = list(snappable = FALSE, hintlineStyle = list(color = "red", opacity = 0.5), templineStyle = list(color = "red")), editOptions = list(snappable = FALSE)) x <- editFeatures(pp, editor = "leafpm", editorOptions = optsA) optsB <- list(editOptions = list(remove = FALSE), circleOptions = FALSE, markerOptions = FALSE, circleMarkerOptions = FALSE, rectangleOptions = FALSE) x <- editFeatures(pp, editor = "leaflet.extras", editorOptions = optsB) ```
- Loading branch information
1 parent
90fd5e6
commit 9f705ee
Showing
8 changed files
with
204 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
|
||
|
||
##' @title Prepare arguments for addDrawToolbar or addPmToolbar | ||
##' @param fun Function used by editor package (leafpm or | ||
##' leaflet.extras) to set defaults | ||
##' @param args Either a (possibly nested) list of named options of | ||
##' the form suitable for passage to \code{fun} or (if the chosen | ||
##' editor is \code{"leaflet.extras"}) \code{FALSE}. | ||
##' @return An object suitable for passing in as the supplied argument | ||
##' to one of the formals of either for passage to either | ||
##' \code{leaflet.extras::addDrawToolbar} or | ||
##' \code{leafpm::addPmToolbar}. | ||
processOpts <- function(fun, args) { | ||
## Account for special meaning of `FALSE` as arg in leaflet.extras | ||
if(isFALSE(args)) { | ||
return(FALSE) | ||
} else { | ||
return(do.call(fun, args)) | ||
} | ||
} | ||
|
||
|
||
##' @title Add a (possibly customized) toolbar to a leaflet map | ||
##' @param leafmap leaflet map to use for Selection | ||
##' @param editorOptions A list of options to be passed on to either | ||
##' \code{leaflet.extras::addDrawToolbar} or | ||
##' \code{leafpm::addPmToolbar}. | ||
##' @param editor Character string giving editor to be used for the | ||
##' current map. Either \code{"leafpm"} or | ||
##' \code{"leaflet.extras"}. | ||
##' @param targetLayerId \code{string} name of the map layer group to | ||
##' use with edit | ||
##' @return The leaflet map supplied to \code{leafmap}, now with an | ||
##' added toolbar. | ||
addToolbar <- function(leafmap, editorOptions, editor, | ||
targetLayerId) { | ||
## Set up this package's defaults | ||
if (editor == "leafpm") { | ||
if(any(sapply(leafmap$x$calls, "[[", "method") %in% | ||
c("addPolylines", "addPolygons"))) { | ||
editorDefaults <- | ||
list(toolbarOptions = list(drawCircle = FALSE), | ||
drawOptions = list(allowSelfIntersection = FALSE), | ||
editOptions = list(allowSelfIntersection = FALSE), | ||
cutOptions = list(allowSelfIntersection = FALSE)) | ||
} else { | ||
editorDefaults <- | ||
list(toolbarOptions = list(drawCircle = FALSE), | ||
drawOptions = list(), | ||
editOptions = list(), | ||
cutOptions = list()) | ||
} | ||
} | ||
if (editor == "leaflet.extras") { | ||
editorDefaults <- | ||
list(polylineOptions = list(repeatMode = TRUE), | ||
polygonOptions = list(repeatMode = TRUE), | ||
circleOptions = FALSE, | ||
rectangleOptions = list(repeatMode = TRUE), | ||
markerOptions = list(repeatMode = TRUE), | ||
circleMarkerOptions = list(repeatMode = TRUE), | ||
editOptions = list()) | ||
} | ||
|
||
## Apply user-supplied options, if any | ||
editorArgs <- modifyList(editorDefaults, editorOptions) | ||
|
||
|
||
## Add toolbar to leafmap object | ||
if (editor == "leaflet.extras") { | ||
leaflet.extras::addDrawToolbar( | ||
leafmap, | ||
targetGroup = targetLayerId, | ||
polylineOptions = | ||
processOpts(leaflet.extras::drawPolylineOptions, | ||
editorArgs[["polylineOptions"]]), | ||
polygonOptions = | ||
processOpts(leaflet.extras::drawPolygonOptions, | ||
editorArgs[["polygonOptions"]]), | ||
circleOptions = | ||
processOpts(leaflet.extras::drawCircleOptions, | ||
editorArgs[["circleOptions"]]), | ||
rectangleOptions = | ||
processOpts(leaflet.extras::drawRectangleOptions, | ||
editorArgs[["rectangleOptions"]]), | ||
markerOptions = | ||
processOpts(leaflet.extras::drawMarkerOptions, | ||
editorArgs[["markerOptions"]]), | ||
circleMarkerOptions = | ||
processOpts(leaflet.extras::drawCircleMarkerOptions, | ||
editorArgs[["circleMarkerOptions"]]), | ||
editOptions = | ||
processOpts(leaflet.extras::editToolbarOptions, | ||
editorArgs[["editOptions"]]) | ||
) | ||
} else if (editor == "leafpm") { | ||
leafpm::addPmToolbar( | ||
leafmap, | ||
targetGroup = targetLayerId, | ||
toolbarOptions = processOpts(leafpm::pmToolbarOptions, | ||
editorArgs[["toolbarOptions"]]), | ||
drawOptions = processOpts(leafpm::pmDrawOptions, | ||
editorArgs[["drawOptions"]]), | ||
editOptions = processOpts(leafpm::pmEditOptions, | ||
editorArgs[["editOptions"]]), | ||
cutOptions = processOpts(leafpm::pmCutOptions, | ||
editorArgs[["cutOptions"]]) | ||
) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.