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

refactor: move {raster} and {terra} to Suggests #943

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
Open
5 changes: 0 additions & 5 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,5 @@ jobs:
uses: rstudio/shiny-workflows/.github/workflows/website.yaml@v1
routine:
uses: rstudio/shiny-workflows/.github/workflows/routine.yaml@v1
with:
node-version: '12'
R-CMD-check:
uses: rstudio/shiny-workflows/.github/workflows/R-CMD-check.yaml@v1
with:
minimum-r-version: "3.5.0"
ubuntu: "ubuntu-20.04"
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ Imports:
magrittr,
methods,
png,
raster (>= 3.6.3),
rlang,
RColorBrewer,
scales (>= 1.0.0),
Expand All @@ -71,12 +70,13 @@ Suggests:
maps,
purrr,
R6,
raster (>= 3.6-3),
RJSONIO,
rmarkdown,
s2,
shiny (>= 1.0.0),
sp,
terra,
terra (>= 1.6-3),
testthat (>= 3.0.0)
Config/testthat/edition: 3
Config/Needs/website: dplyr, ncdf4, rnaturalearth, tidyverse/tidytemplate
Expand Down
6 changes: 3 additions & 3 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# leaflet (development version)

* `{leaflet}` no longer install sp by default and attempts to convert object to sf internally before creating a map and warns when it fails conversion (@olivroy, #942).
* `{leaflet}` no longer installs `{raster}` `{terra}` by default (@olivroy, #943). If you are using leaflet interactively, you will receive a prompt to install it.

* `{leaflet}` no longer installs `{sp}` by default and attempts to convert object to `{sf}` internally before creating a map and warns when it fails conversion (@olivroy, #942).

* The `breweries91`, `atlStorms2005`, and `gadmCHE` datasets are now `{sf}` objects (@olivroy, #944).

Expand All @@ -10,8 +12,6 @@

* Updated vignettes to replace `{sp}`/`{raster}` usage with `{sf}`/`{terra}` and their corresponding examples. (@jack-davison, #928)

* Updated vignettes to replace `{sp}`/`{raster}` usage with `{sf}`/`{terra} and their corresponding examples. (@jack-davison, #928)

* `addProviderTiles()` will now error if the chosen `provider` does not match any currently loaded provider (by default, those in `providers`). This behaviour can be toggled off by setting the new `check` argument to `FALSE` (@jack-davison, #929)

# leaflet 2.2.2
Expand Down
132 changes: 67 additions & 65 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,10 +131,10 @@ hideGroup <- function(map, group) {
#'
#' @export
groupOptions <- function(map, group, zoomLevels = NULL) {
if (is.null(zoomLevels)) # Default to TRUE if nothing specified.
if (is.null(zoomLevels)) # Default to TRUE if nothing specified.
zoomLevels <- TRUE
invokeMethod(map, getMapData(map), "setGroupOptions", group,
list(zoomLevels = zoomLevels)
list(zoomLevels = zoomLevels)
)
}

Expand All @@ -155,13 +155,13 @@ groupOptions <- function(map, group, zoomLevels = NULL) {
#' @describeIn map-layers Add a tile layer to the map
#' @export
addTiles <- function(
map,
urlTemplate = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution = NULL,
layerId = NULL,
group = NULL,
options = tileOptions(),
data = getMapData(map)
map,
urlTemplate = "https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png",
attribution = NULL,
layerId = NULL,
group = NULL,
options = tileOptions(),
data = getMapData(map)
) {
options$attribution <- attribution
if (missing(urlTemplate) && is.null(options$attribution))
Expand All @@ -170,7 +170,7 @@ addTiles <- function(
"<a href=\"https://opendatacommons.org/licenses/odbl/\">ODbL</a>"
)
invokeMethod(map, data, "addTiles", urlTemplate, layerId, group,
options)
options)
}

epsg4326 <- "+proj=longlat +datum=WGS84 +no_defs"
Expand Down Expand Up @@ -226,7 +226,7 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' @param method the method used for computing values of the new, projected raster image.
#' `"bilinear"` (the default) is appropriate for continuous data,
#' `"ngb"` - nearest neighbor - is appropriate for categorical data.
#' Ignored if `project = FALSE`. See [projectRaster()] for details.
#' Ignored if `project = FALSE`. See [terra::project()] for details.
#' @param maxBytes the maximum number of bytes to allow for the projected image
#' (before base64 encoding); defaults to 4MB.
#' @param options a list of additional options, intended to be provided by
Expand All @@ -236,12 +236,12 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' @seealso [addRasterLegend()] for an easy way to add a legend for a
#' SpatRaster with a color table.
#'
#' @examples
#' \donttest{library(raster)
#' @examplesIf rlang::is_installed("terra")
#' \donttest{
#'
#' r <- raster(xmn = -2.8, xmx = -2.79, ymn = 54.04, ymx = 54.05, nrows = 30, ncols = 30)
#' values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
#' crs(r) <- CRS("+init=epsg:4326")
#' r <- terra::rast(xmin = -2.8, xmax = -2.79, ymin = 54.04, ymax = 54.05, nrows = 30, ncols = 30)
#' terra::values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
#' terra::crs(r) <- "epsg:4326"
#'
#' pal <- colorNumeric("Spectral", domain = c(0, 1000))
#' leaflet() %>% addTiles() %>%
Expand All @@ -250,18 +250,18 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
#' }
#' @export
addRasterImage <- function(
map,
x,
colors = if (is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
map,
x,
colors = if (is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {
if (inherits(x, "SpatRaster")) {
addRasterImage_SpatRaster(
Expand Down Expand Up @@ -310,7 +310,7 @@ addRasterImage <- function(
#' @param layer the layer of the raster to target
#' @param ... additional arguments to pass through to [addLegend()]
#' @seealso [addRasterImage()]
#' @examplesIf interactive()
#' @examplesIf interactive() && rlang::is_installed("terra")
#'
#' library(terra)
#'
Expand All @@ -332,6 +332,7 @@ addRasterImage <- function(
#' @export
addRasterLegend <- function(map, x, layer = 1, ...) {
stopifnot(inherits(x, "SpatRaster"))
rlang::check_installed("terra (>= 1.6-3)")
stopifnot(length(layer) == 1 && layer > 0 && layer <= terra::nlyr(x))

# might as well do this here and only once. Subsetting would otherwise have
Expand Down Expand Up @@ -387,21 +388,21 @@ addRasterLegend <- function(map, x, layer = 1, ...) {


addRasterImage_RasterLayer <- function(
map,
x,
colors = if (is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
map,
x,
colors = if (is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {


rlang::check_installed("raster")
options$opacity <- opacity
options$attribution <- attribution

Expand All @@ -425,8 +426,8 @@ addRasterImage_RasterLayer <- function(

bounds <- raster::extent(
raster::projectExtent(
raster::projectExtent(x, crs = sp::CRS(epsg3857)),
crs = sp::CRS(epsg4326)
raster::projectExtent(x, crs = raster::crs(epsg3857)),
crs = raster::crs(epsg4326)
)
)

Expand Down Expand Up @@ -464,21 +465,20 @@ addRasterImage_RasterLayer <- function(
}

addRasterImage_SpatRaster <- function(
map,
x,
colors = if (terra::is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
map,
x,
colors = if (terra::is.factor(x)[1]) "Set1" else "Spectral",
opacity = 1,
attribution = NULL,
layerId = NULL,
group = NULL,
project = TRUE,
method = c("auto", "bilinear", "ngb"),
maxBytes = 4 * 1024 * 1024,
options = gridOptions(),
data = getMapData(map)
) {
rlang::check_installed("terra (>= 1.6-3)", reason = "to use addRasterImage() for SpatRaster objects.") # for terra::has.RGB()

rlang::check_installed("terra (>= 1.6-3)")
options$opacity <- opacity
options$attribution <- attribution

Expand Down Expand Up @@ -513,12 +513,12 @@ addRasterImage_SpatRaster <- function(
epsg3857),
epsg4326)
)
## can't the above be simplified to this?
# bounds <- terra::ext(
# terra::project(
# terra::as.points(terra::ext(x), crs=terra::crs(x)),
# epsg4326)
# )
## can't the above be simplified to this?
# bounds <- terra::ext(
# terra::project(
# terra::as.points(terra::ext(x), crs=terra::crs(x)),
# epsg4326)
# )

if (project) {
# if we should project the data
Expand Down Expand Up @@ -572,6 +572,7 @@ addRasterImage_SpatRaster <- function(
#' @export
projectRasterForLeaflet <- function(x, method) {
if (inherits(x, "SpatRaster")) {
rlang::check_installed("terra (>= 1.6-3)")
if (method=="ngb") {
method = "near"
}
Expand All @@ -581,10 +582,11 @@ projectRasterForLeaflet <- function(x, method) {
method=method
)
} else {
rlang::check_installed("raster")
raster_is_factor <- raster::is.factor(x);
projected <- raster::projectRaster(
x,
raster::projectExtent(x, crs = sp::CRS(epsg3857)),
raster::projectExtent(x, crs = raster::crs(epsg3857)),
method = method
)
# if data is factor data, make the result factors as well.
Expand Down
11 changes: 7 additions & 4 deletions R/leaflet.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,14 @@ leafletSizingPolicy <- function(

#' Create a Leaflet map widget
#'
#' This function creates a Leaflet map widget using \pkg{htmlwidgets}. The
#' @description
#' * `leaflet()` creates a Leaflet map widget using \pkg{htmlwidgets}. The
#' widget can be rendered on HTML pages generated from R Markdown, Shiny, or
#' other applications.
#' * `leafletOptions()`: options for map creation
#' * `leafletCRS()`: class to create custom CRS.
#'
#' @details
#' The `data` argument is only needed if you are going to reference
#' variables in this object later in map layers. For example, `data` can be
#' a data frame containing columns `latitude` and `longitude`, then
Expand Down Expand Up @@ -152,13 +156,12 @@ mapOptions <- function(map, zoomToLimits = c("always", "first", "never")) {
#' @param minZoom Minimum zoom level of the map. Overrides any `minZoom` set on map layers.
#' @param maxZoom Maximum zoom level of the map. This overrides any `maxZoom` set on map layers.
#' @param crs Coordinate Reference System to use. Don't change this if you're not sure what it means.
#' @seealso [leafletCRS()] for creating a custom CRS.
#' @param worldCopyJump With this option enabled, the map tracks when you pan
#' to another "copy" of the world and seamlessly jumps to the original
#' one so that all overlays like markers and vector layers are still visible.
#' @param preferCanvas Whether leaflet.js Paths should be rendered on a Canvas renderer.
#' @param ... other options used for leaflet.js map creation.
#' @describeIn leaflet Options for map creation
#' @rdname leaflet
#' @seealso See <https://web.archive.org/web/20220702182250/https://leafletjs.com/reference-1.3.4.html#map-option> for details and more options.
#' @export
leafletOptions <- function(
Expand Down Expand Up @@ -200,7 +203,7 @@ crsClasses <- list("L.CRS.EPSG3857", "L.CRS.EPSG4326", "L.CRS.EPSG3395",
#' Proj4Leaflet will use this in the getSize method, otherwise
#' defaulting to Leaflet's default CRS size
#' @param tileSize DEPRECATED! Specify the tilesize in the [tileOptions()] argument.
#' @describeIn leaflet class to create a custom CRS
#' @rdname leaflet
#' @export
leafletCRS <- function(
crsClass = "L.CRS.EPSG3857",
Expand Down
2 changes: 1 addition & 1 deletion R/normalize-terra.R
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ polygonData.SpatVector <- function(obj) {

# helpers -----------------------------------------------------------------
check_crs_terra <- function(x) {
rlang::check_installed("terra")
rlang::check_installed("terra (>= 1.6-3)")
crs <- terra::crs(x)

# Don't have enough information to check
Expand Down
9 changes: 4 additions & 5 deletions inst/examples/geojson.R
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
library(leaflet)
library(sp)
#' <br/><br/>
#' The V8 part is simply to read the JSON embeded in the Javascript.<br/>
#' For a geojson file `jsonlite::fromfromJSON()` or `geojsonio::regeojson_read()` will do
#' For a geojson file `jsonlite::fromJSON()` or `geojsonio::geojson_read()` will do
#'
jsURL <- "https://rawgit.com/Norkart/Leaflet-MiniMap/master/example/local_pubs_restaurant_norway.js"
v8 <- V8::v8()
v8$source(jsURL)
geoJson <- geojsonio::as.json(v8$get("pubsGeoJSON"))

# This is the kicker, convert geojson to a Spatial object.
# This is the kicker, convert geojson to a sf object.
# This then allows us to use formulas in our markers, polygons etc.
spdf <- geojsonio::geojson_sp(geoJson)
spdf <- geojsonio::geojson_sf(geoJson)

icons <- awesomeIconList(
pub = makeAwesomeIcon(icon = "glass", library = "fa", markerColor = "red"),
Expand All @@ -34,7 +33,7 @@ leaflet() %>%
#' Another examples this time with polygons
url <- "https://www.partners-popdev.org/wp-content/themes/original-child/vendor/Geojson/States/Maharashtra.geojson"

mhSPDF <- geojsonio::geojson_read(url, what = "sp")
mhSPDF <- sf::st_read(url)

cols <- colorFactor(topo.colors(nrow(mhSPDF)), mhSPDF$NAME_2)

Expand Down
1 change: 0 additions & 1 deletion inst/examples/leaflet.R
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,3 @@ m %>% addCircleMarkers(~lng, ~lat, radius = ~size,
color = ~greens(value), fillOpacity = 0.5)

}

1 change: 1 addition & 0 deletions inst/examples/normalize.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# obsolete
library(leaflet)
library(sp)
library(maps)
Expand Down
4 changes: 2 additions & 2 deletions inst/examples/proj4Leaflet-PolarProjections.R
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ resolutions <- c(8192, 4096, 2048, 1024, 512, 256)
zoom <- 0
maxZoom <- 5

border <- geojsonio::geojson_read(system.file("examples/Seamask_medium_res_polygon.kml", package = "leaflet"), what = "sp")
points <- geojsonio::geojson_read(system.file("examples/Historic_sites_and_monuments_point.kml", package = "leaflet"), what = "sp")
border <- sf::st_read(system.file("examples/Seamask_medium_res_polygon.kml", package = "leaflet"))
points <- sf::st_read(system.file("examples/Historic_sites_and_monuments_point.kml", package = "leaflet"))

crsAntartica <- leafletCRS(
crsClass = "L.Proj.CRS",
Expand Down
Loading
Loading