Skip to content

Commit

Permalink
add other tests
Browse files Browse the repository at this point in the history
  • Loading branch information
olivroy committed Sep 18, 2023
1 parent 0102015 commit 2254cc7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 97 deletions.
7 changes: 7 additions & 0 deletions tests/testthat/_snaps/terra-stars.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# terra works

Code
tm_shape(land_terra) + tm_raster("treess")
Error <simpleError>
Visual values used for the variable, "col" of layer function "tm_raster" are incorrect.

67 changes: 20 additions & 47 deletions tests/testthat/test-terra-stars.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,35 @@ test_that("terra works", {
})

# FIXME this is not expected.
expect_snapshot(error = TRUE, {
tm_shape(landsat) + tm_raster("landsat_1", col.free = FALSE)
})

tm_shape(landsat) +
tm_rgb(tm_mv("landsat_4", "landsat_3", "landsat_2"), col.scale = tm_scale_rgb(maxValue = 31961))
# Defaults of stars and terra are identical.
tm_shape(landsat) + tm_raster(col.free = FALSE)
tm_shape(landsat) +
tm_raster(c("landsat_1", "landsat_2", "landsat_3", "landsat_4"), col.free = FALSE) +
tm_options(max.raster = 10000)

skip_on_os("linux")
tm_shape(landsat) + tm_raster("landsat_1", col.free = FALSE)

})

test_that("stars works", {

skip_on_cran()
landsat = stars::read_stars(system.file("raster/landsat.tif", package = "spDataLarge"))
land_stars <- tmap::land
land_stars = tmap::land

tm_shape(land_stars) + tm_raster("trees")
tm_shape(land_stars) + tm_raster("treess")

tm_shape(landsat) + tm_raster("landsat.tif", col.free = FALSE)
tm_shape(land_stars) + tm_raster("treess")

expect_warning(tm_shape(landsat) + tm_raster("landsat.tif", col.free = FALSE))

p <- tm_shape(landsat) + tm_raster(col.free = FALSE)
tm_shape(landsat) +
tm_raster("landsat.tif") +
tm_facets("band") + tm_options(max.raster = 10000)
})


Expand All @@ -40,7 +51,7 @@ test_that("stars works", {

test_that("multi rast works.", {
multi_raster_file = system.file("raster/landsat.tif", package = "spDataLarge")
multi_rast = rast(multi_raster_file)
multi_rast = terra::rast(multi_raster_file)
expect_no_error({
tm_shape(multi_rast[[3:1]]) +
tm_rgb()
Expand All @@ -51,44 +62,6 @@ test_that("multi rast works.", {

})



## defaults are identical:
# (terra uses integers, so tm_scale_discrete is used)
tm_shape(landsat_stars) + tm_raster(col.free = FALSE)
tm_shape(landsat_stars) +
tm_raster("landsat.tif") +
tm_facets("band") + tm_options(max.raster = 10000)
tm_shape(landsat_terra) + tm_raster(col.free = FALSE)
tm_shape(landsat_terra) +
tm_raster(c("landsat_1", "landsat_2", "landsat_3", "landsat_4"), col.free = FALSE) +
tm_options(max.raster = 10000)



tm_shape(landsat_terra) +
tm_rgb(tm_mv("landsat_4", "landsat_3", "landsat_2"), col.scale = tm_scale_rgb(maxValue = 31961))




# approach: reshape landsat_stars (somewhere in the shape functions)
# - step1_helper L75: create value
# - subset: use this to reshape stars stars::split
# - Meta2: redo smeta
landsat_stars2

# step2_helper L292 move ShapeMeta1 redo earlier, so that faceting will be succesfull










test_that("Both approaches work for stars.", {
skip_on_cran()
# idea: tm_attr to specify an attribute as mv
Expand All @@ -98,7 +71,7 @@ test_that("Both approaches work for stars.", {
tm_rgb(tm_mv_dim("band", c(4,3,2)), col.scale = tm_scale_rgb(maxValue = 31961))

# indirect approach
landsat_stars2 = split(landsat_stars. "band")
landsat_stars2 = split(landsat_stars, "band")
tm_shape(landsat_stars2) +
tm_rgb(tm_mv("X4", "X3", "X2"), col.scale = tm_scale_rgb(maxValue = 31961))
})
25 changes: 2 additions & 23 deletions tests/testthat/test-tm_layers_aux.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,9 @@ test_that("Aux layers work.", {
Africa = World[World$continent == "Africa", ]
})




txt = function(x) print(grid::grid.text(x, gp = gpar(cex = 3)))
v3 = function(e) {
if (tmapV == "3") {
print(e)
} else {
txt("Only for tmap 3")
}
invisible(NULL)
}
v4 = function(e) {
if (tmapV == "4") {
print(e)
} else {
txt("Only for tmap 4")
}
invisible(NULL)
}
txt(paste("Loaded tmap version", tmapV))
```

test_that("Base layer works at different positions", {
Africa = World[World$continent == "Africa", ]

tm_basemap("OpenStreetMap")+
tm_shape(Africa) +
tm_polygons("HPI", fill.scale = tm_scale(values = "viridis")) +
Expand Down
53 changes: 26 additions & 27 deletions tests/testthat/test-v3.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
test_that("v3 syntax works", {
skip_on_cran()
data(World)

expect_snapshot({
tm_shape(World) + tm_fill("darkolivegreen3") + tm_format("World", title = "A green World")
tm_shape(World) + tm_polygons(fill = "darkolivegreen3", col = NA) + tm_format("World", title = "A green World")
})


expect_warning(tm_shape(World) + tm_fill("darkolivegreen3") + tm_format("World", title = "A green World"))
expect_warning(tm_shape(World) + tm_polygons(fill = "darkolivegreen3", col = NA) + tm_format("World", title = "A green World"))



Expand Down Expand Up @@ -60,28 +57,30 @@ test_that("Possible to revert to v3 styling.", {
World$HPI[1:10] = NA
tm_shape(World) + tm_polygons("economy", style = "cat")
tmap_style("v3")
expect_snapshot({
tm_shape(World) + tm_polygons("economy", style = "cat")
tm_shape(World) + tm_polygons("HPI",
expect_message(tm_shape(World) + tm_polygons("economy", style = "cat"))
expect_message(tm_shape(World) + tm_polygons("HPI",
style = "fixed",
breaks = c(0, 20, 35, 42, 50))
tm_shape(World) + tm_polygons("HPI", style = "sd")
tm_shape(World) + tm_polygons("HPI", style = "equal")
tm_shape(World) + tm_polygons("HPI", style = "pretty")
tm_shape(World) + tm_polygons("HPI", style = "quantile")
tm_shape(World) + tm_polygons("HPI", style = "kmeans")
tm_shape(World) + tm_polygons("HPI", style = "hclust")
tm_shape(World) + tm_polygons("HPI", style = "bclust")
tm_shape(World) + tm_polygons("HPI", style = "fisher")
tm_shape(World) + tm_polygons("HPI", style = "jenks")
tm_shape(World) + tm_polygons("HPI", style = "dpih")
tm_shape(World) + tm_polygons("HPI", style = "headtails")
tm_shape(World) + tm_polygons("HPI", style = "log10_pretty")

tm_shape(World) + tm_polygons("HPI", style = "cont")
tm_shape(World) + tm_polygons("HPI", style = "order")
tm_shape(World) + tm_polygons("HPI", style = "log10")
})
breaks = c(0, 20, 35, 42, 50)))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "sd"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "equal"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "pretty"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "quantile"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "kmeans"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "hclust"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "bclust"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "fisher"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "jenks"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "dpih"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "headtails"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "cont"))
expect_message(tm_shape(World) + tm_polygons("HPI", style = "log10"))
})

test_that("log10_pretty and order styles work", {

expect_no_error(expect_message(tm_shape(World) + tm_polygons("HPI", style = "log10_pretty")))
expect_no_error(expect_message(tm_shape(World) + tm_polygons("HPI", style = "order")))

})

test_that("v3 that doesn't work", {
Expand Down

0 comments on commit 2254cc7

Please sign in to comment.