[R-sig-Geo] Using gdistance to compute a least cost path which avoids certain cells entirely, no matter the distance

Facundo Muñoz famuvie at alumni.uv.es
Fri May 1 22:06:21 CEST 2015


Hi Sharon,

note that if the road is "thin" (i.e. it takes only one or two pixels
wide), and if you use the function "mean" when creating the transition
layer, then it will always be possible to cross the roads.

I faced a similar problem, where I wanted to model an absolute barrier
(impossible to cross), and found it more convenient to work with
permeability rasters (rather than cost). So I can define permeability =
0 for barriers (i.e. infinte cost). Moreover, I defined a transition
function that returns the mean when both cells have non-zero values, but
returns zero if not.

ƒacu.-


El 01/05/2015 a las 17:08, sharx escribió:
> Hi Jacob,
>
> Thank you for the quick response! In the example where we want the animal
> to "go ten times as far to avoid crossing a road", would the non-road cells
> be assigned a resistance of 0? Or a resistance of 1?
>
> If I know that the animal only has enough to time to travel, say, 5000
> meters from one point to the next, is it correct to assign road cell
> resistance 5000 (given units in meters) and non-road cells a resistance of
> 1? By taking into account the 30 m raster cell size, do you mean we should
> multiply 5000 by 30 for the road resistance?
>
> Again, thank you so much for your help!
>
> Best,
> Sharon
>
> On Tue, Apr 28, 2015 at 8:25 AM, R-sig-geo mailing list [via R-sig-geo] <
> ml-node+s2731867n7588123h51 at n2.nabble.com> wrote:
>
>> 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 <[hidden email]
>> <http:///user/SendEmail.jtp?type=node&node=7588123&i=0>> 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
>> [hidden email] <http:///user/SendEmail.jtp?type=node&node=7588123&i=1>
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>>
>>         [[alternative HTML version deleted]]
>>
>> _______________________________________________
>> R-sig-Geo mailing list
>> [hidden email] <http:///user/SendEmail.jtp?type=node&node=7588123&i=2>
>> https://stat.ethz.ch/mailman/listinfo/r-sig-geo
>>
>>
>> ------------------------------
>>  If you reply to this email, your message will be added to the discussion
>> below:
>>
>> 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-tp7588118p7588123.html
>>  To unsubscribe from Using gdistance to compute a least cost path which
>> avoids certain cells entirely, no matter the distance, click here
>> <http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=7588118&code=c2hhcnhAdWNsYS5lZHV8NzU4ODExOHwxMjg5NzgzNjgz>
>> .
>> NAML
>> <http://r-sig-geo.2731867.n2.nabble.com/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
>
> --
> 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-tp7588118p7588150.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
>



More information about the R-sig-Geo mailing list