I am using arcgis 10.5.1 64 bit.
I have a set of 10 point features that I am trying to connect using PathDistance. Each point has an id, say 0...9. Effectively I want to generate the path from 0 to 1, 1 to 2, and so on.
I am using both my own tables to define vertical and horizontal factors. I have created these paths manually using the exact same factors. So I know those work.
The two main files I am using are: 1) a sample DEM (5m resolution, tif) called test_dem.tif 2) a point shapefile test_loc.shp which contains 11 points with several columns but the main one used here is 'Id' column. These files and others used for the script can be found below.
The files used for the vertical and horizontal factors are the following,
I have the following code:
import arcpy.sa as sa
import arcpy.management as mng
from math import pi
# Checking out SA extension
if arcpy.CheckExtension('Spatial') == 'Available':
print('Spatial Analyst license is unavailable!')
# ArcGIS directories
arcpy.env.scratchWorkspace = r'C:\GIS\projects\network\potential\SCRATCH'
arcpy.env.workspace = r'C:\GIS\projects\network\potential'
# input rasters
rDEM = arcpy.Raster(r'C:\GIS\projects\network\potential\tst_dem.tif')
shpFeat = r'C:\GIS\projects\network\potential\testloc.shp'
deg2rad = pi/180.0
rad2deg = 180.0/pi
wFactor = 0.2
I = 4.0
wPthPot = 0.75
Gmax = 5.0
# path distance parameters
rCost = "" # use unit
rSurface = rDEM
rVertical = rDEM
hCostfn = r'C:\GIS\projects\network\potential\hsine.txt'
HF = sa.HfTable(hCostfn)
vCostfn = r'C:\GIS\projects\network\potential\vertical3.txt'
VF = sa.VfTable(vCostfn)
maxDist = ""
nbrfn = r'C:\GIS\projects\network\potential\expd.txt'
#Set geoprocessing environmental preferences
arcpy.env.extent = rDEM.extent
arcpy.env.cellSize = rDEM.meanCellHeight
# INITIAL CONDITIONS
rTPot = rDEM / rDEM.maximum
rDirRel = rTPot
rG0 = sa.CreateConstantRaster(0.0, 'FLOAT', arcpy.env.cellSize, arcpy.env.extent)
rGmax = sa.CreateConstantRaster(Gmax, 'FLOAT', arcpy.env.cellSize, arcpy.env.extent)
rGt_1 = sa.CreateConstantRaster(0.0, 'FLOAT', arcpy.env.cellSize, arcpy.env.extent)
# sample sequence
pthseq = list(range(11))
# create a feature layer
for o,d in zip(pthseq[:-1], pthseq[1:]):
# lambda direction
rDir = sa.Aspect(rDirRel)
# CALCULATE PATH
mng.SelectLayerByAttribute('lyr', "NEW_SELECTION", ' "Id" = '+str(o)) # origin
# accumulated cost surface
rAcs= sa.PathDistance('lyr', "", rSurface, rDir, HF, rVertical, VF, "", 'rBL.tif')
mng.SelectLayerByAttribute('lyr', "NEW_SELECTION", ' "Id" = '+str(d)) # destination
rPath = sa.CostPath('lyr', rAcs, 'rBL.tif', "EACH_CELL")
rPath = sa.Con(sa.IsNull(rPath), 0, 1)
pathFn = 'path'+str(o)+'_'+str(d)+'.tif'
rW = (rG0 - rGt_1) * wFactor
rMark = rPath * (1.0 - (rGt_1 / rGmax)) * I
rGt = rGt_1 + rW + rMark
rPthPot = sa.FocalStatistics(rGt, sa.NbrWeight(nbrfn),'SUM')
rPthPot = rPthPot.maximum - rPthPot
rPthPot = rPthPot / rPthPot.maximum
# Update directional relation
rDirRel = rPthPot * wPthPot + rTPot * (1.0 - wPthPot)
# Update rGt_1 *** missing earlier ***
rGt_1 = rGt
Overall, I get very weird behavior. Some times, it will fail when trying to generate the backlink. In other occasions, it will calculate the first path and then fail. It will calculate three paths and fail. Only once it has ran through the entire sequence. After crashing I make sure to delete all possible files (usually the backlink file is created but it is corrupted).
I have tried many things:
1. Creating a fresh feature layer and erasing it at the end of each iteration.
2. Generating with each iteration different feature layers, one for the origin and another for the destination, and deleting them after each iteration.
3. I have tried saving to the disk the layer feature as well as all the other rasters (I set arcpy to overwrite).
4. I have tried saving to the disk the backlink
5. I have used relative and absolute filenames.
6. I am using only .tif files
7. I have use try...except to catch information about the error with no success
I have spent much time reading through the different forums, etc. I know there were problems with this method in previous iterations of the software. The lack of any feedback relating to possible errors is a real problem! Often it will be python that crashes all together. It is very finicky, there are certain Horizontal Factor settings (even those found in the help pages) that will not generate a complete cost accumulation raster. This is not my case though.
My question is whether there is something that I am missing, something that needs to be reset or deleted? or perhaps someone can suggest how to find more information after the procedure fails???