AnsweredAssumed Answered

Path Distance erratic and crashes in a loop

Question asked by mllo1 on Oct 25, 2017
Latest reply on Nov 6, 2017 by xander_bakker

Hi,

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,

 

hsine.txtvertical3.txt

0 1
10 1.02
20 1.06
30 1.13
40 1.23
50 1.36
60 1.5
70 1.66
80 1.83
90 2
100 2.17
110 2.34
120 2.5
130 2.64
140 2.77
150 2.87
160 2.94
170 2.98
180 3

-90 20000
-80 5000
-70 1500
-60 300
-50 100
-40 40
-30 12
-20 3
-10 1
0 3
10 7
20 14
30 24
40 40
50 75
60 180
70 750
80 1500
90 5000

 

I have the following code:

import arcpy
import arcpy.sa as sa
import arcpy.management as mng
from math import pi

# Checking out SA extension
class SAerror(Exception):
    pass
try:
    if arcpy.CheckExtension('Spatial') == 'Available':
       arcpy.CheckOutExtension('Spatial')
    else:
       raise SAerror
except SAerror:
    print('Spatial Analyst license is unavailable!')
except arcpy.ExecuteError:
    print(arcpy.GetMessages(2))

# 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'

# PARAMETERS
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
arcpy.env.overwriteOutput= True

# 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
mng.MakeFeatureLayer(shpFeat, 'lyr')

# MAIN
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

    # path
    rPath = sa.CostPath('lyr', rAcs, 'rBL.tif', "EACH_CELL")
    rPath = sa.Con(sa.IsNull(rPath), 0, 1)
    pathFn = 'path'+str(o)+'_'+str(d)+'.tif'
    rPath.save(pathFn)

    rW = (rG0 - rGt_1) * wFactor
    rMark = rPath * (1.0 - (rGt_1 / rGmax)) * I
    rGt = rGt_1 + rW + rMark
    rGt.save('rGt.tif')

    # DECAY
    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???

 

thank you,

 

Marc  

Attachments

Outcomes