Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update plot_map function for simple map plots using leaflet and mapview #291

Merged
merged 8 commits into from
Dec 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,19 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased](https://github.com/openghg/openghg/compare/0.1.1...HEAD)

### Fixed

- Added a function to ensure correct polygon display across the dateline - [PR #285](https://github.com/4DModeller/fdmr/pull/285)

### Added

- Added a new tutorial on loading data from different sources - [PR #266](https://github.com/4DModeller/fdmr/pull/266/)
- Added new geophysical processes tutorial thanks to Alexander Minakov (4minakov) - [PR #257](https://github.com/4DModeller/fdmr/pull/257)
- Added mouse pointer coordinates header and standard measurement tool - [PR #260](https://github.com/4DModeller/fdmr/pull/260)
- New plotting function `plot_map_mapview` to use `mapview` to plot raster and spatial data - [#291](https://github.com/4DModeller/fdmr/pull/291)

### Changed

- Updated `plot_map` to allow use of both `leaflet` and `mapview` packages - [#291](https://github.com/4DModeller/fdmr/pull/291)

## [0.1.1] - 2023-11-01

Expand Down
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Imports:
bsicons,
future,
leafem,
sfheaders
sfheaders,
mapview
Suggests:
bookdown,
knitr,
Expand Down
116 changes: 102 additions & 14 deletions R/plot_mapping.R
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
#' Create a simple Leaflet map from data
#' Create a simple map from data using either the leaflet or mapview packages
#'
#' NOTE that the mapview backend is only intended for quick viewing of data,
#' most of the customisation arguments are not available.
#'
#' The leaflet backend will work for most use cases and is recommended.
#' For plotting of maps with UTM coordinates, the mapview backend is recommended.
#'
#' @param polygon_data Polygon data
#' @param raster_data Raster datas
Expand All @@ -13,8 +19,10 @@
#' @param polygon_line_weight Polygon surrounding line weight
#' @param reverse Reverse the colour palette if TRUE
#' @param wrapping Split polygons along the antimeridian (-180/180 boundary) if TRUE
#' @param backend Backend package to use for plotting, either "leaflet" or "mapview"
#'
#' @return leaflet::leaflet or mapview::mapview
#'
#' @return leaflet::leaflet
#' @export
plot_map <- function(polygon_data = NULL,
raster_data = NULL,
Expand All @@ -28,14 +36,73 @@ plot_map <- function(polygon_data = NULL,
polygon_line_weight = 1,
polygon_fill_opacity = 0.6,
reverse = FALSE,
wrapping = FALSE) {
wrapping = FALSE,
backend = "leaflet") {
if (is.null(polygon_data) && is.null(raster_data)) {
stop("Polygon or raster data must be given.")
}
library(leaflet)

if (backend == "leaflet") {
plot_map_leaflet(
polygon_data = polygon_data,
raster_data = raster_data,
domain = domain,
markers = markers,
palette = palette,
legend_title = legend_title,
add_scale_bar = add_scale_bar,
polygon_fill_colour = polygon_fill_colour,
polygon_line_colour = polygon_line_colour,
polygon_line_weight = polygon_line_weight,
polygon_fill_opacity = polygon_fill_opacity,
reverse = reverse,
wrapping = wrapping
)
} else if (backend == "mapview") {
plot_map_mapview(
polygon_data = polygon_data,
raster_data = raster_data
)
} else {
stop("Invalid backend given, must be either 'leaflet' or 'mapview'.")
}
}


#' Create a simple Leaflet map from data
#'
#' @param polygon_data Polygon data
#' @param raster_data Raster data
#' @param domain Domain data to be passed to leaflet::colorNumeric and leaflet::addLegend
#' @param markers Markers to display on map. A named list with latitude, longitude and label names must be given.
#' @param palette Palette to be used for colours, defaults to viridis
#' @param legend_title Title for legend
#' @param add_scale_bar Add scale bar if TRUE
#' @param polygon_fill_opacity Leaflet polygon fill opacity, float from 0 to 1.0, passed to fillOpacity of leaflet::addPolygons
#' @param polygon_fill_colour Polygon fill colour
#' @param polygon_line_colour Polygon surrounding line colour
#' @param polygon_line_weight Polygon surrounding line weight
#' @param reverse Reverse the colour palette if TRUE
#' @param wrapping Split polygons along the antimeridian (-180/180 boundary) if TRUE
#'
#' @return leaflet::leaflet
#' @keywords internal
plot_map_leaflet <- function(polygon_data = NULL,
raster_data = NULL,
domain = NULL,
markers = NULL,
palette = "viridis",
legend_title = NULL,
add_scale_bar = FALSE,
polygon_fill_colour = "#E4572E",
polygon_line_colour = "grey",
polygon_line_weight = 1,
polygon_fill_opacity = 0.6,
reverse = FALSE,
wrapping = FALSE) {
m <- leaflet::leaflet()
m <- leaflet::addTiles(m)
m <- leaflet::addProviderTiles(m, leaflet::providers$Esri.WorldImagery, group = "Satellite")
m <- leaflet::addProviderTiles(m, leaflet::providers$Openstreetmap, group = "Satellite")
m <- leafem::addMouseCoordinates(m, native.crs = TRUE)

# Store a vector of layers we add to the map,
Expand All @@ -45,7 +112,7 @@ plot_map <- function(polygon_data = NULL,
if (!is.null(polygon_data)) {
if (isTRUE(wrapping)) {
polygon_data <- fdmr::antimeridian_wrapping(polygon_data, crs = "+proj=longlat +datum=WGS84", unique_inst = TRUE, to_sp = FALSE)
}
}
if (!is.null(domain)) {
colours <- leaflet::colorNumeric(palette = palette, domain = domain, reverse = reverse)
polygon_fill_colour <- ~ colours(domain)
Expand All @@ -69,7 +136,7 @@ plot_map <- function(polygon_data = NULL,
}

if (!is.null(raster_data)) {
colours <- leaflet::colorNumeric(palette = palette, domain = raster::values(raster_data), na.color=rgb(0,0,0,0), reverse = reverse)
colours <- leaflet::colorNumeric(palette = palette, domain = raster::values(raster_data), na.color = rgb(0, 0, 0, 0), reverse = reverse)
m <- leaflet::addRasterImage(m,
x = raster_data,
opacity = 0.75,
Expand All @@ -78,11 +145,11 @@ plot_map <- function(polygon_data = NULL,
colors = colours,
)
m <- leaflet::addLegend(m,
pal = colours,
values = raster::values(raster_data),
opacity = 0.75,
title = legend_title,
na.label = ""
pal = colours,
values = raster::values(raster_data),
opacity = 0.75,
title = legend_title,
na.label = ""
)
layers <- append(layers, "Raster")
}
Expand All @@ -103,7 +170,28 @@ plot_map <- function(polygon_data = NULL,
m <- leaflet::addScaleBar(m, position = "bottomleft")
}

m <- leaflet::addMeasure(m, position = "bottomleft", primaryLengthUnit = 'kilometers', primaryAreaUnit = 'sqmeters')

m <- leaflet::addMeasure(m, position = "bottomleft", primaryLengthUnit = "kilometers", primaryAreaUnit = "sqmeters")

return(m)
}


#' A simple map plotter using mapview. This is only intended for very quick viewing of data.
#'
#' @param polygon_data Spatial data to plot
#' @param raster_data Raster data to plot
#'
#' @return mapview::mapview
#' @keywords internal
plot_map_mapview <- function(polygon_data = NULL, raster_data = NULL) {
map_types <- c("OpenStreetMap", "Esri.WorldImagery", "OpenTopoMap")

m <- mapview::mapview(map.types = map_types)
if (!is.null(polygon_data)) {
m <- m + mapview::mapview(polygon_data)
}
if (!is.null(raster_data)) {
m <- m + mapview::mapview(raster_data)
}
return(m)
}
4 changes: 3 additions & 1 deletion _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ reference:
desc: Functions to help convert coordinates and units
contents:
- latlong_to_utm
- to_dates
- antimeridian_wrapping
- convert_from_lon_360
- title: Utility functions
desc: These help us handle paths, retrieve data etc
contents:
- to_dates
- retrieve_tutorial_data
- get_tutorial_datapath
- load_tutorial_data
Expand Down
22 changes: 18 additions & 4 deletions man/plot_map.Rd

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

56 changes: 56 additions & 0 deletions man/plot_map_leaflet.Rd

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

20 changes: 20 additions & 0 deletions man/plot_map_mapview.Rd

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

6 changes: 5 additions & 1 deletion man/plot_mesh.Rd

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

Loading