5

I am at the end of my rope here. This cursor runs fine for one iteration and then stops. It doesn't throw an error, it just completes. There are 3,000 rows in my table, so I don't know why it would do this. Does the Generate Near Table function somehow stop the cursor?

import arcpy        
rows = arcpy.SearchCursor("mergedVMS")
trip_id = 0
for row in rows:
    if row.trip_id == trip_id:
        continue
    else:
        trip_id = int(row.trip_id)
        arcpy.SelectLayerByAttribute_management("mergedTrawls", "NEW_SELECTION", '"trip_id" = ' + str(trip_id))    
        arcpy.SelectLayerByAttribute_management("mergedVMS", "NEW_SELECTION", '"trip_id" = ' + str(trip_id))
        arcpy.GenerateNearTable_analysis("mergedVMS", "mergedTrawls", "C:\\Desktop\\Feb25\\Project.gdb\\pointdist" + str(trip_id))
del row, rows

Per dmahr's answer, my new script that works is as follows:

import arcpy        
rows = arcpy.SearchCursor("mergedVMS2")
trip_id = 0
for row in rows:
    if row.trip_id == trip_id:
        continue
    else:
        trip_id = int(row.trip_id)
        arcpy.SelectLayerByAttribute_management("mergedTrawls", "NEW_SELECTION", '"trip_id" = ' + str(trip_id))    
        arcpy.SelectLayerByAttribute_management("mergedVMS", "NEW_SELECTION", '"trip_id" = ' + str(trip_id))
        arcpy.GenerateNearTable_analysis("mergedVMS", "mergedTrawls", "C:\\Desktop\\Feb25\\Project.gdb\\pointdist" + str(trip_id))
        arcpy.SelectLayerByAttribute_management("mergedTrawls", "CLEAR_SELECTION")
        arcpy.SelectLayerByAttribute_management("mergedVMS", "CLEAR_SELECTION")
del row, rows

I have simply duplicated my mergedVMS layer and run the cursor on that (trip_id is my unique value). I also added two clear selection lines. It is currently running exactly as expected.

2
  • 2
    Do you have a row of your table selected? When running a script in-process, layer selections are applied before the cursor begins reading. Also, your second SecondLayerByAttribute command may be influencing the behavior of your cursor. I would suggest creating a duplicate of mergedVMS and selecting it by some unique ID for the Near Table analysis rather than trying to use a row of the table your cursor is running on.
    – dmahr
    Commented Feb 26, 2013 at 14:19
  • Thanks so much. I have never had a select issue within a cursor before, but creating a duplicate to run the cursor on and also adding lines that clear selection at the end of the loop seems to be working. If you repeat this in an answer form then I can close the question and give you the credit for it.
    – AlmaThom
    Commented Feb 26, 2013 at 14:28

1 Answer 1

6

There are two reasons that I can think this may happen, and both involve selections:

  1. You may have a row of your mergedVMS table accidentally selected. When running Python tools from ArcMap "in-process", any selections you apply to features or tables are applied before your cursor begins running, even if your cursor has a separate where_clause parameter specified. Think of it as a second definition query.

  2. Row 10 of your code is interfering with your cursor. Unlike other programming languages, the cursor you create is not separate from the mergedVMS layer--in other words, it is not "pass by value." So when you perform a Select By Attributes on mergedVMS, this impacts the behavior of the cursor you are currently iterating in your for loop. The easiest way to handle this is to just create a separate copy of mergedVMS and select the row of interest using a unique ID before passing the table into the Near Analysis tool.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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