Skip to content

Commit 92b733f

Browse files
committedDec 22, 2022
Remove outdated dependencies; use testthat-3e
See #833
1 parent 51daa69 commit 92b733f

14 files changed

+110
-62
lines changed
 

‎DESCRIPTION

+2-3
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,8 +54,6 @@ Suggests:
5454
maps,
5555
sf (>= 0.9-6),
5656
shiny,
57-
rgdal,
58-
rgeos,
5957
R6,
6058
RJSONIO,
6159
purrr,
@@ -65,3 +63,4 @@ Suggests:
6563
RoxygenNote: 7.2.1
6664
Encoding: UTF-8
6765
LazyData: true
66+
Config/testthat/edition: 3

‎R/layers.R

+3-4
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

+17-4
Original file line numberDiff line numberDiff line change
@@ -90,11 +90,24 @@ 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+
}
105+
x <- to_multipolygon_list(sf::st_geometry(sf::st_as_sf(SpatialPolygons(list(pgons)))))
106+
return(x[[1]])
107+
} else {
108+
comment <- paste(collapse = " ", rep_len("0", length(pgons@Polygons)))
109+
}
110+
}
98111
pstatus <- as.integer(strsplit(comment, " ")[[1]])
99112
lapply(which(pstatus == 0L), function(index) {
100113
# Return a list of rings, exterior first

‎docs/raster.Rmd

+7-13
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

+3-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/_snaps/normalize-2.md

+53
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.5 4.5
34+
2 5.5 4.5
35+
3 5.5 5.5
36+
4 4.5 5.5
37+
5 4.5 4.5
38+
39+
[[1]][[2]][[3]]
40+
lng lat
41+
1 4.05 4.05
42+
2 4.45 4.05
43+
3 4.45 4.45
44+
4 4.05 4.45
45+
5 4.05 4.05
46+
47+
48+
49+
attr(,"bbox")
50+
[,1] [,2]
51+
lng -1 6
52+
lat -1 6
53+

‎tests/testthat/test-colors.R

-2
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))

‎tests/testthat/test-evalFormula.R

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
2-
3-
context("evalFormula")
4-
5-
6-
71
test_that("evalFormula() does not discard the class of a list", {
82
res <- evalFormula(structure(list(1, ~x, ~x + 1), class = "FOO"), data.frame(x = 2))
93

‎tests/testthat/test-icon.R

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
context("icon")
3-
41
test_that("icon deduping works", {
52
icons <- c("leaf-green.png", "leaf-red.png")
63
m <-

‎tests/testthat/test-measure.R

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
2-
context("measure")
3-
41
test_that("dependency got added", {
52
expect_true(
63
!is.na(Position(

‎tests/testthat/test-normalize-2.R

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,11 @@
22
expect_maps_equal <- function(m1, m2) {
33
attr(m1$x, "leafletData") <- NULL
44
attr(m2$x, "leafletData") <- NULL
5-
expect_equal(m1, m2, check.environment = FALSE)
5+
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
66
}
77

88
test_that("normalize", {
99
skip_if_not_installed("sf")
10-
skip_if_not_installed("rgeos")
1110

1211
library(sf)
1312
library(sp)
@@ -101,12 +100,15 @@ test_that("normalize", {
101100
create_square(1, 5, 5, hole = TRUE),
102101
create_square(0.4, 4.25, 4.25, hole = TRUE)
103102
), "A")
104-
comment(polys) <- rgeos::createPolygonsComment(polys)
103+
comment(polys) <- "0 0 1 2 2"
105104

106105
spolys <- SpatialPolygons(list(
107106
polys
108107
))
109108
stspolys <- st_as_sf(spolys)
109+
110+
testthat::expect_snapshot_output(derivePolygons(spolys))
111+
110112
(l101 <- leaflet(spolys) %>% addPolygons())
111113
(l102 <- leaflet(stspolys) %>% addPolygons())
112114
expect_maps_equal(l101, l102)

‎tests/testthat/test-normalize-3.R

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
expect_maps_equal <- function(m1, m2) {
33
attr(m1$x, "leafletData") <- NULL
44
attr(m2$x, "leafletData") <- NULL
5-
expect_equal(m1, m2, check.environment = FALSE)
5+
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
66
}
77

88

@@ -75,12 +75,12 @@ test_that("normalize terra", {
7575
create_square(1, 5, 5, hole = TRUE),
7676
create_square(0.4, 4.25, 4.25, hole = TRUE)
7777
), "A")
78-
comment(polys) <- rgeos::createPolygonsComment(polys)
78+
comment(polys) <- "0 0 1 2 2"
7979

8080
spolys <- SpatialPolygons(list(
8181
polys
8282
))
83-
# these "commented" Spatial objects need to go through
83+
# these "commented" Spatial objects need to go through
8484
# sf for terra to understand them properly
8585
vpolys = vect(sf::st_as_sf(spolys ))
8686
(l101 <- leaflet(spolys) %>% addPolygons())

‎tests/testthat/test-normalize.R

+6-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
context("normalize")
2-
31
# derivePoints -------------------------------------------------------------
42

53
test_that("can get point data from SpatialPointsDataFrame", {
@@ -27,13 +25,13 @@ test_that("derivePolygons works with sf classes", {
2725

2826
verifyPolygonData <- function(x) {
2927
expect_true(!is.null(attr(x, "bbox", exact = TRUE)))
30-
expect_is(x, "list")
28+
expect_type(x, "list")
3129
lapply(x, function(multipolygon) {
32-
expect_is(multipolygon, "list")
30+
expect_type(multipolygon, "list")
3331
lapply(multipolygon, function(polygon) {
34-
expect_is(polygon, "list")
32+
expect_type(polygon, "list")
3533
lapply(polygon, function(ring) {
36-
expect_is(ring, "data.frame")
34+
expect_s3_class(ring, "data.frame")
3735
})
3836
})
3937
})
@@ -50,7 +48,7 @@ test_that("derivePolygons normalizes polygon data across sp polygon classes", {
5048
expect_equal(out[[1]][[1]][[1]]$lng, meuse.riv[, 1])
5149
expect_equal(out[[1]][[1]][[1]]$lat, meuse.riv[, 2])
5250
# row/col names are different but values are the same
53-
expect_equivalent(attr(out, "bbox"), sp::bbox(meuse.riv))
51+
expect_equal(attr(out, "bbox"), sp::bbox(meuse.riv), ignore_attr = TRUE)
5452

5553
polys <- sp::Polygons(list(poly), "river")
5654
expect_equal(derivePolygons(polys), out)
@@ -72,7 +70,7 @@ test_that("derivePolygons normalizes polygon data across sp line classes", {
7270
expect_equal(out[[1]][[1]][[1]]$lng, meuse.riv[, 1])
7371
expect_equal(out[[1]][[1]][[1]]$lat, meuse.riv[, 2])
7472
# row/col names are different but values are the same
75-
expect_equivalent(attr(out, "bbox"), sp::bbox(meuse.riv))
73+
expect_equal(attr(out, "bbox"), sp::bbox(meuse.riv), ignore_attr = TRUE)
7674

7775
lines <- sp::Lines(list(line), "river")
7876
expect_equal(derivePolygons(lines), out)

‎tests/testthat/test-raster.R

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,27 +2,32 @@
22
expect_maps_equal <- function(m1, m2) {
33
attr(m1$x, "leafletData") <- NULL
44
attr(m2$x, "leafletData") <- NULL
5-
expect_equal(m1, m2, check.environment = FALSE)
5+
expect_equal(m1, m2, ignore_function_env = TRUE, ignore_formula_env = TRUE)
66
}
77

8+
# Some proj4string values differ only by one having whole numbers represented as
9+
# x while others have x.0. So, strip each trailing .0 value.
10+
normalize_zero_values <- function(str) {
11+
gsub("=(\\d+).0( |$)", "=\\1\\2", str)
12+
}
813

914
test_that("rasters", {
1015
skip_if_not_installed("terra")
1116

1217
library(terra)
13-
library(raster)
18+
library(raster)
1419

1520
lux <- rast(system.file("ex/elev.tif", package="terra"))
1621
pmerc <- "+proj=merc +a=6378137 +b=6378137 +lat_ts=0 +lon_0=0 +x_0=0 +y_0=0 +k=1 +units=m +nadgrids=@null +wktext +no_defs"
1722

1823
plux <- projectRasterForLeaflet(lux, "bilinear")
19-
expect_equal(crs(plux, proj=TRUE), pmerc)
24+
expect_equal(normalize_zero_values(crs(plux, proj=TRUE)), pmerc)
2025
test <- projectRasterForLeaflet(raster(lux), "bilinear")
21-
expect_equal(proj4string(test), pmerc)
26+
expect_equal(normalize_zero_values(proj4string(test)), pmerc)
2227

2328
# terra and raster have different projection algorithms, and while
24-
# their outputs are very similar, they are not identical. Hence we need
25-
# to use pre-projected rasters and project=FALSE
29+
# their outputs are very similar, they are not identical. Hence we need
30+
# to use pre-projected rasters and project=FALSE
2631
rtest <- function(x) {
2732
leaflet() %>% addTiles() %>% addRasterImage(x, project=FALSE)
2833
}

0 commit comments

Comments
 (0)
Please sign in to comment.