I am trying to create a script that allows me to to set various starting points on a network. Then perform an upstream trace and export the selected lines to a new feature class. No matter what I try though I keep copying the entire feature class over and cannot get to the selected files from the trace.

This does have to be a stand alone application

This is what I have so far:

import arcpy
import os

def get_source_id(network, network_layer):
    d = arcpy.Describe(network_layer)
    fc_name = d.name
    d = arcpy.Describe(network)
    for s in d.sources:
        if s.name == fc_name:
            return s.sourceID

        'Could not determine network source ID for ' + network_layer.name)
    raise arcpy.ExecuteError

def point_to_flag(shape_xy, spatial_ref, source_id, global_id):
    flag_fc = arcpy.CreateFeatureclass_management(
    flag_fc = arcpy.management.AddField(flag_fc, 'SOURCEID', 'LONG')[0]
    flag_fc = arcpy.management.AddField(flag_fc, 'FEATUREGLOBALID', 'GUID')[0]
    with arcpy.da.InsertCursor(flag_fc, fields) as cursor:
        cursor.insertRow((shape_xy, source_id, global_id))
    return flag_fc

arcpy.env.overwriteOutput = True
arcpy.env.workspace = r"C:\Users\jconshick.BARTON\Desktop\TemporaryProject\ArcHydroTemplate.gdb\Layers"

network = "AHStormwater_New"
junction_layer = "PipeOutlet"
pipes = "pipe"
test = "Test6"

Trace_Results_Aggregated_Points = "Trace_Results_Aggregated_Points"
Trace_Results_Aggregated_Lines = "Trace_Results_Aggregated_Lines"
grpLyr = "Test"

spatial_ref = arcpy.Describe(junction_layer).spatialReference
source_id = get_source_id(network, junction_layer)

with arcpy.da.SearchCursor(junction_layer, ['SHAPE@XY', 'GLOBALID']) as cursor:
    for i, row in enumerate(cursor):        
        flag = point_to_flag(row[0], spatial_ref, source_id, row[1])

        print (grpLyr)
        arcpy.CopyFeatures_management(pipes, "Test3")
        print (arcpy.GetCount_management("Test3"))

1 Answer 1


The reason your output includes all the features is because you are using the CopyFeatures tool on the wrong "thing".

Golden rule for all geoprocessing tools is that they honour selections on LAYERS.

Your input to CopyFeatures is pipes, which when checked is simply "pipe", and as you are intending to run this script outside the GIS you have set the workspace. What you are supplying to CopyFeatures is not a LAYER but a FEATURE CLASS. Think of that as the raw data, a feature class has no concept of what a selection is or in fact what symbology is, that is the job of the LAYER, the visual representation of the FEATURE CLASS.

You can run your trace as many times as you like but if you keep pointing CopyFeatures to the Feature Class you will always get the entire contents of the Feature Class.

Now you know the difference you need to be supplying CopyFeatures tool with the LAYER which will have your selection created by the Trace.

Looking at the help file on Trace and read the information on parameter out_network_layer, which in your case is called grpLyr. You need to drill down into that and get the LAYER name and feed that into the CopyFeatures tool.

When using Trace in a scripting environment and to ensure your group layer is populated you need to set the result type to Network Layers. This is documented here, then further down the help it says this:

The Network Layers result types option allows you to return trace results as selection sets on feature layers in a group layer. When this option is selected in ArcGIS Pro, a new group layer is added to the map. Feature layers are only created for classes that contain features returned by the trace. Each feature layer contains a selection set of features returned by the trace that can be used to work with the results of a trace in ModelBuilder and Python.

When the Network Layers option is selected, an additional Output Group Layer Name parameter is provided to name the output group layer that is created. When working in ArcGIS Pro a new group layer is added to the active map with the specified name.

Seems like you can then drill down the group layer and get the layer object with the selection.

  • Does anyone have a working example of accessing the network layers produced by the trace in a stand-alone script? I can access the layers via code if the script is executed from within ArcGIS Pro, but the layer object does not appear to be returned if the script is executed outside of ArcGIS Pro?
    – Dan
    Dec 20, 2023 at 3:28
  • @Dan you need to ask this as a new question showing your workings, not tagged on as a comment to my answer.
    – Hornbydd
    Dec 20, 2023 at 11:51
  • 1
    Done. gis.stackexchange.com/questions/473270/…
    – Dan
    Dec 21, 2023 at 1:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.