-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add interactive mapping helper functions
1 parent
b11fa0f
commit ca4d006
Showing
8 changed files
with
292 additions
and
0 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
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
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,107 @@ | ||
#' Use mapview to interactively explore spatial data | ||
#' | ||
#' A wrapper for [mapview::mapview()]. x can be an sf object or sf list. If nm is | ||
#' NULL and x is an sf list the first item in the list is used. | ||
#' | ||
#' @inheritParams mapview::mapview | ||
#' @inheritDotParams mapview::mapview | ||
#' @param remove_na If TRUE and col is not `NULL`, filter `NA` values from the col | ||
#' before passing to [mapview::mapview()] | ||
#' @inheritParams make_img_leafpop | ||
#' @seealso | ||
#' [mapview::mapview()] | ||
#' @export | ||
#' @importFrom dplyr filter | ||
mapview_ext <- function(x, zcol = NULL, remove_na = FALSE, ...) { | ||
check_installed("mapview") | ||
|
||
if (!inherits(x, "sf")) { | ||
return(mapview::mapview(x, ...)) | ||
} | ||
|
||
nm <- names(x)[unlist(lapply(x, is.list))] | ||
|
||
if (length(nm) > 1) { | ||
nm <- nm[nm != attr(x, "sf_column")] | ||
cli::cli_alert( | ||
"Dropping list columns: {.val {nm}}" | ||
) | ||
x <- x[, !(names(x) %in% nm)] | ||
} | ||
|
||
if (remove_na && !is_null(zcol)) { | ||
x <- x[!is.na(x[[zcol]]), ] | ||
} | ||
|
||
mapview::mapview( | ||
x = x, | ||
zcol = zcol, | ||
... | ||
) | ||
} | ||
|
||
#' @rdname mapview_ext | ||
#' @name mapview_exif | ||
#' @export | ||
#' @inheritParams read_sf_exif | ||
mapview_exif <- function(path = NULL, | ||
fileext = "jpeg", | ||
popup = TRUE, | ||
photos = NULL, | ||
...) { | ||
photos <- photos %||% read_sf_exif( | ||
path = path, | ||
fileext = fileext, | ||
... | ||
) | ||
|
||
make_img_leafpop( | ||
images = photos, | ||
popup = popup | ||
) | ||
} | ||
|
||
#' @param popup If `TRUE`, add a popup image to a leaflet map; defaults `TRUE`. | ||
#' @rdname mapview_ext | ||
#' @param images A simple feature object with columns for the image path/url, image width, and image height. | ||
#' @name make_img_leafpop | ||
#' @export | ||
make_img_leafpop <- function(images, | ||
popup = TRUE) { | ||
check_installed("leaflet") | ||
check_installed("leafpop") | ||
|
||
stopifnot( | ||
all(has_name(images, c("img_width", "img_height"))), | ||
any(has_name(images, c("path", "img_url"))), | ||
is_sf(images) | ||
) | ||
|
||
leaflet_map <- | ||
leaflet::addCircleMarkers( | ||
leaflet::addTiles(leaflet::leaflet()), | ||
data = images, | ||
group = "images" | ||
) | ||
|
||
if (has_name(images, "img_url")) { | ||
image <- images$img_url | ||
} else if (has_name(images, "path")) { | ||
image <- images$path | ||
} | ||
|
||
width <- images$img_width | ||
height <- images$img_height | ||
|
||
if (!popup) { | ||
return(leaflet_map) | ||
} | ||
|
||
leafpop::addPopupImages( | ||
map = leaflet_map, | ||
image = image, | ||
width = width, | ||
height = height, | ||
group = "images" | ||
) | ||
} |
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,41 @@ | ||
#' rdeck editor | ||
#' | ||
#' @inheritParams rdeck::rdeck | ||
#' @inheritDotParams rdeck::rdeck | ||
#' @export | ||
rdeck_edit <- function(features, mode = rdeck::cur_value(), initial_bounds = NULL, ...) { | ||
check_required(features) | ||
features <- st_wgs84(features) | ||
check_installed("rdeck") | ||
|
||
rdeck::rdeck( | ||
initial_bounds = initial_bounds %||% sf::st_bbox(features), | ||
editor = rdeck::editor_options(features = features, mode = mode), | ||
... | ||
) | ||
} | ||
|
||
#' @export | ||
#' @rdname rdeck_edit | ||
rdeck_select <- function(features, ..., mode = "select") { | ||
rdeck_edit(features, mode = mode, ...) | ||
} | ||
|
||
#' Editor options with CRS conversion | ||
#' | ||
#' A wrapper for [rdeck::editor_options()] that automatically converts features | ||
#' to WGS84. | ||
#' | ||
#' @name rdeck_editor_options | ||
#' @rdname rdeck_edit | ||
#' @inheritParams rdeck::editor_options | ||
#' @export | ||
editor_options <- function(mode = rdeck::cur_value(), | ||
features = rdeck::cur_value()) { | ||
check_installed("rdeck") | ||
if (!is_wgs84(features)) { | ||
features <- st_wgs84(features) | ||
} | ||
|
||
rdeck::editor_options(mode, features) | ||
} |
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.