Select to view content in your preferred language

How do I stop the script from removing layers while it is running?

64
2
yesterday
Laura_m_Conner
Regular Contributor

I still need help with my script.

I have a Python script to check for duplicates in a key field. The script is removing layers  while it is running on successive runs. this is not designed or intended. How do I stop the script from removing layers while it is running? I have seen other posts on Arcypy removing layers when scripts run, but there is no definitive answer or way to stop it. I need to stop it from removing features while it is running.

My script is:

# F is the list of feature layers that the script is to iterate over
F=("Sanitary Sewer Manholes","Water Network Structures","Water Fittings","Water System Valves","sw STRUCTURES", "Water Service Connections","Water Hydrants","Sewer Gravity Mains","Sewer Pressurized Mains","Storm Drain Pipes", "Water Lateral Lines", "Water Mains")
# output path for MBID duplactes
m="C:\\Users\\lconner\\Documents\\ArcGIS\\Projects\\edit_map7\\edit_map7.gdb\\MBID_duplicate" + str(1)


for e in F:
    # test if the feature class is in the map. if so clear the slection and run FindIdentical.
    if arcpy.Exists(e)== True:
        try:
            s=str(e).replace(" ", "_")
            o= "C:\\Users\\lconner\\Documents\\ArcGIS\\Projects\\edit_map7\\edit_map7.gdb\\" +s+"_findidencal"+("_a")

            # clear slection, so the finde identical runs on all features 
            arcpy.management.SelectLayerByAttribute(
            in_layer_or_view=e,
            selection_type="CLEAR_SELECTION",
            where_clause="",
            invert_where_clause=None
            )

            arcpy.management.FindIdentical(
            in_dataset= e,
            out_dataset=o,
            fields="FACILITYID",
            xy_tolerance=None,
            z_tolerance=0,
            output_record_option="ONLY_DUPLICATES")

            count_result = arcpy.management.GetCount(o)
            count = int(count_result[0])
            
            #print out to check if the findidentical ran
            print('successfuly ran find indentical for ' + str(e))  

        # handle an exeption if the code did not run
        except Exception as err:
            print(f"FindIdentical failed for {e}: {err}")
            continue
   
    #flag if the the frature is not in the map at this stage e.g. to begin with
    elif (arcpy.Exists(e)== False):
        print(str(e)+ " is not in the document 1") 

    
    try:
        # if there are duplactes join the duplicate out put table to the feature class and slect the duplactes 
        if (count > 0) and (arcpy.Exists(e)== True):
            # print statments to flag duplicates 
            print("Duplicate entries in "+str (e) )
            print(f"Number of duplicates found: {count}")

            #remove all joins to rule out previsous existing joins ant causing problems 
            arcpy.management.RemoveJoin(
                in_layer_or_view=e,
                join_name=""
                )

            arcpy.management.AddJoin(
                in_layer_or_view=e,
                in_field="OBJECTID",
                join_table=o,
                join_field="IN_FID",
                join_type="KEEP_ALL",
                index_join_fields="NO_INDEX_JOIN_FIELDS",
                rebuild_index="NO_REBUILD_INDEX",
                join_operation="JOIN_ONE_TO_FIRST"
                )

            arcpy.management.SelectLayerByAttribute(
                in_layer_or_view= e,
                selection_type="NEW_SELECTION",
                where_clause="IN_FID IS NOT NULL",
                invert_where_clause=None
                )

        # print statment for when there are no duplacts and the  FC is in the map
        elif (count == 0) and (arcpy.Exists(e)== True):
            print("no Duplicate in " + str(e) )

        # pirnt statment if the FC is not in the doucment at the time of preforming the join
        elif arcpy.Exists(e) == False:
            print(str(e) + " is not in the document 2" )
        
    #exeption if the FC is in the map byt the join cant be preformed
    except Exception as err:
            print(f"AddJoin failed for {e}: {err}")
            continue

    # render a blank line for neat print formating
    print("    ")
   
    
#portion of the script dealing with checking duplicat MBIDs
# if Water Service Connections is in the map findidentical
if arcpy.Exists("Water Service Connections")== True:
    try:
        arcpy.management.SelectLayerByAttribute(
            in_layer_or_view="Water Service Connections",
            selection_type="NEW_SELECTION",
            where_clause="MBID IS NOT NULL",
            invert_where_clause=None
            )

        arcpy.management.FindIdentical(
            in_dataset="Water Service Connections",
            out_dataset=m,
            fields="MBID",
            xy_tolerance=None,
            z_tolerance=0,
            output_record_option="ONLY_DUPLICATES"
            )
   
    #execption for handling erors where the FC is in the map
    except Exception as err:
            print(f"FindIdentical failed for Water Service Connections: {err}")
            
    # join and select if there are duplicates 
    if count > 0:
        print("Duplicate MBID in Meters " )
        print(f"Number of duplicates found: {count}")
        try:
            arcpy.management.AddJoin(
                in_layer_or_view="Water Service Connections",
                in_field="OBJECTID",
                join_table=m,
                join_field="IN_FID",
                join_type="KEEP_ALL",
                index_join_fields="NO_INDEX_JOIN_FIELDS",
                rebuild_index="NO_REBUILD_INDEX",
                join_operation="JOIN_ONE_TO_FIRST"
                )


            arcpy.management.SelectLayerByAttribute(
                in_layer_or_view= "Water Service Connections",
                selection_type="NEW_SELECTION",
                where_clause="IN_FID IS NOT NULL",
                invert_where_clause=None
                )
        #expection if the join could not be preformed
        except:
            print("the join for the duplacate MBIDs could not be preformed on Water Service Connections")        
    # print stament if the are no duplicates 
    else:
            print("no Duplicate MBIDs ")

#indicate the scipt is finshed 
print("done")

 

In particular, it works fine on the 1st run. However running it a2nd time it removes the feature classes it found duplicates in. The layers are being removed after the find identical feature. 

Here are the out puts for each run

1st run:

successfuly ran find indentical for Sanitary Sewer Manholes
no Duplicate in Sanitary Sewer Manholes

successfuly ran find indentical for Water Network Structures
no Duplicate in Water Network Structures

successfuly ran find indentical for Water Fittings
no Duplicate in Water Fittings

successfuly ran find indentical for Water System Valves
no Duplicate in Water System Valves

sw STRUCTURES is not in the document 1
sw STRUCTURES is not in the document 2

successfuly ran find indentical for Water Service Connections
no Duplicate in Water Service Connections

successfuly ran find indentical for Water Hydrants
no Duplicate in Water Hydrants

successfuly ran find indentical for Sewer Gravity Mains
no Duplicate in Sewer Gravity Mains

successfuly ran find indentical for Sewer Pressurized Mains
no Duplicate in Sewer Pressurized Mains

successfuly ran find indentical for Storm Drain Pipes
Duplicate entries in Storm Drain Pipes
Number of duplicates found: 9

Water Lateral Lines is not in the document 1
Water Lateral Lines is not in the document 2

successfuly ran find indentical for Water Mains
Duplicate entries in Water Mains
Number of duplicates found: 2

Duplicate MBID in Meters
Number of duplicates found: 2
done

 

 

on the 2nd run:

successfuly ran find indentical for Sanitary Sewer Manholes
no Duplicate in Sanitary Sewer Manholes

successfuly ran find indentical for Water Network Structures
no Duplicate in Water Network Structures

successfuly ran find indentical for Water Fittings
no Duplicate in Water Fittings

successfuly ran find indentical for Water System Valves
no Duplicate in Water System Valves

sw STRUCTURES is not in the document 1
sw STRUCTURES is not in the document 2

successfuly ran find indentical for Water Service Connections
no Duplicate in Water Service Connections

successfuly ran find indentical for Water Hydrants
no Duplicate in Water Hydrants

successfuly ran find indentical for Sewer Gravity Mains
no Duplicate in Sewer Gravity Mains

successfuly ran find indentical for Sewer Pressurized Mains
no Duplicate in Sewer Pressurized Mains

successfuly ran find indentical for Storm Drain Pipes
Storm Drain Pipes is not in the document 2

Water Lateral Lines is not in the document 1
Water Lateral Lines is not in the document 2

successfuly ran find indentical for Water Mains
Water Mains is not in the document 2

Duplicate MBID in Meters
Number of duplicates found: 2
done

 

Thanks 

Laura 

0 Kudos
2 Replies
2Quiker
Frequent Contributor

I have this issue as well, never been able to find answer, I have just used addDataFromPath() to re-add the layer.

0 Kudos
DuncanHornby
MVP Notable Contributor

I think this is default behaviour, you can alter the behaviour of tools through the options dialog, try unticking this option to see if this creates the behaviour you are after?

DuncanHornby_0-1752334830155.png

 

0 Kudos