From e8f607f5bbb182d235f0c8820289356fbe27242d Mon Sep 17 00:00:00 2001 From: see24 Date: Tue, 28 May 2024 17:21:51 -0400 Subject: [PATCH] minor vignette change and checking gradePenalty change --- data-raw/dem_example.R | 38 ++++++++++++++++++++++++++++++++++ tests/testthat/test-getGraph.R | 3 +-- vignettes/grade-penalty.Rmd | 16 +------------- vignettes/roads-vignette.Rmd | 2 +- 4 files changed, 41 insertions(+), 18 deletions(-) create mode 100644 data-raw/dem_example.R diff --git a/data-raw/dem_example.R b/data-raw/dem_example.R new file mode 100644 index 0000000..b528766 --- /dev/null +++ b/data-raw/dem_example.R @@ -0,0 +1,38 @@ +## code to prepare `dem_example` dataset goes here +library(roads) +library(dplyr) +library(terra) + +dir <- tempdir() +can_elev <- geodata::elevation_30s("CAN", dir) +wrld_lc <- geodata::landcover("water", dir) +plot(can_elev) + +# ext <- terra::draw() +# dput(as.vector(ext)) + +ext <- c(xmin = -118.490016655244, xmax = -115.954077203458, ymin = 49.4109306683323, + ymax = 50.846908182386) + +ex_elev <- crop(can_elev, ext) +ex_lc <- crop(wrld_lc, ext) + +plot(ex_elev) +plot(ex_lc >0.2) + + + +tpi <- ex_elev %>% terra::terrain(v = "TPI") + +# Try TPI over larger window +TPI <- focal(ex_elev, w=7, fun=\(x) x[25] - mean(x[-25])) + +# thing is at this resolution crossing the river and following the river are +# going to both be in the higher cost area which isn't correct... + +plot(TPI < -200 |ex_lc > 0.2) + + + +# Move the above to a data-raw file +usethis::use_data(dem_example, overwrite = TRUE) diff --git a/tests/testthat/test-getGraph.R b/tests/testthat/test-getGraph.R index 6c65357..f284721 100644 --- a/tests/testthat/test-getGraph.R +++ b/tests/testthat/test-getGraph.R @@ -35,8 +35,7 @@ test_that("getGraph works with gdistance method", { expect_length(igraph::edge_attr(gR_gD, "weight"), 40) expect_length(igraph::edge_attr(gQ_gD, "weight"), 72) expect_length(igraph::edge_attr(gO_gD, "weight"), 72) - # TODO: figure out why not as expected but not really using gdistance right now - # expect_true(all(igraph::edge_attr(gO_gD, "weight") >= igraph::edge_attr(gQ_gD, "weight"))) + expect_true(all(igraph::edge_attr(gO_gD, "weight") >= igraph::edge_attr(gQ_gD, "weight"))) }) test_that("getGraph works with gradePenaltyFun", { diff --git a/vignettes/grade-penalty.Rmd b/vignettes/grade-penalty.Rmd index f76eb9e..b7fd919 100644 --- a/vignettes/grade-penalty.Rmd +++ b/vignettes/grade-penalty.Rmd @@ -17,19 +17,5 @@ knitr::opts_chunk$set( ``` ```{r setup} -library(roads) - -demoScen <- prepExData(demoScen) - -purrr::map(demoScen, "cost.rast") %>% terra::rast() %>% terra::plot() - -can_elev <- geodata::elevation_30s("CAN", "test") - -ext <- terra::draw() - -ex_elev <- terra::crop(can_elev, ext) - -plot(ex_elev) - -# Move the above to a data-raw file +# see data being prepared in dem_example.R ``` diff --git a/vignettes/roads-vignette.Rmd b/vignettes/roads-vignette.Rmd index 36a0275..e9cbb16 100644 --- a/vignettes/roads-vignette.Rmd +++ b/vignettes/roads-vignette.Rmd @@ -65,7 +65,7 @@ CLUSexample <- prepExData(CLUSexample) ### 1. Weights Raster and Weight Function -The cost of new roads development within the landscape is determined by applying the `weightFunction` to the `weightRaster`. The `weightRaster` must be provided as a single, numeric `SpatRaster` or `RasterLayer` object. The cell values in the raster for two adjacent cells are provided to the `weightFunction` to calculate the weight for the edge connecting the two cells in the graph which should represent the cost of building a road connecting the two cells. Two ways of calculating the cost are supported by the package with the option for users to develop their own. The simplest method is for the `weightRaster` values to represent the cost of building a road across a cell and the for the `weightFunction` to simply take the mean of the values in adjacent cells. The other option included in the package is to use the `gradePenaltyFn`. This function takes a modified DEM raster as input and determines costs by applying a base cost and a grade penalty which is multiplied by the % grade between adjacent cells. The DEM can be modified by adding barriers to the raster, either as negative values which represent high costs (e.g. a stream crossing) or NAs for inaccessible areas (e.g. a cliff or a lake). See `?gradePenaltyFn` for more details. +The cost of new roads development within the landscape is determined by applying the `weightFunction` to the `weightRaster`. The `weightRaster` must be provided as a single, numeric `SpatRaster` or `RasterLayer` object. The cell values in the raster for two adjacent cells and the distance between the cells are provided to the `weightFunction` to calculate the weight for the edge connecting the two cells in the graph which should represent the cost of building a road connecting the two cells. Two ways of calculating the cost are supported by the package with the option for users to develop their own. The simplest method is for the `weightRaster` values to represent the cost of building a road across a cell and the for the `weightFunction` to simply take the mean of the values in adjacent cells (`simpleCostFn`). The other option included in the package is to use the `gradePenaltyFn`. This function takes a modified DEM raster as input and determines costs by applying a base cost and a grade penalty which is multiplied by the % grade between adjacent cells. The DEM can be modified by adding barriers to the raster, either as negative values which represent high costs (e.g. a stream crossing) or NAs for inaccessible areas (e.g. a cliff or a lake). See `?gradePenaltyFn` for more details. The following points apply to all `weightRasters`: