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 hope my question is not redundant.

I wrote a script to Update a table using two imbricate cursors. The goal was 1) to read a records, update some of its values 2) Make a selection based on this record (e.g. closest neighbors) 3) Update the neighbors

I have first tried using two imbricate update cursors like the following:

rows = gp.UpdateCursor(Infile)
row = rows.Next() 
while row:
    # Read InFile attribute
    val = row.GetValue(Attribute)
    #  Update some attributes
    val = row.SetValue(Field, function(val))
    # Select Neighbors
    gp. gp.GANeighborhoodSelection(Infile, Layer, Condition related to val)
    //.....//
    # Update Neighbors
    urows = gp.UpdateCursor(Infile)
    urow = urows.Next() 
    while urow:
        #  Update some attributes
        val = urow.SetValue(Field, function(val))
        urows.UpdateRow(urow)
        urow = urows.Next() 
    del urow, urows
    //.....//
    row.UpdateRow(row)
    row = rows.Next()
    //.....//

Using such a code, the second update cursor was calculating correctly (i printed some intermediate results) but the calculation was not applied to the table (ie. not physical updation).

I modified the code by replacing the second update cursor by a Select by attributes followed by a calculate field. Again, the calculations were not applied to the table. I think there is a lock problem somewhere but I did not found how to solve the problem.

Finally, I replaced my first UpdateCursor by a SearchCursor and then used an UpdateCursor based on a condition in order to reduce the number of record processed. The code look like this:

rows = gp.SearchCursor(Infile)
row = rows.Next() 
while row:
    # Read InFile attribute
    val = row.GetValue(Attribute)
    #  Update some attributes
    val = row.SetValue(Field, function(val))
    # Select Neighbors
    gp. gp.GANeighborhoodSelection(Infile, Layer, Condition related to val)
    //.....//
    # Update Neighbors
    urows = gp.UpdateCursor(Infile, Condition based on val)
    urow = urows.Next() 
    while urow:
        #  Update some attributes
        val = urow.SetValue(Field, function(val))
        urows.UpdateRow(urow)
        urow = urows.Next() 
    del urow, urows
    //.....//
    row.UpdateRow(row)
    row = rows.Next()
    //.....//

Using such a structure, all is working fine. However, calculation is longer (around 5 sec. for 1000 records).

Do you have any idea 1) why using 2 imbricate UpdateCursor is not working and 2) how to use select by attributes and calculate field within a cursor to fasten the calculation?

Thanks a lot

Cedric

share|improve this question
add comment

1 Answer 1

You haven't used the updateRow method.

while urow:
    #  Update some attributes
    val = urow.SetValue(Field, function(val))
    urows.updateRow(urow) 
    urow = urows.Next() 
share|improve this answer
    
Thanks for your reply. I modified my post. I did put the "row.UpdateRow(row)" within my script. Just forgot to put it on the post..... Sorry about this mistake! –  vega Oct 19 '12 at 6:15
    
You need to call the updateRow method on the updatecursor object "urows", not the row object "urow" and you can't use the updateRow method on your outer cursor as it's a searchcursor not an updatecursor. –  Luke Oct 19 '12 at 7:55
    
Hi Luke. My original code was correct. Piece of code were just for highlighting. Basically, what I found is that: 1) Using UpdateCursor within a SearchCursor is working fine, but is taking time. 2) using an Updatecursor (C2) within an Updatecursor (C1) on the same table, updates from C2 are not persistent (records from the last run are ok, previous ones are erased. I found that by checking the results after a different number of row.next()). 3) I got the same problem using a calculate field within an Updatecursor. I do not understand why updates are not persistent in 2) and 3). –  vega Oct 19 '12 at 9:44
    
To give more details, it seems that in 2) and 3) only the updates from C2 (or CalculateField) computed during the last iteration of the first cursor (C1) are taken into account.... Hope these comments help to the understanding of my original questions. –  vega Oct 19 '12 at 10:44
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.