R 中用于多重最低成本分析的循环或 sapply 函数

发布于 2024-12-15 05:34:43 字数 1559 浏览 3 评论 0原文

我正在使用 gdistance 包进行最低成本分析。 这个想法是通过具有定义的成本值的成本网格(栅格)确定从目标点到源的路径;因此,该路径会避开具有高成本的像素并优先选择具有低成本值的像素。适用于我的数据的代码是:

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

因此,CostTrans 构成成本网格,Cherangfirstloc.utm[1,] 是 Spatialpoints 数据帧中的第一个位置/点(源),Cherangfirstloc.utm[132,] 是 Spatialpoints 数据帧(目的地)中的最后一个位置/点。输出是连接两个位置/点的线。

但是,我现在想要计算多个最低成本路径,因此源应为数据帧的每一行,目的地保持不变。这意味着下一个源将是 Cherangfirstloc.utm[2,],然后是 Cherangfirstloc.utm[3,],依此类推。我认为这可以通过 for 循环或 sapply 函数来完成。不幸的是,我不知道如何表述这一点。

您能给我一些关于如何制定这个迭代过程的提示吗? 如果我在这个地方问这个问题,我希望没关系。基本上,我只想知道如何迭代数据框。因此,gdistance 和最低成本分析如何工作并不重要。

以下是一些可用作示例数据的代码:

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

不幸的是,我不知道如何将 c1、c2 和 c3 包含在 Spatialpoints 数据框中,以便可以迭代。希望这仍然有帮助。

如果您能给我任何提示,我将不胜感激。感谢您的努力!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

像你 2024-12-22 05:34:43

如果您只想使用不同的参数调用 shortestPath 函数,最简单的解决方案是使用 for 循环,如下所示。由于我不熟悉这种分析,我不知道你想对结果做什么,所以这里有两个解决方案:

在这个例子中你将立即使用最短路径,因为它会在下一个被忘记步骤:

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 ###
}

在这个中,所有路径都将存储在一个列表中(似乎 shortestPath 函数返回一个对象,因此它是更方便的存储方式),每个结果都可以在 >results 变量,例如从第 12 行开始的路径的 results[[12]]

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 ###

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 ###
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文