1

I am still pretty new to python (and programming for that matter) but I believe there must be an easier way to do a certain task. My task is to flip road centerline directions. The centerlines have address ranges in the attributes, and I need to flip them, from left to right. Basically I am swapping the value of some attributes.

Is that something I could accomplish with some scripting? I have a Python book on my desk that I've been reading through. I'm eager to tackle new problems with code.

  • Are you looking to edit the line direction - or just the attribute? If it is just the attribute - can you give me a sample of the attributes you want to adjust? – dklassen Dec 5 '13 at 21:04
  • @dklassen I was only thinking within the scope of editing the attributes. If python can do the Flip command, extra awesome! screenshot EDIT looks like no images in comments. – Elliott Dec 5 '13 at 21:12
2

To do attributes, you can use either arcpy.da.UpdateCursor (requires 10.1 or later) or arcpy.UpdateCursor

To use arcpy.da.UpdateCursor (see arcpy.da.UpdateCursor)

# using arcpy.da.UpdateCursor  (requires 10.1 or later...)
import arcpy

fc = 'C:/Temporary Files/Centerlines.shp'
fields = ['L_ADD_FROM', 'L_ADD_TO']

with arcpy.da.UpdateCursor(fc, fields, "\"L_ADD_TO\" < \"L_ADD_FROM\"") as cursor:
    for row in cursor:
        # Get the values - row[0] refers to the first field in your fields list
        value0 = row[0]
        value1 = row[1]

        # Switch the values
        row[0] = value1
        row[1] = value0

        cursor.updateRow(row)

To use arcpy.UpdateCursor (See arcpy.UpdateCursor)

# using arcpy.UpdateCursor
import arcpy

fc = 'C:/Temporary Files/Centerlines.shp'

cursor = arcpy.UpdateCursor(fc, "\"R_ADD_TO\" < \"R_ADD_FROM\"")
row = cursor.next()
while row:
    value0 = row.getValue('R_ADD_FROM')
    value1 = row.getValue('R_ADD_TO')

    row.setValue('R_ADD_FROM', value1)
    row.setValue('R_ADD_TO', value0)

    cursor.updateRow(row)
    row = cursor.next()
  • Awesome. So it seems that (at first glance) the .da.UpdateCursor iterates through the rows using a for loop, while the latter uses a while loop. The first one seems more simple and elegant. – Elliott Dec 5 '13 at 23:12
  • Without testing it for sure, I think you can use both for and while loops on either one.... also, you can have your cursor include all rows, and then put your logic of whether or not to update in the "for" part...if you like I can edit my answer to provide an example... – Jason Miller Dec 6 '13 at 1:02
  • You can use a for on both and I would always use a for over a while when you can. – Nathan W Dec 6 '13 at 1:55
  • 2
    You can also do the swap in one line row[0], row[1] = row[1], row[0] – Nathan W Dec 6 '13 at 1:55
  • I got this working, using a mix of @JasonMiller and NathanW's help. Here is my code – Elliott Dec 6 '13 at 18:12

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.