I have a distribution of points on a geometric network. I have code to select a point, run an upstream trace on the network, select intersecting polygon features, sum some attributes from those polygons, and write the summation to the selected point.
The problem with the trace geometric network tool is that it returns a group layer. My stand-alone script approach (not shown) would not return this group layer to Python. I can run the script in the Python window of an ArcGIS 10.5 project though.
My script generates the group layer and adds it to the table of contents. I exploit the trace to get a selection and calculate my summation. Then I remove the trace group layer output and continue to the next point. After a few thousand iterations ArcGIS offers up a message box with the text:
TooboxEXT Out of Memory
If I do not rename the group layer output from the trace geometric tool each iteration arcpy returns an error that the group layer already exists even though I can watch it get removed from the table of contents. This suggests that the arcpy mapping module is not actually removing the group layer or that something is getting stuck in memory.
My question: How do I remove or delete the group layer so that I do not end up with an out-of-memory error?
Here is the code.
import sys
import traceback
import numpy
import arcpy
inNodes = r'Z:\GISpublic\GerryG\WaterResources\WRIA1Growth\WRIA1_Growth_Outputs.gdb\flags'
inSSHIAPsegCat = r'Z:\GISpublic\GerryG\WaterResources\WRIA1Growth\OtherSupportingData\joinedandsummed.shp'
inGeometricNetwork = r'Z:\GISpublic\GerryG\WaterResources\WRIA1Growth\WRIA1_Growth_Outputs.gdb\gfd\gfd_Net'
mxd = arcpy.mapping.MapDocument("CURRENT")
fieldList = arcpy.ListFields(inNodes)
fieldList2 = []
for field in fieldList:
fieldList2.append(field.name)
if "sum_1_4" not in fieldList2:
arcpy.AddField_management(inNodes, "sum_1_4", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
if "sum_1_6" not in fieldList2:
arcpy.AddField_management(inNodes, "sum_1_6", "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
inNodessLayer = arcpy.MakeFeatureLayer_management(inNodes)
inSSHIAPsegCatLayer = arcpy.MakeFeatureLayer_management(inSSHIAPsegCat)
watershedsFields = ['sum1_4', 'sum1_6']
nodesFields = ["SHAPE@", "sum_1_4","sum_1_6"]
counter = 0
with arcpy.da.UpdateCursor(inNodessLayer, nodesFields) as cursor:
for row in cursor:
if row[1] == -999 or row[1] >= 0 :
pass
else:
tracelayers = "tracelayers"+str(counter)
traceselect = tracelayers +"/Flowlines"
arcpy.TraceGeometricNetwork_management(inGeometricNetwork, tracelayers, row[0], "FIND_UPSTREAM_ACCUMULATION", "", "", "", "", "", "NO_TRACE_ENDS", "NO_TRACE_INDETERMINATE_FLOW", "", "", "AS_IS", "", "", "", "AS_IS")
arcpy.SelectLayerByLocation_management (inSSHIAPsegCatLayer, "INTERSECT", traceselect)
field = arcpy.da.TableToNumPyArray (inSSHIAPsegCatLayer, 'sum1_4', skip_nulls=True)
sum1_4 = field['sum1_4'].sum()
field = arcpy.da.TableToNumPyArray (inSSHIAPsegCatLayer, 'sum1_6', skip_nulls=True)
sum1_6 = field['sum1_6'].sum()
row[1]= sum1_4
row[2]= sum1_6
cursor.updateRow(row)
arcpy.SelectLayerByAttribute_management(inSSHIAPsegCatLayer, "CLEAR_SELECTION")
print "remove trace now..."
dfList = arcpy.mapping.ListDataFrames(mxd, "*")
for df in dfList:
for lyr in arcpy.mapping.ListLayers(mxd, "", df):
if lyr.isGroupLayer:
if lyr.name == tracelayers:
arcpy.mapping.RemoveLayer(df, lyr)
counter +=1
for df
loop, delete each grouplayer? Make sure you're not deleting a grouplayer that's a child of an already-deleted grouplayer. Also, did you try to isolate the problem writing a simple script that adds and removes grouplayers (without all the network tracing). If that runs without errors, it might suggest the issue is elsewhere (e.g. Tracing or SelectByLocation).arcpy.env.OverwriteOutput = True
, maybe it will overwrite old data instead of creating new data for each row in the cursor