0

I'm writing a script to find duplicate cables in a layer. I want to create a layer from the result to find the closest cables to those already cut with ArcPy but I can't find a way to do it.

The license I use is Standard so I don't have the FindIdentical_management function.

here is my code :

couche_cable = arcpy.MakeFeatureLayer_management(chem_caftth)
champ_a_verfier = ["REFERENCE", "OBJECTID", "CREATIONUSER", "LASTUSER", "USERREFERENCE", "SUBTYPEID", "SHAPE"]  
    values={}
    values["Ref_Nulle"]=0
    with arcpy.da.SearchCursor(couche_cable, champ_a_verfier) as rows:
        for r in rows:
            if r[0] in [None,""]:
                values["Ref_Nulle"]+=1
            else:
                if r[0] not in values:
                    values[r[0]]=1
                else:
                    values[r[0]]+=1 
    del rows
    
    
    dictDoublons = {}
    dictPasDoublons = {}
    cable_cda = 'CDA'
    count = 0
    for item in values:
        if values[item] > 1 and values[item] < 3:
            if not item.startswith("CDA"):
                dictDoublons[item] = 'Doublons'
                count+=1
                arcpy.AddWarning('Il y a {} cables en doublons  a Verifier / Corriger.'.format(count))
        else:   
            dictPasDoublons[item] = 'PasDoublons'
1
  • yes duplicate by attributes. Because cables are cut in two part by a point. Commented Feb 2, 2022 at 10:16

1 Answer 1

2

You can use a set:

import arcpy

fc = r'C:\GIS\ArcMap_default_folder\Default.gdb\my_sample'
fieldlist = ['kkod','kategori'] #Fields to consider for duplicates

values = set() #Create a set, which cant have duplicate values
oids = [] #A list to store object ids
for row in arcpy.da.SearchCursor(fc, ['OID@']+fieldlist):
    if row[1:] in values: #If the row values are in the set = they are a duplicate
        oids.append(row[0]) #Add the oid to oids list
    else:
        values.add(row[1:]) #If not add the values to the set

#Select the oids in oids list
oidfield = arcpy.Describe(fc).OIDFieldName
sql = """{0} IN{1}""".format(arcpy.AddFieldDelimiters(datasource=fc, field=oidfield), tuple(oids))
arcpy.SelectLayerByAttribute_management(in_layer_or_view='my_sample', where_clause=sql) #Or Select, MakeFeatureLayer etc.

enter image description here

3
  • Just a question please, Are the objects in oids duplicates? if yes, can i makeFeature of the duplicates ? Commented Feb 2, 2022 at 13:22
  • the object ids in oids list are there because the attributes already were in the set. You want to create a new feature class of them? Yes, use Select instead of select layer by attributes Commented Feb 2, 2022 at 14:33
  • Yes, I wanted to create a layer with fieldlist duplicates in my case: fieldLlist = ["REFERENCE"]. your script is clear but I can't find the duplicates. Commented Feb 2, 2022 at 14:47

Your Answer

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.