Note that there are some explanatory texts on larger screens.

plurals
  1. POloop or sapply function for multiple least cost analysis in R
    primarykey
    data
    text
    <p>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:</p> <pre><code>Costpath&lt;-shortestPath(CostTrans,Cherangfirstloc.utm[1,],Cherangfirstloc.utm[132,], output="SpatialLines") </code></pre> <p>Thereby, <code>CostTrans</code> constitutes the costgrid, <code>Cherangfirstloc.utm[1,]</code> is the first location/point from a Spatialpoints dataframe (source) and <code>Cherangfirstloc.utm[132,]</code> is the last location/point from the Spatialpoints dataframe (destination). The output is a line connecting both locations/points.</p> <p>However, I now want to calculate <strong>multiple least cost paths</strong>, the source shall thereby be <strong>every row</strong> of the dataframe, the destination stays the same. This means the next source would be <code>Cherangfirstloc.utm[2,]</code>, then <code>Cherangfirstloc.utm[3,]</code> and so on. I think this can be done with a for loop or maybe a <code>sapply</code> function. Unfortunately, I don't know how to formulate this. </p> <p><strong>Could you give me any hints on how to formulate this iterative process?</strong> 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.</p> <p>Here is some code that can be used as sample data:</p> <pre><code>library(gdistance) r &lt;- raster(nrows=6, ncols=7, xmn=0, xmx=7, ymn=0, ymx=6, crs="+proj=utm +units=m") r[] &lt;- 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 &lt;- transition(r, function(x) 1/mean(x), 8) #creates transition layer of costgrid T &lt;- geoCorrection(T) #correction c1 &lt;- c(5.5,1.5) #first source point c2 &lt;- c(5.5,4) #second source point c3 &lt;- c(1.5,5.5) #destination sPath2 &lt;- shortestPath(T, c1, c3, output="SpatialLines") # creates the least cost path </code></pre> <p>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.</p> <p>I would appreciate if you could give me any hints on that. Thanks for your efforts! </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload