Select to view content in your preferred language

Stopiteration error and failure with CalculateField_Management update cursor

1221
2
Jump to solution
05-24-2021 08:33 AM
kad37_pns
Emerging Contributor

Hello! 

I'm trying to run a field calculation on a set of selected features from my layer. While my custom button may be considered "functioning" when I hard program into the python window on arcmap, I'll get an error on my update cursor saying" stopiteration: iteration not started". Additionally, when I do manage to get my print statement back, the calculation isn't being run, I'm getting statements such as "This is the new acreage: layer name" for however many polygons I've selected. 

 

I know it can be done using calculate geometry, but I'd like to be able to streamline this process as it'll be used when subdivisions are drawn out and save on the amount of button clicks for each individual polygon. I have to calculate the geometry for both fields "calc_acre" (acres) and "shape_leng" (sq ft), but I'm currently trying to get calc_acre working before I move to the next row. 

 

My code: 

import arcpy
import pythonaddins

class CalcGeometry(object):
    """Implementation for CalcGeometry_addin.button (Button)"""
    def __init__(self):
        self.enabled = True
        self.checked = False
    def onClick(self):
		mxd = arcpy.mapping.MapDocument('current')
		Fields = ["CALC_ACRE", "SHAPE_LENG"]
        lyr = arcpy.mapping.ListLayers(mxd)[0]
        sample = lyr.getSelectionSet()
		with arcpy.da.UpdateCursor(lyr, Fields) as Calc:
			for row in Calc: 
				row[0] = arcpy.CalculateField_management(lyr, "CALC_ACRE", "!shape.area@acres!", "PYTHON", "" )
				print("this is the new acreage: {}".format(row[0]))	
			Calc.updateRow(row)
0 Kudos
1 Solution

Accepted Solutions
DanPatterson
MVP Esteemed Contributor

CalculateField_management is not designed to be used in a cursor.  It does the calculations on a featureclass all at once.  If you want to use a cursor, then get the geometry and use arcpy methods to calculate the area.


... sort of retired...

View solution in original post

2 Replies
DanPatterson
MVP Esteemed Contributor

CalculateField_management is not designed to be used in a cursor.  It does the calculations on a featureclass all at once.  If you want to use a cursor, then get the geometry and use arcpy methods to calculate the area.


... sort of retired...
BlakeTerhune
MVP Regular Contributor

I would also like to add that you probably want to be using "PYTHON_9.3" as the expression_type parameter for CalculateField.

0 Kudos