I am trying to total the lengths of line segments. I know you can see this by right clicking field name and selecting summarize. How do I pass this value to a field via python.
Here is what I have tried.
expression = "sum(float(!SHAPE.length!))" arcpy.CalculateField_management(in_table, field_name, expression, "PYTHON_9.3")
The error I receive tells me that the 'float object is not iterable'. Which tells me maybe I should be using a search cursor?
I have also tried this:
expression = "getSum(float(!SHAPE.length!))" codeblock = """def getSum(length): b = sum(row for row in arcpy.da.SearchCursor(fc, 'length')) return b """ arcpy.CalculateField_management(in_table, field_name, expression, "PYTHON_9.3", codeblock)
But I get global fc not defined. How do I pass fc to codeblock.
What are my best options for performing this simple task?
Update: I found a solution using geometry objects. Reference here.
geometries = arcpy.CopyFeatures_management(fc, arcpy.Geometry()) # Walk through each geometry, totaling the length # length = sum([g.length for g in geometries]) # Execute CalculateField arcpy.CalculateField_management(in_table, field_name, length, "PYTHON_9.3")