[R-sig-Geo] Using gdistance to compute a least cost path which avoids certain cells entirely, no matter the distance
Jacob van Etten
jacobvanetten at yahoo.com
Tue Apr 28 17:17:33 CEST 2015
Hi Sharon,
"Going ten times as far to avoid crossing a road" would require for each pair of points you measure the direct distance first and then make the road crossing value as 10x this value, taking into account you have set the road width to 30.
Best,
Jacob
On Monday, 27 April 2015, 18:35, sharx <sharx at ucla.edu> wrote:
Hi all,
I have some data of GPS locations of an animal and want to construct paths
between those coordinates. The goal is to obtain animal movement paths that
the cross the fewest roads possible by using shortestPath(), and assigning
resistance values to a raster of the animal's habitat. Ideally this means
that the path would show animals going out of their way to avoid roads, even
going ten times the distance they would have by crossing roads. I have
experimented with different resistance values with limited success.
Below is an example. Blue points are coordinates. Purple lines are roads.
Red line is the least cost path calculated, which goes through several roads
unnecessarily. Green line is the path I would like to generate.
<http://r-sig-geo.2731867.n2.nabble.com/file/n7588118/leastcost-example.png>
With my current results, I have achieved some avoidance of roads, but cannot
construct a path that goes too much extraneous distance in order to avoid
roads. When setting the non-road cell resistance to 0, however, I got an
extraordinarily complicated set of paths, perhaps due to values of infinity
when calculating the transition matrix.
*Could anyone could give me an idea as to how to choose these resistance
values, or how shortestPath() calculates with regards to the transition
matrix values and the actual distance in meters? *
I have created a raster called cost from a shapefile of roads in projection
NAD83, and using extract(), I have assigned much higher resistance values to
each cell of the raster cost if the cell contains a road. Here the cost of
non-road cells is 2^-20 and the cost of road cells is 10^100.
Here is my code:
library(raster)
cost <-rasterize(rd, r.30m, field=2^-20)
numbers <- extract(cost, rd, cellnumbers=TRUE, buffer=30)
cellnum <- unlist(numbers)
cost[cellnum] <- 10^100
library(gdistance)
## Produce transition matrices, and correct because 8 directions
trCost <- transition(1/cost, mean, directions=8)
trCost <- geoCorrection(trCost, type="c")
# Iterate between a list of coordinate pairs, use shortestPath() to get a
path for each pair
#Each element of the list coords contains two points that straddle a road.
# For each pair of points, calculate the least cost path between them.
getpath <- function(coords) {
c = unlist(coords)
pt1 = c(c[1], c[3])
pt2 = c(c[2], c[4])
if (sqrt((pt1[1]-pt2[1])^2 + (pt1[2]-pt2[2])^2) <= sqrt(2*30^2)) {
return(SpatialLines(list(Lines(Line(rbind(pt1,pt2)), ID="1"))))
} # if the points are in the same raster cell, return a straight line
between them (a least cost path will not work)
return(shortestPath(trCost, pt1, pt2, output="SpatialLines"))
}
paths <- numeric(0)
for (i in 1:length(coords[[1]])){
c=lapply(coords, "[[", i)
paths <- c(paths, getpath(c))
}
#Code end
Thank you for your help in advance!
Best,
Sharon
--
View this message in context: http://r-sig-geo.2731867.n2.nabble.com/Using-gdistance-to-compute-a-least-cost-path-which-avoids-certain-cells-entirely-no-matter-the-distae-tp7588118.html
Sent from the R-sig-geo mailing list archive at Nabble.com.
_______________________________________________
R-sig-Geo mailing list
R-sig-Geo at r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
[[alternative HTML version deleted]]
More information about the R-sig-Geo
mailing list