Select to view content in your preferred language

Alternative for Wildcards in Update Cursor

4363
12
Jump to solution
04-20-2016 02:53 PM
DonDailey
Emerging Contributor

I've got a point feature class of road intersections in which I used a script to create a field of joined road names from a line feature class. I've got to separate each road name into separate fields. I've been able to do that without much problem. But the road names were joined in more or less random fashion and now I need to rank the road names based on their type into their own field i.e. Interstates in the first field, US Highways in the second, state highways in the third, etc. All the highways begin with either Interstate, US or State.

I've tried doing a wildcard if ... else on the list created by the .split inside the update cursor but apparently that's not allowed. From searching it seems that regular expressions (which I've never used) might be my only hope. Anybody have any suggestions?

The code I used to split the field and place the road names into individual fields.

try:
# Import arcpy module
    from arcpy import env

#set workspace
    env.workspace = r"GDB"

# Local variables:
    fc = r"feature class"
    
#create update cursor to place values in fields
    fieldList = ["AllRoutes","ID_Road1_Cross","ID_Road2_Cross","ID_Road3_Cross",
                 "ID_Road4_Cross","ID_Road5_Cross"]
    with arcpy.da.UpdateCursor(fc,fieldList) as cursor:
        for row in cursor:
              cleaned = [x.strip() for x in row[0].split("&")]
              if len(cleaned) == 2:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
              elif len(cleaned) == 3:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
              elif len(cleaned) == 4:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
                row[4] = cleaned[3]
              elif len(cleaned) == 5:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
                row[4] = cleaned[3]
                row[5] = cleaned[4]
              cursor.updateRow(row)


except arcpy.ExecuteError:
    print arcpy.GetMessage(2)

except:
    print "Process did not complete"
0 Kudos
12 Replies
DanPatterson_Retired
MVP Emeritus

have you ruled out reordering your fields, or for such a small file, producing a new one, with the fields in hierachical order to begin with.  That would sure speed up (in terms of code writing and testing) and simplify the concatenation issue.

0 Kudos
DonDailey
Emerging Contributor

My first thought was, like you mention, to figure out a way to order the route field so that the routes would be concatenated in the right order to begin with. But the method I used to get the attributes was to create a point class using the intersect tool on our route lines and then doing a spatial join of the points and the route lines they touch to get the route names. I couldn't see a way of forcing the route names to be joined in a particular order. I'm assuming that even if the field values are grabbed in the same direction for each spatial join (clockwise or north to south, etc.)  the result would be basically a random ordering because the intersecting routes come from all different directions from intersection to intersection.

0 Kudos
DonDailey
Emerging Contributor

This is what I ended up using, a combo of the two code suggestions. It slotted the routes into the fields in this hierarchy: Interstate > US > State > Local. Thanks for the help. My python scripting knowledge isn't deep and I learned some things in figuring this one out.

fieldList = ["AllRoutes","ID_Road1_Cross","ID_Road2_Cross","ID_Road3_Cross","ID_Road4_Cross","ID_Road5_Cross"]
   
    with arcpy.da.UpdateCursor(fc,fieldList) as cursor:
        for row in cursor:
              cleaned = [x.strip() for x in row[0].split("&") ]
              rds = []
              for rd in cleaned:
                rdup = rd.upper()
                if rdup.startswith('INTER'): key = 0
                elif rdup.startswith('US'): key = 1 # US route
                elif rdup.startswith('STATE'): key = 2 # state route
                else: key = 3 #local routes
                rds.append([key, rd])
                rds.sort()
              if len(rds) == 2:
                row[1] = rds[0][1]
                row[2] = rds[1][1]
              elif len(rds) == 3:
                row[1] = rds[0][1]
                row[2] = rds[1][1]
                row[3] = rds[2][1]
              elif len(rds) == 4:
                row[1] = rds[0][1]
                row[2] = rds[1][1]
                row[3] = rds[2][1]
                row[4] = rds[3][1]
              elif len(rds) == 5:
                row[1] = rds[0][1]
                row[2] = rds[1][1]
                row[3] = rds[2][1]
                row[4] = rds[3][1]
                row[5] = rds[4][1]
              cursor.updateRow(row)
0 Kudos