Skip to content

Commit 668b6c0

Browse files
authored
Merge pull request #837 from rstudio/remove-retired-dependencies
Remove outdated dependencies; use testthat-3e
2 parents 51daa69 + 6b7af0b commit 668b6c0

17 files changed

+185
-103
lines changed

DESCRIPTION

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Imports:
4343
methods,
4444
png,
4545
RColorBrewer,
46-
raster,
46+
raster (>= 3.6.3),
4747
scales (>= 1.0.0),
4848
sp,
4949
stats,
@@ -54,14 +54,13 @@ Suggests:
5454
maps,
5555
sf (>= 0.9-6),
5656
shiny,
57-
rgdal,
58-
rgeos,
5957
R6,
6058
RJSONIO,
6159
purrr,
6260
testthat (>= 3.0.0),
6361
s2,
6462
terra
65-
RoxygenNote: 7.2.1
63+
RoxygenNote: 7.2.3
6664
Encoding: UTF-8
6765
LazyData: true
66+
Config/testthat/edition: 3

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ BUG FIXES and IMPROVEMENTS
88

99
* Use `xfun::base64_uri()` for base64 encoding instead of **markdown** and **base64enc**. (#823)
1010

11+
* Remove dependencies on rgdal and rgeos. (#837)
12+
1113
leaflet 2.1.1
1214
--------------------------------------------------------------------------------
1315

R/layers.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,9 @@ epsg3857 <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y
232232
#' values(r) <- matrix(1:900, nrow(r), ncol(r), byrow = TRUE)
233233
#' crs(r) <- CRS("+init=epsg:4326")
234234
#'
235-
#' if (requireNamespace("rgdal")) {
236-
#' leaflet() %>% addTiles() %>%
237-
#' addRasterImage(r, colors = "Spectral", opacity = 0.8)
238-
#' }}
235+
#' leaflet() %>% addTiles() %>%
236+
#' addRasterImage(r, colors = "Spectral", opacity = 0.8)
237+
#' }
239238
#' @export
240239
addRasterImage <- function(
241240
map,

R/normalize-sp.R

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,29 @@ to_multipolygon_list.SpatialPolygons <- function(pgons) {
9090
to_multipolygon.Polygons <- function(pgons) {
9191
if (length(pgons@Polygons) > 1) {
9292
# If Polygons contains more than one Polygon, then we may be dealing with
93-
# a polygon with holes or a multipolygon (potentially with holes). Use
94-
# createPolygonsComment to validate and determine what the situation is.
93+
# a polygon with holes or a multipolygon (potentially with holes). We used
94+
# to use rgeos::createPolygonsComment, but rgeos has been deprecated, so now
95+
# we use sf.
9596
comment <- comment(pgons)
96-
if (is.null(comment) || comment == "FALSE")
97-
comment <- rgeos::createPolygonsComment(pgons)
97+
if (is.null(comment) || comment == "FALSE") {
98+
if (any(vapply(pgons@Polygons, methods::slot, logical(1), "hole"))) {
99+
if (!require("sf")) {
100+
stop("You attempted to use an sp Polygons object that is missing hole ",
101+
"information. Leaflet can use the {sf} package to infer hole ",
102+
"assignments, but it is not installed. Please install the {sf} ",
103+
"package, and try the operation again.")
104+
} else if (packageVersion("sf") < "1.0.10") {
105+
stop("You attempted to use an sp Polygons object that is missing hole ",
106+
"information. Leaflet can use the {sf} package to infer hole ",
107+
"assignments, but only with sf v1.0-10 and above. Please upgrade ",
108+
"the {sf} package, and try the operation again.")
109+
}
110+
x <- to_multipolygon_list(sf::st_geometry(sf::st_as_sf(SpatialPolygons(list(pgons)))))
111+
return(x[[1]])
112+
} else {
113+
comment <- paste(collapse = " ", rep_len("0", length(pgons@Polygons)))
114+
}
115+
}
98116
pstatus <- as.integer(strsplit(comment, " ")[[1]])
99117
lapply(which(pstatus == 0L), function(index) {
100118
# Return a list of rings, exterior first

R/staticimports.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,11 +81,9 @@ system_file <- function(..., package = "base") {
8181
normalizePath(files, winslash = "/")
8282
}
8383

84-
# A wrapper for `system.file()`, which caches the results, because
85-
# `system.file()` can be slow. Note that because of caching, if
86-
# `system_file_cached()` is called on a package that isn't installed, then the
87-
# package is installed, and then `system_file_cached()` is called again, it will
88-
# still return "".
84+
# A wrapper for `system.file()`, which caches the package path because
85+
# `system.file()` can be slow. If a package is not installed, the result won't
86+
# be cached.
8987
system_file_cached <- local({
9088
pkg_dir_cache <- character()
9189

@@ -97,7 +95,9 @@ system_file_cached <- local({
9795
not_cached <- is.na(match(package, names(pkg_dir_cache)))
9896
if (not_cached) {
9997
pkg_dir <- system.file(package = package)
100-
pkg_dir_cache[[package]] <<- pkg_dir
98+
if (nzchar(pkg_dir)) {
99+
pkg_dir_cache[[package]] <<- pkg_dir
100+
}
101101
} else {
102102
pkg_dir <- pkg_dir_cache[[package]]
103103
}

docs/raster.Rmd

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,31 @@ pagetitle: Leaflet for R - Raster Images
44

55
## Raster Images
66

7-
Two-dimensional `RasterLayer` objects (from the [`raster` package](http://CRAN.R-project.org/package=raster)) can be turned into images and added to Leaflet maps using the `addRasterImage` function.
7+
Two-dimensional `SpatRaster` objects (from the [`terra` package](https://CRAN.R-project.org/package=terra)) or `RasterLayer` objects (from the [`raster` package](https://CRAN.R-project.org/package=raster)) can be turned into images and added to Leaflet maps using the `addRasterImage` function.
88

9-
The `addRasterImage` function works by projecting the `RasterLayer` object to [EPSG:3857](http://spatialreference.org/ref/sr-org/7483/) and encoding each cell to an RGBA color, to produce a PNG image. That image is then embedded in the map widget.
9+
The `addRasterImage` function works by projecting the `SpatRaster` or `RasterLayer` object to [EPSG:3857](http://spatialreference.org/ref/sr-org/7483/) and encoding each cell to an RGBA color, to produce a PNG image. That image is then embedded in the map widget.
1010

11-
It's important that the `RasterLayer` object is tagged with a proper coordinate reference system. Many raster files contain this information, but some do not. Here is how you'd tag a raster layer object "`r`" which contains WGS84 data:
11+
It's important that the raster object is tagged with a proper coordinate reference system. Many raster files contain this information, but some do not. Here is how you'd tag a raster object "`r`" which contains WGS84 data:
1212

1313
```r
14-
crs(r) <- sp::CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
14+
crs(r) <- "+proj=longlat +datum=WGS84"
1515
```
1616

1717
### Large Raster Warning
1818

1919
Because the `addRasterImage` function embeds the image in the map widget, it will increase the size of the generated HTML proportionally. In order to avoid unacceptable download times and memory usage, `addRasterImage` will error when the PNG is beyond the size indicated by the `maxBytes` argument (defaults to 4 megabytes).
2020

21-
If you have a large raster layer, you can provide a larger number of bytes and see how it goes, or use `raster::resample` or `raster::aggregate` to decrease the number of cells.
21+
If you have a large raster layer, you can provide a larger number of bytes and see how it goes, or use `terra::resample`, `raster::resample`, or `raster::aggregate` to decrease the number of cells.
2222

2323
### Projection Performance
2424

25-
The `addRasterImage` function projects using `raster::projectRaster`, which can take a while on all but the smallest rasters. To improve performance, the first thing to do is install a new version of `raster`; version 2.4 includes optimizations that speed up bilinear projection by about 10X. This version has not yet been released to CRAN at the time of this writing (June 17, 2015) but can be installed directly from R-Forge:
26-
27-
```r
28-
install.packages('raster', repos = 'http://r-forge.r-project.org/', type = 'source')
29-
```
30-
31-
If you have a large raster layer or expect to call `addRasterImage` on the same raster layer many times, you can perform the [EPSG:3857](http://spatialreference.org/ref/sr-org/7483/) projection yourself (either using `leaflet::projectRasterForLeaflet` or using another GIS library or program) and call `addRasterImage` with `project = FALSE`.
25+
The `addRasterImage` function projects using `terra::project` or `raster::projectRaster`, which can take a while on all but the smallest rasters. If you have a large raster layer or expect to call `addRasterImage` on the same raster layer many times, you can perform the [EPSG:3857](http://spatialreference.org/ref/sr-org/7483/) projection yourself (either using `leaflet::projectRasterForLeaflet` or using another GIS library or program) and call `addRasterImage` with `project = FALSE`.
3226

3327
Be sure that your pre-projected raster layer is tagged with an accurate extent and CRS, as these values are still needed to place the image in the proper position on the map.
3428

3529
### Coloring
3630

37-
In order to render the `RasterLayer` as an image, each cell value must be converted to an RGB(A) color. You can specify the color scale using the `colors` argument, which accepts a variety of color specifications:
31+
In order to render the raster object as an image, each cell value must be converted to an RGB(A) color. You can specify the color scale using the `colors` argument, which accepts a variety of color specifications:
3832

3933
* The name of a [Color Brewer 2](http://colorbrewer2.org/) palette. If no `colors` argument is provided, then `"Spectral"` is the default.
4034
* A vector that represents the ordered list of colors to map to the data. Any color specification that is accepted by `grDevices::col2rgb` can be used, including `"#RRGGBB"` and `"#RRGGBBAA"` forms. Example: `colors = c("#E0F3DB", "#A8DDB5", "#43A2CA")`.

man/addRasterImage.Rd

Lines changed: 3 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# normalize
2+
3+
[[1]]
4+
[[1]][[1]]
5+
[[1]][[1]][[1]]
6+
lng lat
7+
1 -1 1
8+
2 1 1
9+
3 1 -1
10+
4 -1 -1
11+
5 -1 1
12+
13+
[[1]][[1]][[2]]
14+
lng lat
15+
1 -0.5 -0.5
16+
2 0.5 -0.5
17+
3 0.5 0.5
18+
4 -0.5 0.5
19+
5 -0.5 -0.5
20+
21+
22+
[[1]][[2]]
23+
[[1]][[2]][[1]]
24+
lng lat
25+
1 4 6
26+
2 6 6
27+
3 6 4
28+
4 4 4
29+
5 4 6
30+
31+
[[1]][[2]][[2]]
32+
lng lat
33+
1 4.05 4.05
34+
2 4.45 4.05
35+
3 4.45 4.45
36+
4 4.05 4.45
37+
5 4.05 4.05
38+
39+
[[1]][[2]][[3]]
40+
lng lat
41+
1 4.5 4.5
42+
2 5.5 4.5
43+
3 5.5 5.5
44+
4 4.5 5.5
45+
5 4.5 4.5
46+
47+
48+
49+
attr(,"bbox")
50+
[,1] [,2]
51+
lng -1 6
52+
lat -1 6
53+

tests/testthat/helper.R

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
create_square <- function(width = 2, lng = 0, lat = 0, hole = FALSE, type = Polygon) {
2+
lngs <- c(lng - width / 2, lng + width / 2, lng + width / 2, lng - width / 2)
3+
lats <- c(lat + width / 2, lat + width / 2, lat - width / 2, lat - width / 2)
4+
5+
if (hole) {
6+
lngs <- rev(lngs)
7+
lats <- rev(lats)
8+
}
9+
10+
if ("hole" %in% names(formals(type))) {
11+
type(cbind(lng = lngs, lat = lats), hole = hole)
12+
} else {
13+
type(cbind(lng = lngs, lat = lats))
14+
}
15+
}
16+
17+

tests/testthat/test-colors.R

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
context("colors")
2-
31
# Like expect_warning, but returns the result of the expr
42
with_warning <- function(expr) {
53
expect_warning(result <- force(expr))

0 commit comments

Comments
 (0)