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.
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\\[email protected]\\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.
updateRow
(list),updateRows
(cursor),updateRow
(string), andarcpy.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 usedrow
,cursor
, andvalue
for the names over which you have control, then you would see thatcursor.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.