2

I am having difficulty getting to grips with arcpy and cursors. What i aim to do is the following: Depending on the direction specified in Test I wish to update the Target field. If it is forward, the Target should receive the Value of AP from the next row/feature. In this example for the first feature, the Target would be 2. Aditionally, if the direction specified is backward the Target field should receive the value from the previous row. To make matters worse, the AP and Target fields are strings (and converting them is not an option as the data provided in ususally alphanumeric), so simply adding and subtracting 1 is not possible.

enter image description here

what i have so far:

i = 0  
lastvalue = []  
with arcpy.da.SearchCursor(InFC, [Direction, AP]) as cursor:  
    for row in cursor:
        if Direction == "forward":
            newField = AP[row+1] ## here id like to reference the next value somehow
        else:
            newField = AP[row-1] ## here id like to reference the previous value
  • Welcome to GIS SE! As a new user be sure to take the Tour. For questions that involve code we ask that you show us where you are stuck with your own code by including a code snippet in your question. There is an edit button beneath your question which will enable you to do that and a {} button that enables you to format any highlighted code nicely. – PolyGeo May 18 '18 at 8:19
  • I would add code if i had any to begin with. It is mostly failed attempts that i have promptly deleted. – JamesPatrick May 18 '18 at 8:32
  • @BERA's right - all that we try to avoid is becoming a "code writing service": gis.meta.stackexchange.com/a/3353/115 – PolyGeo May 18 '18 at 8:39
  • fair enough, I am new to python. coming from R where indexing is a breeze i find this pretty challenging – JamesPatrick May 18 '18 at 8:52
  • What happens when you run the exact code snippet that have presented? If it gives an error then telling us the exact error message including line number is what we are after. – PolyGeo May 18 '18 at 9:06
5

A cursor can only look at one row at a time but you can use two: Store all AP values in a list using a SearchCursor. Then use indexes to fetch values from the list and update using UpdateCursor. counter is keeping track of current row and used as index to fetch values from AP list. I dont know what you want to do at first/last row when you cant go backwards/forwards, im using current row value:

fc = 'contour'
fields = ['AP','Test','Target']

AP_list = [i[0] for i in arcpy.da.SearchCursor(fc, fields[0])]
listlength = len(AP_list)

counter = 0
with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if row[1] == 'forward':
            if counter == listlength-1:
                print 'Last row, cant go forward! Using current row value'
                value = 0                
            else:    
                value = 1
        else:
            if counter == 0:
                print 'First row, cant go back! Using current row value'
                value = 0
            else:
                value = -1
        row[2] = AP_list[counter+value]
        cursor.updateRow(row)
        counter+=1

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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