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 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:
<SPAN class="keyword token">import</SPAN> arcpy
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>sa <SPAN class="keyword token">as</SPAN> sa
<SPAN class="keyword token">import</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>management <SPAN class="keyword token">as</SPAN> mng
<SPAN class="keyword token">from</SPAN> math <SPAN class="keyword token">import</SPAN> pi
<SPAN class="comment token"># Checking out SA extension</SPAN>
<SPAN class="keyword token">class</SPAN> <SPAN class="token class-name">SAerror</SPAN><SPAN class="punctuation token">(</SPAN>Exception<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">pass</SPAN>
<SPAN class="keyword token">try</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">if</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>CheckExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">==</SPAN> <SPAN class="string token">'Available'</SPAN><SPAN class="punctuation token">:</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>CheckOutExtension<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">else</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">raise</SPAN> SAerror
<SPAN class="keyword token">except</SPAN> SAerror<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN><SPAN class="string token">'Spatial Analyst license is unavailable!'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="keyword token">except</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>ExecuteError<SPAN class="punctuation token">:</SPAN>
<SPAN class="keyword token">print</SPAN><SPAN class="punctuation token">(</SPAN>arcpy<SPAN class="punctuation token">.</SPAN>GetMessages<SPAN class="punctuation token">(</SPAN><SPAN class="number token">2</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># ArcGIS directories</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>scratchWorkspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential\SCRATCH'</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>workspace <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential'</SPAN>
<SPAN class="comment token"># input rasters</SPAN>
rDEM <SPAN class="operator token">=</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>Raster<SPAN class="punctuation token">(</SPAN>r<SPAN class="string token">'C:\GIS\projects\network\potential\tst_dem.tif'</SPAN><SPAN class="punctuation token">)</SPAN>
shpFeat <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential\testloc.shp'</SPAN>
<SPAN class="comment token"># PARAMETERS</SPAN>
deg2rad <SPAN class="operator token">=</SPAN> pi<SPAN class="operator token">/</SPAN><SPAN class="number token">180.0</SPAN>
rad2deg <SPAN class="operator token">=</SPAN> <SPAN class="number token">180.0</SPAN><SPAN class="operator token">/</SPAN>pi
wFactor <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.2</SPAN>
I <SPAN class="operator token">=</SPAN> <SPAN class="number token">4.0</SPAN>
wPthPot <SPAN class="operator token">=</SPAN> <SPAN class="number token">0.75</SPAN>
Gmax <SPAN class="operator token">=</SPAN> <SPAN class="number token">5.0</SPAN>
<SPAN class="comment token"># path distance parameters</SPAN>
rCost <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN> <SPAN class="comment token"># use unit</SPAN>
rSurface <SPAN class="operator token">=</SPAN> rDEM
rVertical <SPAN class="operator token">=</SPAN> rDEM
hCostfn <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential\hsine.txt'</SPAN>
HF <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>HfTable<SPAN class="punctuation token">(</SPAN>hCostfn<SPAN class="punctuation token">)</SPAN>
vCostfn <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential\vertical3.txt'</SPAN>
VF <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>VfTable<SPAN class="punctuation token">(</SPAN>vCostfn<SPAN class="punctuation token">)</SPAN>
maxDist <SPAN class="operator token">=</SPAN> <SPAN class="string token">""</SPAN>
nbrfn <SPAN class="operator token">=</SPAN> r<SPAN class="string token">'C:\GIS\projects\network\potential\expd.txt'</SPAN>
<SPAN class="comment token">#Set geoprocessing environmental preferences</SPAN>
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>extent <SPAN class="operator token">=</SPAN> rDEM<SPAN class="punctuation token">.</SPAN>extent
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>cellSize <SPAN class="operator token">=</SPAN> rDEM<SPAN class="punctuation token">.</SPAN>meanCellHeight
arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>overwriteOutput<SPAN class="operator token">=</SPAN> <SPAN class="token boolean">True</SPAN>
<SPAN class="comment token"># INITIAL CONDITIONS</SPAN>
rTPot <SPAN class="operator token">=</SPAN> rDEM <SPAN class="operator token">/</SPAN> rDEM<SPAN class="punctuation token">.</SPAN>maximum
rDirRel <SPAN class="operator token">=</SPAN> rTPot
rG0 <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>CreateConstantRaster<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FLOAT'</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>cellSize<SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">)</SPAN>
rGmax <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>CreateConstantRaster<SPAN class="punctuation token">(</SPAN>Gmax<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FLOAT'</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>cellSize<SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">)</SPAN>
rGt_1 <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>CreateConstantRaster<SPAN class="punctuation token">(</SPAN><SPAN class="number token">0.0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'FLOAT'</SPAN><SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>cellSize<SPAN class="punctuation token">,</SPAN> arcpy<SPAN class="punctuation token">.</SPAN>env<SPAN class="punctuation token">.</SPAN>extent<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># sample sequence</SPAN>
pthseq <SPAN class="operator token">=</SPAN> list<SPAN class="punctuation token">(</SPAN>range<SPAN class="punctuation token">(</SPAN><SPAN class="number token">11</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># create a feature layer</SPAN>
mng<SPAN class="punctuation token">.</SPAN>MakeFeatureLayer<SPAN class="punctuation token">(</SPAN>shpFeat<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'lyr'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># MAIN</SPAN>
<SPAN class="keyword token">for</SPAN> o<SPAN class="punctuation token">,</SPAN>d <SPAN class="keyword token">in</SPAN> zip<SPAN class="punctuation token">(</SPAN>pthseq<SPAN class="punctuation token">[</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="operator token">-</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">,</SPAN> pthseq<SPAN class="punctuation token">[</SPAN><SPAN class="number token">1</SPAN><SPAN class="punctuation token">:</SPAN><SPAN class="punctuation token">]</SPAN><SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">:</SPAN>
<SPAN class="comment token"># lambda direction</SPAN>
rDir <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>Aspect<SPAN class="punctuation token">(</SPAN>rDirRel<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># CALCULATE PATH</SPAN>
mng<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'lyr'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">' "Id" = '</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>o<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># origin</SPAN>
<SPAN class="comment token"># accumulated cost surface</SPAN>
rAcs<SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>PathDistance<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'lyr'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> rSurface<SPAN class="punctuation token">,</SPAN> rDir<SPAN class="punctuation token">,</SPAN> HF<SPAN class="punctuation token">,</SPAN> rVertical<SPAN class="punctuation token">,</SPAN> VF<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">""</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'rBL.tif'</SPAN><SPAN class="punctuation token">)</SPAN>
mng<SPAN class="punctuation token">.</SPAN>SelectLayerByAttribute<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'lyr'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"NEW_SELECTION"</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">' "Id" = '</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="comment token"># destination</SPAN>
<SPAN class="comment token"># path</SPAN>
rPath <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>CostPath<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'lyr'</SPAN><SPAN class="punctuation token">,</SPAN> rAcs<SPAN class="punctuation token">,</SPAN> <SPAN class="string token">'rBL.tif'</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="string token">"EACH_CELL"</SPAN><SPAN class="punctuation token">)</SPAN>
rPath <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>Con<SPAN class="punctuation token">(</SPAN>sa<SPAN class="punctuation token">.</SPAN>IsNull<SPAN class="punctuation token">(</SPAN>rPath<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">0</SPAN><SPAN class="punctuation token">,</SPAN> <SPAN class="number token">1</SPAN><SPAN class="punctuation token">)</SPAN>
pathFn <SPAN class="operator token">=</SPAN> <SPAN class="string token">'path'</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>o<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">'_'</SPAN><SPAN class="operator token">+</SPAN>str<SPAN class="punctuation token">(</SPAN>d<SPAN class="punctuation token">)</SPAN><SPAN class="operator token">+</SPAN><SPAN class="string token">'.tif'</SPAN>
rPath<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN>pathFn<SPAN class="punctuation token">)</SPAN>
rW <SPAN class="operator token">=</SPAN> <SPAN class="punctuation token">(</SPAN>rG0 <SPAN class="operator token">-</SPAN> rGt_1<SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> wFactor
rMark <SPAN class="operator token">=</SPAN> rPath <SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1.0</SPAN> <SPAN class="operator token">-</SPAN> <SPAN class="punctuation token">(</SPAN>rGt_1 <SPAN class="operator token">/</SPAN> rGmax<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">)</SPAN> <SPAN class="operator token">*</SPAN> I
rGt <SPAN class="operator token">=</SPAN> rGt_1 <SPAN class="operator token">+</SPAN> rW <SPAN class="operator token">+</SPAN> rMark
rGt<SPAN class="punctuation token">.</SPAN>save<SPAN class="punctuation token">(</SPAN><SPAN class="string token">'rGt.tif'</SPAN><SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># DECAY</SPAN>
rPthPot <SPAN class="operator token">=</SPAN> sa<SPAN class="punctuation token">.</SPAN>FocalStatistics<SPAN class="punctuation token">(</SPAN>rGt<SPAN class="punctuation token">,</SPAN> sa<SPAN class="punctuation token">.</SPAN>NbrWeight<SPAN class="punctuation token">(</SPAN>nbrfn<SPAN class="punctuation token">)</SPAN><SPAN class="punctuation token">,</SPAN><SPAN class="string token">'SUM'</SPAN><SPAN class="punctuation token">)</SPAN>
rPthPot <SPAN class="operator token">=</SPAN> rPthPot<SPAN class="punctuation token">.</SPAN>maximum <SPAN class="operator token">-</SPAN> rPthPot
rPthPot <SPAN class="operator token">=</SPAN> rPthPot <SPAN class="operator token">/</SPAN> rPthPot<SPAN class="punctuation token">.</SPAN>maximum
<SPAN class="comment token"># Update directional relation</SPAN>
rDirRel <SPAN class="operator token">=</SPAN> rPthPot <SPAN class="operator token">*</SPAN> wPthPot <SPAN class="operator token">+</SPAN> rTPot <SPAN class="operator token">*</SPAN> <SPAN class="punctuation token">(</SPAN><SPAN class="number token">1.0</SPAN> <SPAN class="operator token">-</SPAN> wPthPot<SPAN class="punctuation token">)</SPAN>
<SPAN class="comment token"># Update rGt_1 *** missing earlier *** </SPAN>
rGt_1 <SPAN class="operator token">=</SPAN> rGt<SPAN class="line-numbers-rows"><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN><SPAN></SPAN></SPAN>
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