Uncategorized

arcpy – How to programmatically access Trace network layer results when using NETWORK_LAYERS result type in a stand-alone Python script?


I have a stand-alone Python script that runs a Connected Trace on a Trace Network. This is a re-write of an existing script that runs a trace on a Geometric Network. The script will be published as a geoprocessing service and will not be run interactively within ArcGIS Pro.

The trace network was created from scratch in ArcGIS Pro 2.9.6 and the existing geometric network layers were appended into it. I have confirmed that there are no topology or network errors.

I am trying to use the result type of NETWORK_LAYERS for the trace. My understanding is that this result type should be similar to the output produced using the TraceGeometricNetwork function in ArcMap 10.8.2. The documentation suggests that a group layer is being returned, however, the sentence “When this option is selected in ArcGIS Pro…” is a bit of a red flag, since most geoprocessing tools (in my experience) work within ArcGIS Pro/Desktop and when called from a stand-alone script. I suspect that this result type may only be available when the script is run from within ArcGIS Pro.

enter image description here
Link to documentation extract

enter image description here
Link to documentation extract

The relevant line from my script is:

arcpy.tn.Trace(network, 'CONNECTED', start_points, lyr_valve, 
    include_barriers="INCLUDE_BARRIERS", validate_consistency='VALIDATE_CONSISTENCY',  
    result_types="NETWORK_LAYERS", out_network_layer="TRACE1")

When the script is run from a toolbox within ArcGIS Pro, a group layer named TRACE1 is added to the active map. This is expected behaviour and I can programmatically review selected features within the group layer to extract what I need for further processing.

However, when the script is run as a stand-alone from a Windows Command prompt, the results from the trace function do not appear to be accessible as an object that contains layers. I added in print statements to try and see what is going on:

trace_result = arcpy.tn.Trace(network, 'CONNECTED', start_points, lyr_valve, 
    include_barriers="INCLUDE_BARRIERS", validate_consistency='VALIDATE_CONSISTENCY', 
    result_types="NETWORK_LAYERS", out_network_layer="TRACE1")

for i in range(0,10):
    try:
        print(i)
        print(type(trace_result.getOutput(i)))
        print(trace_result.getOutput(i))
    except:
        pass

> 0
> <class 'str'>
> Trace_Results_Aggregated_Points
> 1
> <class 'str'>
> Trace_Results_Aggregated_Lines
> 2
> <class 'str'>
> C:\Users\xxxx\Desktop\Testing\c4i_trace_network\c4i_trace_network.gdb\\Gas\Gas_Net
> 3
> <class 'str'>
> TRACE1
> 4
> <class 'str'>

5
6
7
8
9

For comparison, this is how my Geometric Network version of the script accessed the group layer:

arcpy.TraceGeometricNetwork_management(hvGeoNet, r"in_memory\traceConnected", 
    start3_3kV, "FIND_CONNECTED", hvFacility, in_trace_ends="NO_TRACE_ENDS")

for lyr in arcpy.mapping.Layer(r"in_memory\traceConnected"):
    for x in arcpy.mapping.ListLayers(lyr):
        if x.name == "HV_Cable" and FIDcount(x) > 0:
            log("  getting cable tag numbers")
            tmpCables = arcpy.CopyFeatures_management(x, GetScratchFC(scratchGDB))

This methodology doesn’t work for a script running under the ArcGIS Pro arcpy install and there doesn’t appear to be a way to access the group layer created by the trace.

As an extension to the answer proposed in this question, does anyone have an example of working with the NETWORK_LAYERS output from the Trace tool in a stand-alone Python script?



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *