2

I'm trying to create a barrier (rivers) that can't be crossed by the least cost path.

I've created a transition layer using "areas" which contains the transition values on land, and on rivers. However, the barrier has a 'buffer' around it, with increasing values, instead of being a discrete barrier.

Cost raster and transition layer cost raster Transition layer As can be seen, there's a 'buffer' around the barrier in the transition layer.

Any ideas on how to create a discrete boundary? preferably with the barrier as a value 1000, with everywhere else a value of 1.

code: library(gdistance) ## Create cost surface where "land" exists in the middle cost <- raster(nrow=100, ncol=100, xmn=0, xmx=100, ymn=0, ymx=100, crs="+proj=utm") cost[] <- 10 cost[cellFromRowColCombine(cost, 50:55,20:80)] <- 1000 costf <- asFactor(cost < 100) ## Produce transition matrices, and correct because 8 directions trCost <- transition(costf, "areas", directions=16) trCost1 <- geoCorrection(trCost[[2]], type="c") plot(cost) plot(raster(trCost[[1]]))

  • You should give some reproducible code and data if you want any help. so that we can try to help you. However, I suppose that this "buffer" area is normal because you trying to see path. Depending on your starting and arriving positions, you have a small probability to walk along the barrier. – Sébastien Rochette Jun 18 '17 at 7:12
  • Thanks for replying. Reproducible code added. I didn't think about that actually. I guess this stops the line from 'hugging' the barrier, instead of being diverted earlier on. – user3384265 Jun 18 '17 at 8:08
3

There are several options to create this type of barrier by using the transitionFuntion argument in the transition function strategically. This should cover 99% of the cases. Set the conductance value to 0 to make sure the path does not cross the river at all. (To make the river 100 times more costly than land, use a small conductance value instead: 0.01.)

Here we go.

library(gdistance)

Create cost surface setting land to a conductance of 10.

cost <- raster(nrow=100, ncol=100, 
           xmn=0, xmx=100, ymn=0, ymx=100, crs="+proj=utm")
cost[] <- 10

Set origin and goal cells for testing

origin <- c(50, 1)
goal <- c(50, 100)

Option 1 - transitionFunction = mean

cc1 <- cellFromRowColCombine(cost, 50:55,20:80)
cost1 <- cost
cost1[cc1] <- 0 
trCost1 <- transition(cost1, transitionFunction = mean, 16)
trCost1 <- geoCorrection(trCost1, type="c")

Check if Option 1 has worked

plot(raster(trCost1))
sPath1 <- shortestPath(trCost1, origin, goal, output="SpatialLines")
lines(sPath1)

Option 2 - transitionFunction = min

Option 1 will not work with rivers that are only one cell broad. If this is a problem, Option 2 is an alternative.

cc2 <- cellFromRowColCombine(cost, 50,20:80)
cost2 <- cost
cost2[cc2] <- 0 
trCost2 <- transition(cost2, transitionFunction = min, 8)
trCost2 <- geoCorrection(trCost2, type="c")

Don't use 16 directions for Option 2, as this will make it possible to jump over the barrier (chess knight movement)!

Check if Option 2 has worked.

plot(raster(trCost2))
sPath2 <- shortestPath(trCost2, origin, goal, output="SpatialLines")
lines(sPath2)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.