I have a feature dataset with 317 points (UpperPoints) and another with 394 (LowerPoints) points. Now I want to calculate the cost paths as in this example here. However there are multiple files used and i just want to iterate trough my shapefiles. In the end I want the cost paths from every single UpperPoint to every single LowerPoint. I know that I have to calculate the cosDistance rasters for every lower Point which will need a lot of computation time, but that's not an issue. My main problem is how to extract the points from the shapefiles and iterate trough these to create the CostDistance rasters. I assume that my solution, just using the for loop is too simplistic...

PointsHIGH = "C:\\Dogon\\lcp.gdb\\Pts_rangeH_high"
PointsLOW = "C:\\Dogon\\lcp.gdb\\Pts_rangeH_low"
costsurface = ("C:\\Dogon\\lcp.gdb\\Slope_asc")

outputCostDist = "C:\\Dogon\\lcp.gdb\\CstDstLOW"
outputCostBackLink = "C:\\Dogon\\lcp.gdb\\BckLnkLOW"
outputCostPath = "C:\\Dogon\\lcp.gdb\\CostPath"

for points in PointsLOW
    outputCostDist = arcpy.gp.CostDistance_sa(points, costsurface, outputCostBackLink)
    outputCostPath = arcpy.gp.CostPath(points, outputCostDist, outputCostBackLink,"BEST_SINGLE")

Further I want an output of all CostDistance rasters and CostPaths, how could I do this?


you can loop on a selection of each point in a layer.

for i in range(int(arcpy.GetCount_management(PointsLOW).getOutput(0)):
    arcpy.MakeFeatureLayer_management(PointsLOW, "point" + str(i), ' "OID" = ' + str(i)) 
    outputCostDist = arcpy.gp.CostDistance_sa("point" + str(i), costsurface + str(i), outputCostBackLink + str(i))
    outputCostDist.save("yourname" + str(i))
  • Ok, thanks. But then i would have to define every i-th output beforehand. How would you do that? inside the loop?
    – simoet
    May 13 '14 at 13:18
  • Not sure that I understand your comment. Note that in my example, str(i) makes sure that the all the names are different (would be nicer with .format(), but I thought that this would be easier to read ) . If you want specific names, you can build a list by hand, e.g. names= ["name1", "name2", etc] then call each name from the list with names[i] . If you build this list with a loop, it is probably not necessary to use a list (you can create the names on the fly, Python is great for this).
    – radouxju
    May 13 '14 at 13:34
  • Thanks, well i basically just want an output for every file that i create. This does it to some extent. However I think I use another python version, it looks at the costsurface output as the input raster, but otherwise it looks good
    – simoet
    May 13 '14 at 13:46
  • I've added the line of code to save your rster as permanent. In the future, try to mention your ArcGIS version if you need exact code.
    – radouxju
    May 13 '14 at 14:13

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.