Skip to content

Commit

Permalink
minor vignette change and checking gradePenalty change
Browse files Browse the repository at this point in the history
  • Loading branch information
see24 committed May 28, 2024
1 parent 8932fb2 commit e8f607f
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 18 deletions.
38 changes: 38 additions & 0 deletions data-raw/dem_example.R
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 1 addition & 2 deletions tests/testthat/test-getGraph.R
Original file line number Diff line number Diff line change
Expand Up @@ -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", {
Expand Down
16 changes: 1 addition & 15 deletions vignettes/grade-penalty.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
2 changes: 1 addition & 1 deletion vignettes/roads-vignette.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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`:

Expand Down

0 comments on commit e8f607f

Please sign in to comment.