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"
-
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.Midavalo– Midavalo ♦2017年05月18日 18:06:47 +00:00Commented 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 duplicateMidavalo– Midavalo ♦2017年05月18日 18:09:30 +00:00Commented 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!Suffii– Suffii2017年05月18日 19:09:33 +00:00Commented May 18, 2017 at 19:09
-
Are you running the script from ArcMap with background-geoprocessing disabled for the script?Midavalo– Midavalo ♦2017年05月20日 02:52:45 +00:00Commented May 20, 2017 at 2:52
2 Answers 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")
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