2

I have two feature classes. I need to transfer attributes from a point feature class to a line feature class based on a combination of fields. They do not have matching field names. Here is an example of what I want to compare between my SearchCursor dictionary and my UpdateCursor table:

if row[0] in intdict AND row[2] in intdict != row[2] AND row[3] in intdict == row[3] AND row[4] in intdict = row[4] THEN row[6] = row[1] in intdict

intdict = {row[0]:row[1:] for row in arcpy.da.SearchCursor(interSectPts,['FID_ADDRSTAND','STREET', 'POINT_X','POINT_Y'])}
with arcpy.da.UpdateCursor(addrLyr,['FID_ADDRSTAND','STREET','STARTX_2','STARTY_2', 'ENDX_2', 'ENDY_2','FROM_','TO_']) as cursor:
     for row in cursor:
         if (row[0] in intdict):
            update row with value
            update row with value
            update row with value
            etc.....

To add context, I am changing the values of a road segment in the TO_ and FROM_ columns to show the street names of intersecting roads. For example a segment named 'Main St' could intersect '1st st' on the west end and '2nd st' on the east end...thus the attribute table would look like:

STREET   |    FROM_     |     TO_
Main St  |   1st St     |    2nd St

I basically an comparing the x,y coordinates of the intersection point feature class and its street names with the x,y of the starting and ending coordinates of street segments. When an intersection occurs, I am grabbing the values of the street name. My primary question is how to structure my 'if' in the update cursor...the rest, I feel like I could do....

1 Answer 1

1

I think this should work,

intdict = {row[0]:row[1:] for row in arcpy.da.SearchCursor(interSectPts,['FID_ADDRSTAND','STREET', 'POINT_X','POINT_Y'])}
with arcpy.da.UpdateCursor(addrLyr,['FID_ADDRSTAND','STREET','STARTX_2','STARTY_2', 'ENDX_2', 'ENDY_2','FROM_','TO_']) as cursor:
    for row in cursor:
        if (row[0] in intdict and intdict[row[0]][0] != row[1]):
            # pto = start
            if intdict[row[0]][1] == row[2] and intdict[row[0]][2] == row[3]:
                # from = street
                row[6] = intdict[row[0]][0]
            # pto = end
            if intdict[row[0]][1] == row[4] and intdict[row[0]][2] == row[5]:
                # to = street
                row[7] = intdict[row[0]][0]
            cursor.updateRow(row)

One last thing, remember that you can filter the result using where_clause, so in this case you could avoid checking if row[0] in intdict and most important you get only the result you want to update.

with arcpy.da.UpdateCursor(
    addrLyr,
    ['FID_ADDRSTAND','STREET','STARTX_2','STARTY_2', 'ENDX_2', 'ENDY_2','FROM_','TO_'],
    where_clause="'FID_ADDRSTAND' IN ({})".format(','.join(intdict.keys()))
) as cursor:
...
16
  • Thank you for your help! I tried your example (before adding the where clause)...it didn't work but I think I can sort out why if you can explain: intdict[row[0]][1] == row[2] . What is that line of code saying? I dont understand the [row[0]][1]. Feb 7, 2020 at 4:24
  • row[0] is the key in intdict, and [1] indexes POINT_X .. what was the problem?
    – cabesuon
    Feb 7, 2020 at 5:20
  • Thanks for clarifying! I am still trying to get the code to work.....I don't think it is establishing a relationship with the key field.....when I ran the code, it completed successfully but didn't update the FROM/TO field where there are obviously intersections. I am going to keep working at it this morning. Feb 7, 2020 at 16:04
  • 1
    Ok, from what I can tell, your code worked just fine. The issue was the intersectPts feature class had multiple coincident points. A point for each segment at the intersection --- so in most circumstances, it had 3 points per intersection. I am going to first identify the points that have matching x,y and streetname to the segments, remove them from the feature class, and then create a dictionary from remaining points. I'll update my post if that works. Feb 7, 2020 at 20:57
  • That would be great
    – cabesuon
    Feb 8, 2020 at 1:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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