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 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? Thanks

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. Thanks so much for the quick help!

share|improve this question
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 Feb 26 '13 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 Feb 26 '13 at 14:28

1 Answer 1

up vote 6 down vote accepted

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.

share|improve this answer

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.