Reputation: 1
I am trying to compute a matrix distance between points using the function costDistance is the R package gdistance, taking into account the altitude with a transition matrix with a code like this:
read.csv("subset.csv",h=T)->dat
llCRS <- CRS("+proj=longlat +ellps=WGS84")
dat_mat<- cbind(dat$Long, dat$Lat)
dat_sp <- SpatialPointsDataFrame(dat_mat, dat, proj4string = llCRS)
raster("subset.grd")->alt
alt[is.na(alt)] <- 0
heightDiff <- function(x){x[2] - x[1]}
tr <- transition(alt,heightDiff,8,symm=FALSE)
pC <- as.matrix(dat[c("Longitud", "Latitud")])
cosDist <- costDistance(tr, pC)
and I get this error message
Error in .Call("R_igraph_shortest_paths", graph, v - 1, to - 1, as.numeric(mode), :
At structural_properties.c:5200 : cannot run Bellman-Ford algorithm, Negative loop detected while calculating shortest paths
If anyone knows why, it would be very helpful.
Upvotes: 0
Views: 750
Reputation: 203
In your example, the transition function should throw a warning that negative values occur.
This is how to avoid it:
heightDiff <- function(x){abs(x[2] - x[1])}
Normally you need to use function geoCorrection, something you omit above.
Upvotes: 1