diff --git a/CRiSp.Rproj b/CRiSp.Rproj index 69fafd4..c076ae9 100644 --- a/CRiSp.Rproj +++ b/CRiSp.Rproj @@ -18,5 +18,5 @@ LineEndingConversion: Posix BuildType: Package PackageUseDevtools: Yes -PackageInstallArgs: --no-multiarch --with-keep.source +PackageInstallArgs: --no-multiarch --with-keep.source --install-tests PackageRoxygenize: rd,collate,namespace diff --git a/tests/testthat/test-valley.R b/tests/testthat/test-valley.R new file mode 100644 index 0000000..12ca12c --- /dev/null +++ b/tests/testthat/test-valley.R @@ -0,0 +1,50 @@ +bb <- bucharest_osm$bb +asset_urls <- c(paste0("s3://copernicus-dem-30m/", + "Copernicus_DSM_COG_10_N44_00_E026_00_DEM/", + "Copernicus_DSM_COG_10_N44_00_E026_00_DEM.tif"), + paste0("s3://copernicus-dem-30m/", + "Copernicus_DSM_COG_10_N44_00_E025_00_DEM/", + "Copernicus_DSM_COG_10_N44_00_E025_00_DEM.tif")) + +test_that("STAC asset urls are correctly retrieved", { + skip_on_ci() + + ep <- "https://earth-search.aws.element84.com/v1" + col <- "cop-dem-glo-30" + + asset_urls_retrieved <- get_stac_asset_urls(bb, endpoint = ep, + collection = col) + asset_urls_retrieved_default <- get_stac_asset_urls(bb) + expected_asset_urls <- asset_urls + + expect_equal(expected_asset_urls, asset_urls_retrieved) + expect_equal(expected_asset_urls, asset_urls_retrieved_default) +}) + +test_that("raster data are correctly retrieved and merged", { + skip_on_ci() + + dem <- load_raster(bb, asset_urls) |> CRiSp::reproject(32635) + expected_dem <- terra::unwrap(bucharest_dem) + + expect_equal(terra::values(dem), terra::values(expected_dem)) + expect_equal(terra::crs(dem), terra::crs(expected_dem)) + expect_true(all.equal(terra::ext(dem), terra::ext(expected_dem), + tolerance = 1e-4)) +}) + +test_that("valley polygon is correctly constructed", { + dem <- terra::unwrap(bucharest_dem) + river <- bucharest_osm$river_surface + crs <- "epsg:32635" + + valley <- get_valley(dem, river, crs) + expected_valley <- sf::st_read("./testdata/expected_valley.gpkg", + quiet = TRUE) |> + sf::st_as_sfc() + + valley <- st_set_precision(valley, 1e-06) + expected_valley <- st_set_precision(expected_valley, 1e-06) + expect_true(sf::st_equals_exact(valley, expected_valley, + par = 1.e-4, sparse = FALSE)) +}) diff --git a/tests/testthat/testdata/expected_valley.gpkg b/tests/testthat/testdata/expected_valley.gpkg new file mode 100644 index 0000000..d285245 Binary files /dev/null and b/tests/testthat/testdata/expected_valley.gpkg differ diff --git a/tests/testthat/testdata/generate_valley.R b/tests/testthat/testdata/generate_valley.R new file mode 100644 index 0000000..79bbaac --- /dev/null +++ b/tests/testthat/testdata/generate_valley.R @@ -0,0 +1,8 @@ +dem <- terra::unwrap(CRiSp::bucharest_dem) +river <- CRiSp::bucharest_osm$river_surface +crs <- "epsg:32635" + +valley <- CRiSp::get_valley(dem, river, crs) + +filepath <- testthat::test_path("testdata", "expected_valley.gpkg") +sf::st_write(valley, filepath, append = FALSE, quiet = TRUE)