Select to view content in your preferred language

Trace Network Enabling Problem

2650
5
Jump to solution
10-30-2020 08:11 AM
JohnGaiot
Regular Contributor

Hello, 

I am trying to port over an 10.3.1 script which used the old geometric network functionality in Python scripting. I am running ArcGIS Pro 2.6.2.

In my script, I am creating multiple trace networks to set up a cleaned watercourse network for further hydrology and DEM processing (burning and flow directions etc). It creates the first network (wc_clean_net) to find connected segments (isolated segments are dropped) and enables the network no problem. It also successfully creates a second network (wc_net) in the same dataset (called "Layers") using an upstream trace this time from a particular pour point at the edge of the study area. But when I try to enable the network, I get the following error:

160326: The table already exists.

Traceback (most recent call last):
  File "C:\_Workspace\Toolboxes\MNRF_TerrainToolsLocal\Pro\TerrainScripts\Step A.2. Prepare Water_new.py", line 756, in <module>
    arcpy.EnableNetworkTopology_tn(wc_net, "", "ENABLE_TOPO")
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\tn.py", line 174, in EnableNetworkTopology
    raise e
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\tn.py", line 171, in EnableNetworkTopology
    retval = convertArcObjectToPythonObject(gp.EnableNetworkTopology_tn(*gp_fixargs((in_trace_network, max_number_of_errors, only_generate_errors), True)))
  File "c:\program files\arcgis\pro\Resources\arcpy\arcpy\geoprocessing\_base.py", line 511, in <lambda>
    return lambda *args: val(*gp_fixargs(args, True))
arcgisscripting.ExecuteError:  ERROR 160326: The table already exists.
Failed to execute (EnableNetworkTopology).
 Failed to execute (CreateGeometricNetwork).

I can enable it manually by running the Toolbox command when I load the network into a map but I can't do it through python. 

Below is a sample of the script.

First I try to clean up previous runs of the script deleting the networks before creating them again to help try isolating the problem. I am also checking for "TN_" tables (related to the trace network) that might not be cleaned up properly from previous bailed runs. As a last ditch effort, I tried copying all input feature classes and datasets to a new geodatabase to try running on a 'clean' geodatabase. No luck. Line 58 is where it bails.

# Network clean-up from previous runs
arcpy.AddMessage("Checking for Networks from previous run...")
if arcpy.Exists(wc_clean_net):
    arcpy.AddMessage(" >> Deleting 'wc_clean_net'...")
    arcpy.Delete_management(wc_clean_net)
if arcpy.Exists(wc_net):
    arcpy.AddMessage(" >> Deleting 'wc_net'...")
    arcpy.Delete_management(wc_net)
    arcpy.Delete_management(WC_Clean)
if arcpy.Exists(wc_select_net):
    arcpy.AddMessage(" >> Deleting 'wc_select_net'...")
    arcpy.Delete_management(wc_select_net)
    arcpy.Delete_management(WC_Select)
#arcpy.env.workspace = Output_Vector_GDB_Workspace
arcpy.AddMessage("Workspace = " + arcpy.env.workspace)
CurrentLayer = "TN_*"
arcpy.AddMessage("Checking for '" + CurrentLayer + "' orphan Network Tables...")
GDB_Tables = arcpy.ListTables(CurrentLayer, "All")
tbl_ctr = 0
for tbl in GDB_Tables:
    tbl_ctr += 1
    arcpy.Delete_management(tbl)
arcpy.AddMessage(" >> " + str(tbl_ctr) + " '" + CurrentLayer + "' tables deleted.")

arcpy.AddMessage("Performing Connected Trace to create WC Clean layer...")

WC_Name = os.path.basename(WC_Dissolve2)
arcpy.AddMessage(" >> Dissolved WC Name = '" + WC_Name + "'")
arcpy.AddMessage(" >> Creating Trace Network...")
arcpy.CreateTraceNetwork_tn(Layers, "wc_clean_net", "", [[WC_Name, "SIMPLE_EDGE"]])
arcpy.AddMessage(" >> Enabling Network...")
arcpy.EnableNetworkTopology_tn(wc_clean_net, "", "ENABLE_TOPO")

arcpy.AddMessage(" >> Snapping buffer and pour points to network...")
arcpy.Snap_edit(wc_bf_and_pour_pts, [[WC_Dissolve2, "EDGE", "0.1 Meters"]])

arcpy.AddMessage(" >> Tracing network...")
# Process: Trace Geometric Network
##arcpy.TraceGeometricNetwork_management(wc_clean_net, WC_Selection3, wc_bf_and_pour_pts, "FIND_CONNECTED", "", "", "", "", "", "NO_TRACE_ENDS", "", "", "", "AS_IS", "", "", "", "AS_IS")

arcpy.Trace_tn(wc_clean_net, "CONNECTED", wc_bf_and_pour_pts, "", "", "", "", "", "", "", "", "", "EDGES_ONLY", "", "", "AGGREGATED_GEOMETRY", "NEW_SELECTION", "", "Connected_Network_1", "", wc_traceout)
arcpy.MultipartToSinglepart_management(wc_traceout, WC_Clean)
wc_clean_lyr = "wc_clean_lyr"
arcpy.MakeFeatureLayer_management(WC_Clean, wc_clean_lyr)
arcpy.AddMessage(" >> Retrieving connected network results...")
arcpy.AddMessage(" >> Layer name is: {0}. Count is: {1}.".format(os.path.basename(WC_Clean), str(arcpy.GetCount_management(wc_clean_lyr))))

arcpy.AddMessage("Performing Upstream Trace to create initial WC Select layer...")

try:
    WC_Name = os.path.basename(WC_Clean)
    arcpy.AddMessage(" >> Cleaned WC Name = '" + WC_Name + "'")
    arcpy.AddMessage(" >> Creating Trace Network...")
    arcpy.CreateTraceNetwork_tn(Layers, "wc_net", "", [[WC_Name, "SIMPLE_EDGE"]])
except:
    arcpy.AddMessage(" >> Network exists... bypassing creation")
arcpy.AddMessage(" >> Enabling Network...")
arcpy.EnableNetworkTopology_tn(wc_net, "", "ENABLE_TOPO")

arcpy.AddMessage(" >> Snapping pour points to network...")
arcpy.Snap_edit(Flags_or_Sources_for_Tracing, [[WC_Clean, "EDGE", "0.1 Meters"]])

arcpy.AddMessage(" >> Tracing network...")
arcpy.Trace_tn(wc_net, "UPSTREAM", Flags_or_Sources_for_Tracing, Barriers_for_Tracing, "", "", "", "", "", "", "", "", "EDGES_ONLY", "", "", "AGGREGATED_GEOMETRY", "NEW_SELECTION", "", "Upstream_Network_2", "", wc_traceout2)

if Barriers_for_Tracing != "":
    arcpy.MultipartToSinglepart_management(wc_traceout2, Output_Vector_GDB_Workspace + "\\" + wc_inside)
    wc_inside_lyr = "wc_inside_lyr"
    arcpy.MakeFeatureLayer_management(Output_Vector_GDB_Workspace + "\\" + wc_inside, wc_inside_lyr)
    arcpy.AddMessage(" >> Retrieving upstream 'inside' network results...")
    arcpy.AddMessage(" >> Layer name is: {0}. Count is: {1}.".format(wc_inside, str(arcpy.GetCount_management(wc_inside_lyr))))

else:
    arcpy.MultipartToSinglepart_management(wc_traceout2, WC_Select)
    wc_select_lyr = "wc_select_lyr"
    arcpy.MakeFeatureLayer_management(WC_Select, wc_select_lyr)
    arcpy.AddMessage(" >> Retrieving upstream 'select' network results...")
    arcpy.AddMessage(" >> Layer name is: {0}. Count is: {1}.".format(os.path.basename(WC_Select), str(arcpy.GetCount_management(wc_select_lyr))))

All I can think of is that the Enable function is trying to use the same system table name for the second network that was created for the first network.

Am I missing something or is this a bug?

Thanks,

John

0 Kudos
1 Solution

Accepted Solutions
JonDeRose
Esri Contributor

@JohnGaiot  It is possible to have multiple trace networks in the same feature dataset as long as the participating feature classes aren't the same; however, this appears to be a defect.  I apologize for the inconvenience.  We have been able to reproduce and have created an internal issue to be addressed. You can track this as BUG-000136866.

View solution in original post

5 Replies
JohnGaiot
Regular Contributor

UPDATE: I tried saving the results of the first trace to a dataset in a new file geodatabase, created a new network (wc_net) and still got the same 'table already exists' error when I try to enable the network. Nothing else exists in the geodatabase because I just created it. So I have no clue why it's doing this...

0 Kudos
by Anonymous User
Not applicable

UPDATE #2: What appears to be happening here is that when I create each network, they are automatically enabled. I don't have to include the "EnableNetworkTopology_tn" function after creating the Trace, which contradicts the help instructions for the toolbox.

0 Kudos
JohnGaiot
Regular Contributor

UPDATE #3: Even stranger finding... I tried to remove the enable topology functions and it logically came up with an error that topology was not enabled when trying to set flow direction. So basically, I found after creating each network, I had to first disable and then enable topology before setting flow direction and performing a trace successfully. Very weird behaviour, but I seem to be getting results now, which are not exactly the same as I was getting in ArcMap but at least I can troubleshoot my trace results now.

Below attached is a code snippet showing 1 of the 3 network/traces being performed and the weird logic that made it run successfully.

 

0 Kudos
JonDeRose
Esri Contributor

@JohnGaiot  It is possible to have multiple trace networks in the same feature dataset as long as the participating feature classes aren't the same; however, this appears to be a defect.  I apologize for the inconvenience.  We have been able to reproduce and have created an internal issue to be addressed. You can track this as BUG-000136866.

JohnGaiot
Regular Contributor

Thanks Jon for the update. Looking forward to the fix. As a workaround, I keep adding the disable/enable combination for each network created, which seems to be doing the trick.

I found a bigger issue regarding a performance hit in Pro of the DA Update Cursor when processing data in loops compared to ArcMap. I will have to report this in the appropriate forum when I have a chance as it was enough to halt my development and turn to other pressing matters in the interim.

0 Kudos