library(roads)
library(terra)
#> Warning: package 'terra' was built under R version 4.2.2
#> terra 1.6.34
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:terra':
#>
#> intersect, union
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
library(raster)
#> Loading required package: sp
#>
#> Attaching package: 'raster'
#> The following object is masked from 'package:dplyr':
#>
#> select
## colours for displaying cost raster
if(requireNamespace("viridis", quietly = TRUE)){
# Use colour blind friendly palette if available
<- c('grey50', viridis::viridis(30))
rastColours else {
} <- c('grey50', terrain.colors(30))
rastColours
}
## using demo scenario 1
<- demoScen[[1]] scen
Road construction times are not always known but they can be inferred from the harvest times of harvest blocks that they access. To do this we need harvest locations with a known sequence and a present day road network where the sequence of road building is not known. The code below creates a demonstration road data set.
# make "real" roads by using dlcp method
# use all landings and build roads to closest first
<- scen$landings.points[scen$landings.points$set %in% c(1:4),]
land.pnts
<- projectRoads(land.pnts, scen$cost.rast, scen$road.line.sf,
realRoads roadMethod = "dlcp")
#> 0s detected in cost raster, these will be considered as existing roads
plot(scen$cost.rast, col = rastColours, main = 'Scenario')
plot(realRoads$roads, add = TRUE)
plot(land.pnts, add = TRUE, pch = 21, cex = 2, bg = 'white')
text(land.pnts@coords, labels = land.pnts$set, cex = 0.6, adj = c(0.5, 0.3),
xpd = TRUE)
Our scenario is that we have landings that were harvested over four years and a road network that links them. We will assign a year to each road segment by first setting the cost of building roads to very low where our road network is and then projecting road development to landings in each year, assigning that year to the road segments as a maximum year of road construction.
# Use sequence of landings to assign sequence of roads built
# convert cost to SpatRaster for faster rasterization
$cost.rast <- terra::rast(scen$cost.rast)
scen
# burn in realRoads to have cost of 0 so that projected roads will follow them.
<- terra::rasterize(terra::vect(realRoads$roads), scen$cost.rast,
roadsRast background = 0) == 0
# doesn't work if cost is 0 because areas with 0 cost are assumed to already be
# roads so no new ones will be built
$cost.rast <- scen$cost.rast * (roadsRast + 0.00001)
scen
# set pre-existing roads to year 0
$road.line.sf$Year <- 0
scen
# initialize sim list with first landings set
<- list(projectRoads(land.pnts[land.pnts$set == 1,], scen$cost.rast,
multiTime_sim $road.line.sf))
scen#> 0s detected in cost raster, these will be considered as existing roads
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
1]]$roads <- multiTime_sim[[1]]$roads %>%
multiTime_sim[[mutate(Year = ifelse(is.na(Year), 1, Year))
# iterate over landings sets using the sim list from the previous run as input
for (i in 2:4) {
<- projectRoads(sim = multiTime_sim[[i-1]], landings = land.pnts[land.pnts$set == i,])
newSim
$roads <- newSim$roads %>%
newSimmutate(Year = ifelse(is.na(Year), i, Year))
<- c(
multiTime_sim
multiTime_sim,list(newSim)
)
}#> 0s detected in cost raster, these will be considered as existing roads
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
#> 0s detected in cost raster, these will be considered as existing roads
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
#> 0s detected in cost raster, these will be considered as existing roads
#> Warning: attribute variables are assumed to be spatially constant throughout all
#> geometries
plot(multiTime_sim[[4]]$roads["Year"], lwd = 2)