Skip to content

Commit

Permalink
use touches when burning in roads to avoid small road chunks being ma…
Browse files Browse the repository at this point in the history
…de because cell with existing road line is not set to 0 in raster
  • Loading branch information
see24 committed Apr 18, 2024
1 parent 609b117 commit d0b3028
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 4 deletions.
2 changes: 1 addition & 1 deletion R/buildSimList.R
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ burnRoadsInWeight <- function(roads, weightRaster){

roadsRast <- !(rasts[[1]]|rasts[[2]])
} else {
roadsRast <- terra::rasterize(terra::vect(roads), weightRaster, background = 0) == 0
roadsRast <- terra::rasterize(terra::vect(roads), weightRaster, touches = TRUE, background = 0) == 0
}

weightRaster <- weightRaster * roadsRast
Expand Down
18 changes: 15 additions & 3 deletions tests/testthat/test-projectRoads.R
Original file line number Diff line number Diff line change
Expand Up @@ -122,17 +122,29 @@ test_that("input types are tested", {

test_that("duplicate roads are not created", {
res <- projectRoads(scen$landings.points, scen$cost.rast,
scen$road.line, plotRoads = doPlot)
scen$road.line, plotRoads = doPlot, roadsInWeight = FALSE)

#useful to visualize need to load fun from RoadsPaper
# dens <- res$roads %>% rasterizeLineDensity(r = res$weightRaster)

res_mst <- projectRoads(scen$landings.points, scen$cost.rast,
scen$road.line, plotRoads = doPlot, roadMethod = "mst")
# res_mst <- projectRoads(scen$landings.points, scen$cost.rast,
# scen$road.line, plotRoads = doPlot, roadMethod = "mst")

# dens_mst <- res_mst$roads %>% rasterizeLineDensity(r = res_mst$weightRaster)

expect_equal(sf::st_union(res$roads) %>% sf::st_length(), sf::st_length(res$roads) %>% sum())

# # draw extent to get problem tiny line created
# plot(scen$cost.rast)
# plot(scen$landings.points, add = TRUE)
# ext <- terra::draw()
#
# lnds <- sf::st_crop(scen$landings.points, ext)
# cst <- terra::crop(scen$cost.rast, ext)
# rds <- sf::st_crop(scen$road.line, ext)
#
# projectRoads(lnds, cst, rds, plotRoads = doPlot, roadsInWeight = FALSE)

})


Expand Down

0 comments on commit d0b3028

Please sign in to comment.