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'
asked Feb 2, 2022 at 9:51
-
yes duplicate by attributes. Because cables are cut in two part by a point.amaranaitsaidi– amaranaitsaidi2022年02月02日 10:16:24 +00:00Commented Feb 2, 2022 at 10:16
1 Answer 1
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.
answered Feb 2, 2022 at 10:36
-
Just a question please, Are the objects in oids duplicates? if yes, can i makeFeature of the duplicates ?amaranaitsaidi– amaranaitsaidi2022年02月02日 13:22:53 +00:00Commented Feb 2, 2022 at 13:22
-
-
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.amaranaitsaidi– amaranaitsaidi2022年02月02日 14:47:23 +00:00Commented Feb 2, 2022 at 14:47
lang-py