Using Trace by AH Attributes from Python

1384
6
Jump to solution
03-19-2023 01:48 PM
Labels (2)
RandalGreene3
Occasional Contributor

Good day,

I'm exploring using Arc Hydro's Trace by AH Attributes tool in a Python script as an alternative to ArcGIS Pro's built-in Trace tool. Following is our test script:

import arcpy
import tracebyahattributes

arcpy.MakeFeatureLayer_management('C:/GIS/test/PBF2.gdb/CWFHydrology3', 'hydro_lyr')
flags = 'C:/GIS/test/PBF2.gdb/Flag'
barriers = 'C:/GIS/test/PBF2.gdb/AllOthers'
tracebyahattributes.traceviaAttributesAH(in_points=flags, in_streams='hydro_lyr', trace_direction='Upstream', search_distance='50', max_next_lines=0, in_barriers=barriers)
print(arcpy.GetCount_management('hydro_lyr'))
 
Unlike running the tool interactively, I cannot seem to get the selected hydrology segments (it does not make a selection on the passed hydro_lyr)! Is this tool only intended to be run in an interactive ArcGIS Pro session, or is there is something else that can be done to get the selected hydrology segments in the script?
 
Thanks in advance,
Randal
0 Kudos
1 Solution

Accepted Solutions
DeanDjokic
Esri Contributor

Randal,

We have updated the code for the Trace by AH Attributes to make it more Arc Hydro-ish with respect to how it is called from python.  Please install AH version 3.0.58 (of course, uninstall the previous version first).

Your updated code should then look something like (you can type this on the python command line and replace with proper pointers to your data – if it works, then move to your code):

import arcpy

import hydroconfig

import tracebyahattributes

in_flags = "D:\\10Data\\Raindrop2022\\AH2022.gdb\\Trace_by_AH_Attributes_Input_Start_Points_Points_3"

in_barriers = None

in_streams = "DrainageLine"

trace_direction = 'Upstream'  #or 'Downstream', 'Both'

search_distance = 50

max_next_lines = 0    #0=all, 1, 2, 3, =first, first 2, first 3 line(s) when there are splits

exclude_start_elements = False  #..start elements would be included in the results (elements a flag hits)

p = tracebyahattributes.TraceByAHAttributes()

p.DebugLevel = 1

t_returns = p.executeAH(in_flags, in_streams, trace_direction, search_distance, max_next_lines, in_barriers, exclude_start_elements)

if(t_returns[0] == hydroconfig.C_OK):

    print(t_returns)

else:

    print(t_returns[0])

 

-------- to try on the python command line after successful trace run

print(t_returns)

('OK', '36,31,32,25,26,27,30,21,22,18,19,23,24,16,17,14,15,3,4,11,13,1,2,12,20,9,10,5,6,7,8', 'ts_results', 'ts_start_points', '36', None)

Several things to note:

  • t_returns – this is the “result” that the function returns.  It consists of several elements.
    • Fist element (index 0) is whether the function completed successfully.  If it is “OK” then it finished correctly and the following elements in the list are individual results.
    • Second element is a string containing a comma delimited list of ArcIDs that are the trace results.  This can be useful if you are running the script “headless” – e.g. outside of Pro (pure python).
    • Third element is feature set of returned lines ( -- I think you are looking for this --)
    • Fourth element is feature set of input flags (starting points)
    • Fifth element is the count of returned trace lines ( -- I think you are looking for this --)
    • Sixth element is feature set of barriers if any were used.
  • This should return a selected set if you are running your code from within Pro.
  • Using this function makes sense if you have simple “network” and want basic upstream/downstream trace analysis.  You should use trace network if the trace system is more complex (e.g. multiple layers, complex edges, …).  We use TN for stormwater analysis within Arc Hydro.

Let me know if this works for you.

Also, please review document "Arc Hydro - Calling Arc Hydro Tools in Python.pdf" (https://community.esri.com/t5/water-resources-documents/arc-hydro-calling-arc-hydro-tools-in-python-...).  It goes into more detail on accessing AH functions through python script.

View solution in original post

6 Replies
DeanDjokic
Esri Contributor

Randal - which version of Pro and Arc Hydro tools are you using?

0 Kudos
RandalGreene3
Occasional Contributor

Hi Dean,

I'm using ArcGIS Pro 3.1 and the latest ArcHydroToolsforArcGISPro3.msi download. I could potentially try with earlier versions of either on another machine if you think it might be helpful.

Thanks very much,

Randal

0 Kudos
DeanDjokic
Esri Contributor

Thanks.  No need to switch - we are looking at the code and wanted to make sure we are looking at the right version.

0 Kudos
DeanDjokic
Esri Contributor

Randal,

We have updated the code for the Trace by AH Attributes to make it more Arc Hydro-ish with respect to how it is called from python.  Please install AH version 3.0.58 (of course, uninstall the previous version first).

Your updated code should then look something like (you can type this on the python command line and replace with proper pointers to your data – if it works, then move to your code):

import arcpy

import hydroconfig

import tracebyahattributes

in_flags = "D:\\10Data\\Raindrop2022\\AH2022.gdb\\Trace_by_AH_Attributes_Input_Start_Points_Points_3"

in_barriers = None

in_streams = "DrainageLine"

trace_direction = 'Upstream'  #or 'Downstream', 'Both'

search_distance = 50

max_next_lines = 0    #0=all, 1, 2, 3, =first, first 2, first 3 line(s) when there are splits

exclude_start_elements = False  #..start elements would be included in the results (elements a flag hits)

p = tracebyahattributes.TraceByAHAttributes()

p.DebugLevel = 1

t_returns = p.executeAH(in_flags, in_streams, trace_direction, search_distance, max_next_lines, in_barriers, exclude_start_elements)

if(t_returns[0] == hydroconfig.C_OK):

    print(t_returns)

else:

    print(t_returns[0])

 

-------- to try on the python command line after successful trace run

print(t_returns)

('OK', '36,31,32,25,26,27,30,21,22,18,19,23,24,16,17,14,15,3,4,11,13,1,2,12,20,9,10,5,6,7,8', 'ts_results', 'ts_start_points', '36', None)

Several things to note:

  • t_returns – this is the “result” that the function returns.  It consists of several elements.
    • Fist element (index 0) is whether the function completed successfully.  If it is “OK” then it finished correctly and the following elements in the list are individual results.
    • Second element is a string containing a comma delimited list of ArcIDs that are the trace results.  This can be useful if you are running the script “headless” – e.g. outside of Pro (pure python).
    • Third element is feature set of returned lines ( -- I think you are looking for this --)
    • Fourth element is feature set of input flags (starting points)
    • Fifth element is the count of returned trace lines ( -- I think you are looking for this --)
    • Sixth element is feature set of barriers if any were used.
  • This should return a selected set if you are running your code from within Pro.
  • Using this function makes sense if you have simple “network” and want basic upstream/downstream trace analysis.  You should use trace network if the trace system is more complex (e.g. multiple layers, complex edges, …).  We use TN for stormwater analysis within Arc Hydro.

Let me know if this works for you.

Also, please review document "Arc Hydro - Calling Arc Hydro Tools in Python.pdf" (https://community.esri.com/t5/water-resources-documents/arc-hydro-calling-arc-hydro-tools-in-python-...).  It goes into more detail on accessing AH functions through python script.

RandalGreene3
Occasional Contributor

Brilliant, works like a charm! Thanks very much for the quick turnaround.

0 Kudos
DeanDjokic
Esri Contributor

Glad it worked.

0 Kudos