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.txt | vertical3.txt |
---|---|
0 1 | -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
Your script doesn't set any environment factors such as extent, cell size, snap raster etc. Try to explicitly set those prior to working with rasters to remove the potential for erratic behaviour, particularly if you are experiencing it between runs of the same code.
Thanks,
Indeed, all of that was done previously. All environmental variables and working directories were set up before creating anything or making any selection.
So to clarify, there are parts of your script not present? Global settings in arcmap need nor persist even within a session.
Yes... correct I did not include the rest of the code. Up to this point it had to do with several settings. Everything runs fine up to there (I have check this several times) it is only when I get into the loop that I encounter the behavior I described. Some times it runs fine other it does not.
Could you elaborate on what you mean that 'Global settings in arcmap need nor persist...'. Presumably if I define environmental and set overwrite to True within a script, those will be preserved,no?
No... things like
But check each tools' script and Environments section (bottom of each page) to see which environments are supportes
Did that... is there any known memory bug with this routine? I just cannot understand why is so unpredictable!
Esri support... PathDistance
Thanks... there does not seem to be anything there that seems to point to any causes or solutions. Thanks nonetheless.
I think first it would be best to share the entire script, since the error may be caused somewhere else. Also when sharing code use instructions like those described here: Posting code with Syntax Highlighting on GeoNet
I just copied you snippet of code and several syntax errors occurred:
I'm sure those are not the errors that are causing your problems (since the code would not execute with those errors) but it is important to understand that it will make it much more difficult to help in case you don't share the actual code.
In case possible, share a part of your data too so we can see if the error or any strange behavior can be reproduced.