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.

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
  • 1
    Never used that zip() function, nice example bringing 3 values together, thanks! – Hornbydd Nov 11 '18 at 19:14
  • I get the error zip argument #1 must support iteration. How to fix this error? I never tried this before. @BERA – random123 Nov 12 '18 at 22:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.