1

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"
asked May 18, 2017 at 16:34
4
  • My recollection is that if you run the tool it outputs a group layer into your map. You may need to make your script run in foreground for it to work. Commented May 18, 2017 at 18:06
  • I'm fairly sure this has been asked before, in the last few months. I'll see if I can find a duplicate Commented May 18, 2017 at 18:09
  • Thanks Midavalo but that code just add the futures into the gdb . It is not adding them into the map! Commented May 18, 2017 at 19:09
  • Are you running the script from ArcMap with background-geoprocessing disabled for the script? Commented May 20, 2017 at 2:52

2 Answers 2

2

If running the script in foreground doesn't automatically add the output layer to your MXD, try using arcpy.mapping.AddLayer() to insert the layer into ArcMap:

import arcpy
arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb"
mxd = arcpy.mapping.MapDocument(r"CURRENT")
df = mxd.activeDataFrame
# 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"
arcpy.mapping.AddLayer(df, traceOut, "AUTO_ARRANGE")
answered May 20, 2017 at 13:31
0

you can also define a Feature class and fill it after ending of trace and use MakeFeatureLayer to craete Layer

arcpy.env.workspace = r"E:/Utility/Sample/Montgomery.gdb"
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")
spatial_reference = arcpy.SpatialReference('Projected Coordinate Systems/World/WGS 1984 Web Mercator (auxiliary sphere)')
FC = arcpy.CreateFeatureclass_management("in_memory","Q234","POLYLINE","","DISABLED","DISABLED",spatial_reference)

add proper filed to FeatureClass

 FC = arcpy.CreateFeatureclass_management("in_memory","MyLines","POINT","","DISABLED","DISABLED",spatial_reference)
for FeatureClass in FC:
 arcpy.AddField_management(FeatureClass, "TableName", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
 arcpy.AddField_management(FeatureClass, "Obj_ID", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
 arcpy.AddField_management(FeatureClass, "GeoJson", "TEXT", "", "", "880", "", "NULLABLE", "NON_REQUIRED", "")
 arcpy.AddField_management(FeatureClass, "LayerId", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")
 arcpy.AddField_management(FeatureClass, "LayerName", "TEXT", "", "", "80", "", "NULLABLE", "NON_REQUIRED", "")

do a loop on result of trace then fill FeatureClass with values

for sublayer in traceOut :
 layername=sublayer.name
 ld=arcpy.Describe(sublayer)
 layeraliasname=ld.aliasname
 numbeeLayerId=numbeeLayerId+1;
 if arcpy.Describe(sublayer).shapeType=="Point":
 with arcpy.da.SearchCursor(sublayer,("OBJECTID","SHAPE@","SHAPE@JSON"),"",spatial_reference=spatial_reference)as cursora:
 with arcpy.da.InsertCursor(FC,("OID","Shape","TableName","Obj_ID","GeoJson","LayerId","LayerName")) as iCur:
 for row in cursora:
iCur.insertRow((row[0],row[1],layername,row[0],row[2],numbeeLayerId,layeraliasname))

now you can use MakeFeatureLayer function to generate your layer

answered Sep 16, 2017 at 8:49

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.