0

I get new points (inspections) every day. Each point falls within a polygon (district). I need to update 3 fields (Week, RecycleRt, RecyCollDay) in the point feature class with the district fields. Districts and inspections

I did some research and found a script that does something similar to what I need. So modified the script in answer to this post Selecting points within polygon and updating attributes using ArcPy?

Here's my script:

import arcpy, os


arcpy.env.workspace = "\\\\Projects\\RecyclingAudit\\RecyclingAuditUpdate"
PW = "\\\\Projects\\RecyclingAudit\\RecyclingAuditUpdate\\UpdateAudits.gdb"
RecyDistrict = "\\\\connections\\SDELOAD@Pub.sde\\RecyclingDistrict"
RecyAudit = PW +"\\RecyclingAudit"
RecyAuditFieldList = ["Week","RecycleRt","RecyCollDay"]
SpatialJoin = PW + "\\SpatialJoin_RA"
SpatialJoinFields = ["Week", "RecycleRt", "RecyCollDay"]

arcpy.env.overwriteOutput = True

# Delete scratch spatial join feature
if arcpy.Exists(SpatialJoin):
    arcpy.Delete_management(SpatialJoin)

# Scratch spatial join feature
arcpy.SpatialJoin_analysis(RecyAudit, RecyDistrict, SpatialJoin, "JOIN_ONE_TO_ONE", "KEEP_ALL",
                           'Crdt "Crdt" false true false 8 Date 0 0,First,#,RecyclingAudit,Crdt,-1,-1;'
                           'Address1 "address" true true false 255 Text 0 0,First,#,RecyclingAudit,Address1,0,255;'
                           'Classification "Classification" true true false 255 Text 0 0,First,#,RecyclingAudit,Classification,0,255;'
                           'Status "Status" true true false 10 Text 0 0,First,#,RecyclingAudit,Status,0,10;'
                           'Compliance "Compliance" true true false 5 Text 0 0,First,#,RecyclingAudit,Compliance,0,5;'
                           'Week "Week" true true false 50 Text 0 0,First,#,SolidWasteRecyclingDistrict,Week,0,50;'
                           'RecycleRt "RecycleRt" true true false 50 Text 0 0,First,#,SolidWasteRecyclingDistrict,RecycleRt,0,50;'
                           'RecyCollDay "RecyCollDay" true true false 50 Text 0 0,First,#,SolidWasteRecyclingDistrict,RecyCollDay,0,50',
                           "INTERSECT", None, '')

   
# Start edit session
# arcpy.env.workspace doesn't work
edit = arcpy.da.Editor(PW)
edit.startEditing(True, False)
edit.startOperation()

# Populate the dictionary from recycling district
valueDict = {r[0]:(r[1:]) for r in arcpy.da.SearchCursor(SpatialJoin, SpatialJoinFields)}
with arcpy.da.UpdateCursor(RecyAudit, RecyAuditFieldList) as updateRows:
    for updateRow in updateRows:
        keyValue = updateRow[0]
        if keyValue in valueDict:
            for n in range (1,len(SpatialJoinFields)):
                updateRow = valueDict[keyValue][n-1]
            updateRows.updateRow(updateRow)
    del valueDict


# Stop edit operation, stop edit session and save changes
edit.stopOperation()
edit.stopEditing(True)

I'm getting the following error:

Traceback (most recent call last): File "X:/Scripts/Python/TEST/ShortRecyclingAuditUpdate.py", line 45, in updateRows.updateRow(updateRow) TypeError: sequence size must match size of the row

How can I fix this error? This is the first time I work with da.UpdateCursor and I don't understand exactly how it works. I think for this to work correctly it needs to match Crdt (create date) field on both feature classes. If it finds a match, then it should update the fields.

2
  • 1
    It's pretty confusing having updateRow (list), updateRows (cursor), updateRow (string), and arcpy.da.UpdateCursor.updateRow (method) all in one code block (this is the source of your error as well). Your code would be better self-documenting if you used row, cursor, and value for the names over which you have control, then you would see that cursor.updateRow(value) doesn't meet the first parameter expectation of a list of length (len(RecyAuditFieldList)). Also, best practice is to restrict uppercase names to classes.
    – Vince
    Jul 22 at 18:48
  • Not sure, but this answer I provided might be useful for you... somewhat related to what you're trying to do (I think...) gis.stackexchange.com/a/87055/18630 Jul 24 at 2:17
0

OK, I figured out the error. I had to add a common field to both field lists (RecyAuditFieldList and SpatialJoinFields) which are OBJECTID and TARGET_FID. The edited list looks like this:

RecyAuditFieldList = ["OID@","Week","RecycleRt","RecyCollDay"]
SpatialJoinFields = ["TARGET_FID","Week", "RecycleRt", "RecyCollDay"]
1
  • 1
    Those changes won't fix the error in the message.
    – Vince
    Jul 24 at 0:22

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.