0

I want to make a code that select all unique values of a field in a attribute table. However, while everything in the code apparently works, I don't know how to add the current row in the Search Cursor in my selection.

 layer = parameters[0].valueAsText
 mylist = []
 rows = arcpy.SearchCursor(layer, fields="Surveyor")
 for row in rows:
 value = row.getValue("Surveyor")
 if value not in mylist:
 mylist.append(value)
 arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION')
 arcpy.AddMessage(mylist)
 return

On the message (second last line), the program prints a list with 3 unique values. Yet, the code always select every single row. Makes sense, since there was no filter on my SelectLayer, so it just select the whole layer. So I tried using a field with Unique Values:

 layer = parameters[0].valueAsText
 mylist = []
 rows = arcpy.SearchCursor(layer, fields="Surveyor, uniqueID")
 for row in rows:
 value = row.getValue("Surveyor")
 myid = row.getValue("uniqueID")
 if value not in mylist:
 mylist.append(value)
 arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION', '"uniqueID" = myid')
 arcpy.AddMessage(mylist)
 return

But no success, it still selects the whole layer. So, how I make the SelectLayerbyAttribute to add to the selection only the row that the cursor is located?

Hornbydd
44.9k5 gold badges43 silver badges84 bronze badges
asked Nov 21, 2019 at 16:27
1
  • 1
    First rule of arcpy.SearchCursor: Use arcpy.da.SearchCursor instead. Commented Feb 24, 2021 at 17:20

2 Answers 2

2

"Old-style" search cursors are an artifact of the past. All supported versions of ArcGIS Desktop and Server offer Data Access cursors, which are faster and more Pythonic (list-oriented).

This is one way to rewrite your code, though there are options for faster runtime execution if you know that the number of IDs to be added is a relatively small set (e.g., less than 100-200).

The original problem was not placing the 'myid' value in the query expression.

layer = parameters[0].valueAsText
mylist = []
with arcpy.da.SearchCursor(layer, ['surveyor', 'uniqueID']) as cursor:
 for row in cursor:
 value = row[0]
 myid = row[1]
 if value not in mylist:
 mylist.append(value)
 filterexp = '"uniqueID" = {:d}'.format(myid)
 arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION', filterexp)
arcpy.AddMessage(mylist)
return
answered Feb 24, 2021 at 17:33
0

Found a solution:

layer = parameters[0].valueAsText
mylist = []
rows = arcpy.SearchCursor(layer, fields="surveyor, uniqueID")
for row in rows:
 value = row.getValue("surveyor")
 myid = row.getValue("uniqueID")
 filterexp = '"uniqueID" = ' + str(myid)
 if value not in mylist:
 mylist.append(value)
 arcpy.SelectLayerByAttribute_management(layer, 'ADD_TO_SELECTION', filterexp)
arcpy.AddMessage(mylist)
return
answered Nov 21, 2019 at 18:29

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.