1

I am attempting to run a cost distance analysis where my list of source points (roughly 100 points) corresponds to the same number of destination points. Instead of running the cost distance analysis on each pair of points individually, I was hoping to run a script to do it for me. Here is the code below:

    import arcpy
arcpy.CheckOutExtension("spatial")
arcpy.env.workspace = 'H:\\IASGIS\PythonScripts\\CostSurfaceLoops.gdb'
TargetWorkspace = r'H:\\IASGIS\\Geodatabase\\PythonScripts\\CostSurfaceLoops.gdb'

SourcePoints =  "H:\\IASGIS\\PythonScripts\\CostSurfaceLoops.gdb\\PFNZ_Northland_SOURCE_Points_FID_TEST"
DestinationPoints = "H:\\IASGIS\\Geodatabase\\PythonScripts\\CostSurfaceLoops.gdb\\PFNZ_Northland_DESTINATION_Points_FID_TEST"
CostSurface = ("H:\\IASGIS\\Geodatabase\\Island_Isolation\\CostSurface\\PFNZ_CostSurface\\PFNZ_Northland_CostSurface.tif")


TestCostDist = "H:\\IASGIS\\PythonScripts\\CostSurfaceLoops.gdb\\CostDistSource"
TestBacklink = "H:\\IASGIS\\PythonScripts\\CostSurfaceLoops.gdb\\BacklinkSource"
TestCostPath = "H:\\IASGIS\\PythonScripts\\CostPathTest"

int(arcpy.GetCount_management(SourcePoints).getOutput(0))

for i in range (int(arcpy.GetCount_management(SourcePoints).getOutput(0))):
    arcpy.MakeFeatureLayer_management(SourcePoints, "point" + str(i), ' "IN_FID" = ' + str(i))
    TestCostDist = arcpy.gp.CostDistance_sa("point" + str(i), CostSurface + str(i), TestBacklink + str(i))
    TestCostDist.save("CostSurface_Test" + str(i))

How do I solve the issue using ArcGIS 10.3?

When I run the output, I receive the following error:

      Failed to execute. Parameters are not valid.
ERROR 000865: Input cost raster: H:\IASGIS\Geodatabase\Island_Isolation\CostSurface\PFNZ_CostSurface\PFNZ_Northland_CostSurface.tif0 does not exist.
Failed to execute (CostDistance).
  • 1
    Shouldn't CostSurface + str(i) be just CostSurface and TestBacklink + str(i) be TestBacklink? It's saying that it can't find the file with the extension .tif0. – Michael Stimson Feb 13 '18 at 2:08
  • I'd like to create a separate cost surface and backlink layer for each set of points point because they are at different locations. As a result I was looking to keep the "str(i)" within the code to iterate through the different points. I also tried your recommendation and received the same error, unfortunately. I appreciate your input! – Zach Feb 13 '18 at 2:20
  • 1
    Try CostSurface.replace('.tif','{}.tif'.format(i)) to change .tif0 to 0.tif etc.. however what the error message is saying is that it can't find the input raster H:\...\PFNZ_Northland_CostSurface.tif which relates to the input variable CostSurface – Michael Stimson Feb 13 '18 at 2:44
  • Would you mind providing a bit more context for this? I apologise, I am quite new to Python and ArcPy! – Zach Feb 13 '18 at 3:18
  • 1
    That's fine, we were all beginners once, you file isn't called PFNZ_Northland_CostSurface.tif0 which is why it's having a problem, your straight concatenation (joining) of CostSurface + str(i) just adds '0' to the end of the string, instead use replace to insert the 0 before the .tif so that it points (assumedly) to a file; there are better methods for working with file name/extensions have a google search for os.path.splitext for more info. – Michael Stimson Feb 13 '18 at 3:41

Your Answer

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

Browse other questions tagged or ask your own question.