Вопрос

I am using the package gdistance for a least cost analysis. The idea is to determine the path from a destination point to a source over a costgrid (raster) with defined cost values; the path thereby avoids pixels with high costs and prefers pixels with low cost values. The code that works for me with my data is:

Costpath<-shortestPath(CostTrans,Cherangfirstloc.utm[1,],Cherangfirstloc.utm[132,], output="SpatialLines")

Thereby, CostTrans constitutes the costgrid, Cherangfirstloc.utm[1,] is the first location/point from a Spatialpoints dataframe (source) and Cherangfirstloc.utm[132,] is the last location/point from the Spatialpoints dataframe (destination). The output is a line connecting both locations/points.

However, I now want to calculate multiple least cost paths, the source shall thereby be every row of the dataframe, the destination stays the same. This means the next source would be Cherangfirstloc.utm[2,], then Cherangfirstloc.utm[3,] and so on. I think this can be done with a for loop or maybe a sapply function. Unfortunately, I don't know how to formulate this.

Could you give me any hints on how to formulate this iterative process? I hope it is ok, if I ask this question in this place. Basically, I just want to know how to iterate through the dataframe. How gdistance and the least cost analysis work is thereby unimportant.

Here is some code that can be used as sample data:

library(gdistance)

r <- raster(nrows=6, ncols=7, xmn=0, xmx=7, ymn=0, ymx=6, crs="+proj=utm 
+units=m")

r[] <- c(2, 2, 1, 1, 5, 5, 5, #creates costgrid
 2, 2, 8, 8, 5, 2, 1,
 7, 1, 1, 8, 2, 2, 2,
 8, 7, 8, 8, 8, 8, 5,
 8, 8, 1, 1, 5, 3, 9,
 8, 1, 1, 2, 5, 3, 9)

T <- transition(r, function(x) 1/mean(x), 8) #creates transition layer of costgrid
T <- geoCorrection(T) #correction

c1 <- c(5.5,1.5) #first source point
c2 <- c(5.5,4) #second source point
c3 <- c(1.5,5.5) #destination

sPath2 <- shortestPath(T, c1, c3, output="SpatialLines") # creates the least cost path

Unfortunately, I did not know how to include c1, c2 and c3 in a Spatialpoints dataframe so that one can iterate through. Hope this still helps.

I would appreciate if you could give me any hints on that. Thanks for your efforts!

Это было полезно?

Решение

If you just want to call the shortestPath function with a varying argument, the simpliest solution is to use a for loop like the following. As i am not familiar with this kind of analysis, i don't know what you want to do with the results, so here are two solutions :

In this example you will use the shortest path immediatly, as it will be forgotten at the next step :

for(i in 1:nrow(Cherangfirstloc.utm)) {
  # Computation
  Costpath <- shortestPath(CostTrans, Cherangfirstloc.utm[i,], Cherangfirstloc.utm[132,], output="SpatialLines") 

  ### Here your instructions for a direct use of the result ###
}

In this one all the paths will be stored in a list (it seems that the shortestPath function returns an object so it is the more convenient storage way), each result will be accessible in the results variable, e.g. results[[12]] for the path beginning at the 12th row :

results = list()
for(i in 1:nrow(Cherangfirstloc.utm)) {
  # Computation, storage in a list element
  results[[i]] <- shortestPath(CostTrans, Cherangfirstloc.utm[i,], Cherangfirstloc.utm[132,], output="SpatialLines") 
}

### Here your instructions for a global use of the result ###
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top