2

I'm trying to assign a value to the attributes of each feature of a feature layer. To do so I am just iterating through each row using 'arcpy.da.SearchCursor'.

#Import geoprocessing.
import arcpy

#Import workspace.
arcpy.env.workspace = r'D:\ArcGIS_Pro_Projects\Optimity_Alf'

#Input parameters
buffer_distance = arcpy.GetParameterAsText(0)

#Input variables.
fc_Opt_All = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Equipment.gdb\Optimity_All'
fc_Opt_Opp = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\Optimity_Equipment.gdb\Optimity_Prospect'
fc_London_Bldngs = r'D:\ArcGIS_Pro_Projects\Optimity_Alf\London_Building.gdb\London_Buildings_Multipatch'
DSM_model = r'D:\Data4Optimimty\ESRI_UK_Bluesky_DSM\ESRI_UK_Bluesky_DSM.gdb\DSM_25cm_2015'


arcpy.MakeFeatureLayer_management(fc_Opt_Opp, 'fl_Opt_Opp')
field_names = ['LOCATION', 'ESTATE', 'ADDRESS', 'OFFSETA', 'OFFSETB', 'Z', 'OFFSETC', 'AZIMUTH1', 'AZIMUTH2', 'VERT1', 'VERT2', 'RADIUS1', 'RADIUS2', 'STATUS', 'CONNECTION']
observers = []
targets = []  
#Calculating LOS for each point
with arcpy.da.SearchCursor(fc_Opt_Opp,field_names) as cursor:
    for row in cursor:

        #Adding default values to observers' attributes
        row[1] = 'Prospect'
        row[3] = 1
        row[4] = 0
        arcpy.AddSurfaceInformation_3d('fl_Opt_Opp', DSM_model, 'Z', 'BILINEAR')
        row[6] = row[4] + row[5]
        row[7] = 0
        row[8] = 360
        row[9] = 1
        row[10] = -16       
        row[11] = 0
        row[12] = 2000      
        cursor.updateRow(row)   

However, I am getting the following error:

Traceback (most recent call last):
  File "D:\ArcGIS_Pro_Projects\Optimity_Alf\Arcpy_Scripts\LOS_Many_Points.py", line 28, in <module>
    row[1] = 'Prospect'
TypeError: 'tuple' object does not support item assignment
Failed to execute (LOSManyPoints).

I really can't see what tuple is the error referring to. Besides, if I do the same thing but for just one feature (not iterating) I do not get the error and it works fine.

6

You appear to be trying to update values in your data using a Search Cursor arcpy.da.SearchCursor() rather than an Update Cursor arcpy.da.UpdateCursor()

Try changing to an Update Cursor:

#Calculating LOS for each point
with arcpy.da.UpdateCursor(fc_Opt_Opp, field_names) as cursor:
    for row in cursor:

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.