2

I'm working on building a series of updates to street feature class based on fields inside the class. I was attempting to do this with update cursor to get better performance.

When the Streets_GC_FullName is null, the STREETS_NW_SLIPRD = 2 and STREETS_NW_RAMP=1, then change the Streetname field to "Exit Ramp"

The Cursor runs for a little while but then none of the rows are actually updated. I had tried adding the print statment to see if it was stopping on those rows that matched, but none printed.

So I'm assuming that my row test is failing?

fc = r'C:\GIS\Sept2018_MergeLA.gdb\NW_GC'
fields = ['Streets_GC_FULLNAME', 'Streets_NW_FEATTYP', 'Streets_NW_SLIPRD', 'Streets_NW_RAMP', 'StreetName','Streets_NW_ID']

with arcpy.da.UpdateCursor(fc, fields) as cursor:
    for row in cursor:
        if (row[0] is None and row[3] == 2 and row[4] == 1):
            row[5] == "Exit Ramp"
            # print("Updated"+row[6])
            cursor.updateRow(row)
print ("Processing complete")
7
  • 2
    It sounds like you have no rows where row[0] is None and row[3] == 2 and row[4] == 1. Try printing row[0], row[3] and row[4] just before you test to see if you can spot any rows that should meet the criteria. If you have many thousands to check then just add a where clause like OBJECTID < 10 onto your cursor to keep the output manageable.
    – PolyGeo
    Commented Nov 1, 2018 at 1:40
  • 1
    row[0] is None should be row[0] == None, though depending on how your fields are populated blank may not be None (Null), check also row[0] == 0 or row[0] == '' depending on your field type. Commented Nov 1, 2018 at 1:56
  • For this test I have 2460 fields out of close to 1 million, that show up when doing arcpy.management.SelectLayerByAttribute("NW_GC", "SUBSET_SELECTION", "Streets_GC_FULLNAME IS NULL And Streets_NW_SLIPRD = 2 And Streets_NW_RAMP = 1", None)
    – DanceDad
    Commented Nov 1, 2018 at 1:58
  • 2
    You could create a selection on your layer or supply the query in the update cursor.. if you're creating a selection you should be able to use int(arcpy.GetCount_management("NW_GC").getOutput(0)) to tell if you've got any features that satisfy the query. Commented Nov 1, 2018 at 2:09
  • 1
    What do you get when you print the updated row? If you're running from the toolbox you may need to use arcpy.AddMessage("Updated {}".format(row[6])) to get the message to show up in the tool dialog - also works in CMD and from the python console. Commented Nov 1, 2018 at 3:20

1 Answer 1

3

There are three problems with your code that I can see:

PROBLEM 1:

The line row[5] == "Exit Ramp" should use = (assign a value) not == (testing for equality). row[5] is not actually being assigned a value by the statement as it is. In fact nothing at all is being done by that statement.

PROBLEM 2:

Your row indexes appear to be a bit muddled. You have 6 fields in your cursor. They are zero-indexed so the last field is row[5], not row[6]. So I would expect that the (commented out) print statement would crash if uncommented (and processing actually reached that point).

Your fields are:

fields = ['Streets_GC_FULLNAME', 'Streets_NW_FEATTYP', 'Streets_NW_SLIPRD', 'Streets_NW_RAMP', 'StreetName','Streets_NW_ID']

So...

  • row[0] is 'Streets_GC_FULLNAME'
  • row[1] is 'Streets_NW_FEATTYP'
  • row[2] is 'Streets_NW_SLIPRD'
  • row[3] is 'Streets_NW_RAMP'
  • row[4] is 'StreetName'
  • row[5] is 'Streets_NW_ID'
  • row[6] DOES NOT EXIST and is an out-of-range index

So your if statement translates to something like:

if (Streets_GC_FULLNAME is None and Streets_NW_RAMP == 2 and StreetName == 1)

Do you really have any features with StreetName == 1 ? I would guess that StreetName is a string, so testing for equality with 1 is probably not what you really want.

PROBLEM 3:

Assuming problem 1 gets fixed...

Similarly, row[5] = "Exit Ramp" (single '=') would translate to something like Streets_NW_ID = "Exit Ramp"

From your question, I don't think this is what you intended.

Solution:

You said in your question that you wanted to test for:

Streets_GC_FullName is null, the STREETS_NW_SLIPRD = 2 and STREETS_NW_RAMP=1, then change the Streetname field to "Exit Ramp"

This should be something like (untested):

        if row[0] is None and row[2] == 2 and row[3] == 1
            row[4] = "Exit Ramp"
            print "Updated {}".format(row[5])
            cursor.updateRow(row)
1
  • Thank You... The root of all of my problems was my muddled indexes. I kept counting from 1 instead of 0 and getting my fields confused. After resolving the indexes everything is good. What i was trying to do was update the StreetName field to Exit Ramp not test that particular field.
    – DanceDad
    Commented Nov 1, 2018 at 23:16

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.