Adding Geometric Network Trace to Map Using ArcPy

1757
9
05-18-2017 09:36 AM
BehrouzHosseini
Occasional Contributor

I am able to run Geometric Network Trace by using following ArcPy script but I also need to add Trace result into the map. Can you please let me know how I can add the result(graphic/geometry) to the map.

import arcpy

arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb"
# Local variables:
G_N = "E:/Utility/Sample/Montgomery.gdb/Water/Water_Net"
Flag = "E:/Utility/Sample/Flag.shp"
traceOut = "traceOut_Net"

# Process: Trace Geometric Network
arcpy.TraceGeometricNetwork_management(G_N, traceOut, Flag, "TRACE_UPSTREAM", "", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS")
print "Trace Done"
0 Kudos
9 Replies
BehrouzHosseini
Occasional Contributor

Thanks Curtis'

I updated the code as:

import arcpy

arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb"
# Local variables:
G_N = "E:/Utility/Sample/Montgomery.gdb/Water/Water_Net"
Flag = "E:/Utility/Sample/Flag.shp"
traceOut = "traceOut_Net"

# Process: Trace Geometric Network
arcpy.TraceGeometricNetwork_management(G_N, traceOut, Flag, "TRACE_UPSTREAM", "", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS")

glyr = arcpy.mapping.Layer(traceOut)
# Extract the layer based on a search string
tlyr = [lyr for lyr in arcpy.mapping.ListLayers(glyr)
        if "net" in str(lyr).lower()][0]
# copy out to a feature class
arcpy.CopyFeatures_management(tlyr, tfeat)
print "Trace Done"

but I am getting error  on 

arcpy.CopyFeatures_management(tlyr, tfeat)

can you please let me know what is tfeat ?

0 Kudos
curtvprice
MVP Esteemed Contributor

That's a variable I did not set in the code sample, sorry, you can set it to whatever you want, for example "trace_feat" would write a feature class to Montgomery.gdb for you.

0 Kudos
BehrouzHosseini
Occasional Contributor

Thanks for reply but what exactly do you men by setting? "I did not set in the code sample"

something like 

trace_feat = "E:/Utility/Sample/Montgomery.gdb/Water/trace_feat.shp
0 Kudos
curtvprice
MVP Esteemed Contributor
tfeat = "trace_feat"

somewhere above the CopyFeatures call will tell CopyFeatures to write to the current arcpy.env.workspace:

 E:/Utility/Sample/Montgomery.gdb/Water/trace_feat

Warning, dont' specify .shp for feature classes inside a geodatabase (dots aren't allowed!)

0 Kudos
BehrouzHosseini
Occasional Contributor

and now I am getting this error

0 Kudos
curtvprice
MVP Esteemed Contributor

Post your current script.

0 Kudos
BehrouzHosseini
Occasional Contributor

Here is the latest:

import arcpy

arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb/Water/Flag_Trace"
# Local variables:
G_N = "E:/Utility/Sample/Montgomery.gdb/Water/Water_Net"
Flag = "E:/Utility/Sample/Montgomery.gdb/Water/Flag"
traceOut = "traceOut_Net"

# Process: Trace Geometric Network
arcpy.TraceGeometricNetwork_management(G_N, traceOut, Flag, "TRACE_UPSTREAM", "", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS")

glyr = arcpy.mapping.Layer(traceOut)
# Extract the layer based on a search string
tlyr = [lyr for lyr in arcpy.mapping.ListLayers(glyr)
        if "net" in str(lyr).lower()][0]
# copy out to a feature class
tfeat = "Flag_Trace"
arcpy.CopyFeatures_management(tlyr, tfeat)
print "Trace Done"
0 Kudos
curtvprice
MVP Esteemed Contributor

Look at the contents of "traceOut_Net" in the catalog window and modify the search expression "if "net" in str(lyr).lower()" match what you need to find.

0 Kudos