I just realized that the previous solution will not connect point that are at exactly the same height. 
A simple solution to avoid Inf values is to add a small value to the height difference.

require(gdistance)
DEM <- raster(system.file("external/maungawhau.grd",package = "gdistance"))

#Simpler alternative, avoiding Inf values
hd2 <- transition(DEM,function(x){1/(abs(x[2] - x[1]) + 1)},8)
slope2 <- geoCorrection(hd2)

#Define some random points
A <- cbind(2667670, 6479000)
B <- cbind(2667800, 6479400)
pts<-rbind(A,B)

plot(DEM)
BA<-shortestPath(slope2,B,A,"SpatialLines")
AB<-shortestPath(slope2,A,B,"SpatialLines")
lines(BA)
lines(AB)
#Small differences -- are there more than one equivalent options?
#Dijkstra algorithm just gives one arbitrary shortest path -- more than one can exist

#Let's check
BATr <- shortestPath(slope2,B,A)
ABTr <- shortestPath(slope2,A,B)
sum(transitionMatrix((BATr*slope2)))
sum(transitionMatrix((ABTr*slope2)))
#Same length, as expected

#Profiles
shortestProfile1<-unlist(extract(DEM,AB))
shortestProfile2<-unlist(extract(DEM,BA))

plot(shortestProfile1,type='l')
plot(shortestProfile2,type='l')
#Looks ok.

#Let's make a 3D map
map3d <- persp(DEM, theta=160, phi=40)
coord <- coordinates(AB)[[1]][[1]]
z <- extract(DEM, coord)
lines (trans3d(coord[,1], coord[,2], z, pmat = map3d), col = 4, lwd=3)



________________________________

To: Ben Weinstein <benweinstein2010@gmail.com>; r-sig-geo <r-sig-geo@r-project.org> 
Sent: Monday, 3 September 2012, 21:47
Subject: Re: [R-sig-Geo] Validating Cost Paths - minimizing elevation change


Hi Ben,

In your example you produce Inf conductance values, which generates problems.

In this case, it is probably easier to approach this working with resistance values first.

The example below works correctly.

To answer your questions:
Is this repeating for all 8 cells, difference in all 8 cells, does it take the mean? (8)?>>>No, it applies the function you supply to each pair of values.

Is it applying this calculation to all pairwise adjacent cells?
>>> Yes.

Jacob

require(gdistance)
DEM <- raster(system.file("external/maungawhau.grd",package = "gdistance"))

#Avoid Inf values

hd2 <- transition(DEM,function(x){abs(x[2] - x[1])},8)
matrixValues(hd2) <- "resistance"
slope2 <- geoCorrection(hd2)
transitionMatrix(slope2)[transitionMatrix(slope2)>0] <- 1 / transitionMatrix(slope2)[transitionMatrix(slope2)>0]
matrixValues(slope2) <- "conductance"

#Define some random points
A <- cbind(2667670, 6479000)
B <- cbind(2667800, 6479400)
pts <- rbind(A,B)

plot(DEM)
BA<-shortestPath(slope2,B,A,"SpatialLines")
AB<-shortestPath(slope2,A,B,"SpatialLines")
lines(BA)
lines(AB)
#Small differences 
#Dijkstra algorithm just gives one arbitrary shortest path -- more than one shortest path can exist

#Check
BATr <- shortestPath(slope2,B,A)
ABTr <- shortestPath(slope2,A,B)
sum(transitionMatrix((BATr*slope2)))
sum(transitionMatrix((ABTr*slope2)))
#Same length, as expected

#Profiles
shortestProfile1<-unlist(extract(DEM,AB))
shortestProfile2<-unlist(extract(DEM,BA))
plot(shortestProfile1,type='l')

plot(shortestProfile2,type='l')

#3D map
map3d <- persp(DEM, theta=160, phi=20)
coord <- coordinates(AB)[[1]][[1]]
z <- extract(DEM, coord)
lines (trans3d(coord[,1], coord[,2], z, pmat = map3d), col = 4, lwd=3)
#Routes looks for passes and zigzags a bit to keep slope low.




________________________________
 From: Ben Weinstein <benweinstein2010@gmail.com>
To: r-sig-geo <r-sig-geo@r-project.org> 
Sent: Monday, 3 September 2012, 18:19
Subject: [R-sig-Geo] Validating Cost Paths - minimizing elevation change

Hi everyone,

I'm trying to utilize the gdistance package to perform a least cost
analysis on elevation change. I'd like to find the cost of shortest path
that changes as little elvation as possible.

If it helps, think of it as someone trying carry a injured hiker on
a stretcher, flat is the way to go.

I've previously attempted to use the terrain "slope" function to build the
transition matrix, but the example below uses the code and
 structure
provided by the gdistance vignette. I hope it is fully reproducible. My
main concerns 1) Despite usually abs(), i get different paths depending on
origin/destination 2) The conductance values do not line up (in my crude
approximation)

require(gdistance)
#Create a Elev raster of the volcano

DEM<-raster(system.file("external/maungawhau.grd",package = "gdistance"))

#Using the method  given in the gdistance vignette?
#Is this repeating for all 8 cells, difference in all 8 cells, does it take
the mean? Is it applying this calculation to all pairwise adjacent cells
(8)?

hd1<-transition(DEM,function(x){1/abs(x[2] - x[1])},8)

#Incorporate distance across cell centers
slope1 <- geoCorrection(hd1)

#Define some random points
A <- cbind(2667670, 6479000)

B <- cbind(2667800, 6479400)

pts<-rbind(A,B)

#Lets look at it
 visually
plot(raster(slope1))
points(pts,col='red',pch=16)

plot(DEM)
BA<-shortestPath(slope1,B,A,"SpatialLines")
AB<-shortestPath(slope1,A,B,"SpatialLines")
lines(BA)
lines(AB)
#This is the first red flag, i've set an the transition matrix as an
absolute value, but i still get different shortest paths from AB and BA


#Get the elevation path from the shortest profile, this should roughly
relate to the diff in elevation along the path, given not infinite sampling
along the line
shortestProfile1<-unlist(extract(DEM,AB))
shortestProfile2<-unlist(extract(DEM,BA))

par(mfrow=c(1,2))
plot(shortestProfile1,type='l')
plot(shortestProfile2,type='l')


#Get the sum of the differences for the elevation profiles on the shortest
paths, a very approximate measure
sum(abs(diff(shortestProfile1, 1)))
sum(abs(diff(shortestProfile2, 1)))

#If the cell size is 10m,
 then  the conductance/10 should roughly be delta
elevation, ignoring diagonals have a longer distance?
costDistance(slope1,pts)/10

#This doesn't really seem to match the elevation profile, is there anyway
for me to get actual elevation to check the validity of the path?

As always, I appreciate the help

Ben Weinstein
-- 
Ben Weinstein
Graduate Student
Ecology and Evolution
Stony Brook University

http://life.bio.sunysb.edu/~bweinste/index.html

-- 
Ben Weinstein
Graduate Student
Ecology and Evolution
Stony Brook University

<http://life.bio.sunysb.edu/~bweinste/index.html>
<http://life.bio.sunysb.edu/~bweinste/index.html>
http://life.bio.sunysb.edu/~bweinste/index.html

    [[alternative HTML version deleted]]

_______________________________________________
R-sig-Geo mailing list
R-sig-Geo@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-sig-geo
	[[alternative HTML version deleted]]

