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"
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 ?
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.
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
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!)
and now I am getting this error
Post your current script.
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"
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.