I am attempting to create a least cost path using ArcPy for automation. I have the starting/end points and cost surface raster already assembled, so I am just using ArcPy to automate through my dataset. Upon running the script, though, I receive an error at line 24: "CostDist = arcpy.sa.CostDistance(...".

Here is the code block:

import os
import arcpy
from arcpy.sa import *
arcpy.env.overwriteOutput= True
arcpy.env.extent = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3\\Tide_Extent3.shp')

arcpy.env.workspace = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3')
TargetWorkspace = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3')

SourcePoints =  ("H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3\\Tide_Extent3_Source_Points.shp")
DestinationPoints = ("H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3\\Tide_Extent3_Destination_Points.shp")
CostSurface = ("H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3\\Tide_Extent_3_CostSurface.tif")

CostDist = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3')
Backlink = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3')
CostPath = ('H:\\IAS GIS Information\\Geodatabase\\Island_Isolation\\Tidal_Stream\\Tide_LCP\\Extent_3')

for i in range (int(arcpy.GetCount_management(DestinationPoints).getOutput(0))): #Use the "getOutput() to start the FID count"
    if os.path.exists(Backlink + str(i)):
       os.remove(Backlink + str(i))
    arcpy.MakeFeatureLayer_management(DestinationPoints, "endpointA" + str(i), ' "IN_FID_A" = ' + str(i))
    arcpy.MakeFeatureLayer_management(SourcePoints, "sourcepointA" + str(i), ' "IN_FID_A" = ' + str(i))
    CostDist = arcpy.sa.CostDistance("endpointA" + str(i), CostSurface, out_backlink_raster = "BackLinkA"+str(i))
    CostDist.save("CostDistA" + str(i))
    CostPath = arcpy.sa.CostPath("sourcepointA" + str(i), "CostDistA" + str(i), "BackLinkA" + str(i))
    CostPath.save("CostPathA" + str(i))

The execution error reads: "ExecuteError: ERROR 999999: Error executing function. Failed to create raster dataset Failed to execute (CostDistance)."

I have used this code previously with no issues so I am not sure about what is making the script fail.

  • Does the field IN_FID_A exist and is it populated with unique and contiguous numbers with a starting value of 0? It's possible that one of your input layers is empty which may be causing the error. As your input DestinationPoints is a shapefile you could calc IN_FID_A = FID to ensure contiguity. – Michael Stimson May 31 '18 at 2:56
  • 2
    If the code's the same as worked before then maybe it's the data. I think you should re-test your code against data that you know worked previously to see if it still does. – PolyGeo May 31 '18 at 2:59
  • IN_FID_A does exist and is populated properly- I used the FID field to provide unique numbers. As for re-testing the code with previous data, the code still fails and provides the same error. A backlink raster is created, though, but it is empty and cannot be deleted in Arc. I have had to go to the actual file and delete it manually. Pretty odd. – Zach May 31 '18 at 3:14
  • That sounds like a locking situation, do you have more than one Esri session open (Arc Map, catalog, scene)? Perhaps a quick reboot might solve the problem (can't hurt). – Michael Stimson May 31 '18 at 3:24
  • I thought the same thing too- I restarted ArcMap and even rebooted the entire computer but that didn't work either! Good Thought though. – Zach May 31 '18 at 3:33

The issue appeared to have been associated with the file location(s): I was able to resolve the problem by moving all associated files to a different directory. As Keagan Allan May eluded to, the issue may have been with having spaces in the previous directory. Thanks for the help everyone!

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.