Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6811409
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:19:25+00:00 2026-05-26T20:19:25+00:00

I am using the package gdistance for a least cost analysis. The idea is

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:19:26+00:00Added an answer on May 26, 2026 at 8:19 pm

    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 ###
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to filter stop-words from the following documents using package tm .
I'm using wikitools package to parse the wikipedia. I just copy this example from
So I was compiling a package using javac -Xlint from the command prompt and
I am wondering how to start an another app from my app using package
Hi I am using package-maker of Mac OS to create installer package for Mac
When I am using subfig package in latex, it gives some errors: Package subfig
I have created an installation package using Wix which installs a Windows service on
I'm calling a SSIS package using LoadPackage(...). Is it possible to make this call
I am building a Setup Package using VS2008. This is a regular setup package
I'm monkey patching a package using a technique given at the beginning of How

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.