0

I am doing a least cost path analysis. I need to create a for loop that will run 3 times and select the correct weight from each of the two lists (slope and land).

Slope = [10, 20, 30]
Land = [30, 20, 10]
import arcpy
from arcpy import env
from arcpy.sa import *
env.workspace = (c:\)
arcpy.CheckOutExtension("Spatial")
#------------------------------------------------- Times calculation
outTimes10 = Times("Slope",10)
outTimes20 = Times("Slope",20)
outTimes30 = Times("Slope",30)
#------------------------------------------------- Times calculation
outland30 = Times("land_tif", 30)
outland20 = Times("land_tif", 20)
outland10 = Times("land_tif", 10)
#------------------------------------------------- Added two raster together
outPlus1030 = Plus("outTimes10", "outland30")
outPlus2020 = Plus("outTimes20", "outland20")
outPlus3010 = Plus("outTimes30", "outland10")
#------------------------------------------------- Cost Back link
outBacklink = CostBackLink("Hiker.shp","outPlus1030", 100000, 
 "c:/sapyexamples/output/distRast")
pair1bl="CostBac_shp1"
pair2bl="CostBac_shp2"
pair3bl="CostBac_shp3"
#------------------------------------------------- Cost distance
outCostDist = CostDistance("Hiker.shp", "slope", 200000, "pair1bl")
pair1cd="CostDis_shp1"
pair2cd="CostDis_shp2"
pair3cd="CostDis_shp3"
#------------------------------------------------- Least cost path
outCostPath = CostPath("Hiker.shp", "outplus1030", "pair1bl", "EACH_CELL")
path1="CostPat_shp1"
path2="CostPat_shp2"
path3="CostPat_shp3"

I am new to python. I need help with creating a for loop that will select numbers from the two list do the times calculation, plus calculation, cost backlink, cost distance, and least cost path.

I do not know how to direct the output of the calculation to be saved in a different file name everytime the loop runs.

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Nov 11, 2018 at 4:31

1 Answer 1

1

You can zip the lists together and iterate over each slopevalue, landvalue and a name:

#More code above
arcpy.env.overwriteOutput = True
Slope = [10, 20, 30]
Land = [30, 20, 10]
Name = ['Out1','Out2','Out3']
for slopeval,landval,name in zip(Slope,Land,Name):
 outTimes = Times("Slope",slopeval)
 outTimes.save('outTimes.tif') #Dont know if saving is required or if it is possible to use the raster object as input to next steps.
 outland = Times("land_tif", landval)
 outland.save('outland.tif')
 outPlus = Plus('outTimes.tif', 'outland.tif')
 outPlus.save('outplus.tif')
 outBacklink = CostBackLink("Hiker.shp","outplus", 100000, 
 "c:/sapyexamples/output/{0}".format(name+'.tif'))
 #More code here
answered Nov 11, 2018 at 8:07
2
  • 1
    Never used that zip() function, nice example bringing 3 values together, thanks! Commented Nov 11, 2018 at 19:14
  • I get the error zip argument #1 must support iteration. How to fix this error? I never tried this before. @BERA Commented Nov 12, 2018 at 22:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.