Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I am trying to create a Cursor that goes through a selection process and stops looping when only one feature is selected. Basically, as the Cursor loops it subsets a selection, and instead of going through every row of the table I'd like to save time and stop the Cursor when one item is selected. So it would look something like this:

for row in rows:
    ... (selecting process) ...
    if [number of features selected] = 1:
        [stop cursor]
    else:
        rows.updateRow(row)
del row, rows

Any advice? I can't seem to even find any way to stop a cursor other than deleting it but I don't think I could do that type of command from within the cursor itself.

share|improve this question
    
Cursors opened on layers with selections already only process the selected rows. Is this not what you are seeing? Post the complete code if so. –  blah238 Feb 12 '13 at 19:25
    
Why is the ... (selecting process) ... inside of the loop? –  blah238 Feb 12 '13 at 19:25
    
See also: Accessing data using cursors –  blah238 Feb 12 '13 at 19:27
add comment

1 Answer

up vote 3 down vote accepted

I usually do this using arcpy.GetCount_management()

If you set it up like this:

if int(arcpy.GetCount_management(lyr_Name).getOutput(0)) == 1:
    break
else:
    [whatever else you were going to do]

It should kick your code out of the loop when the number of selected features in lyr_Name is one.

Here's the help page from ESRI for the function.

share|improve this answer
    
Thanks so much for this! –  AlmaThom Feb 18 '13 at 19:58
    
Glad it was helpful! –  Kevin Feb 18 '13 at 20:04
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.