ArcGIS Calculate Field Tool – Updating Feature Coordinates Using Calculate Field GP Tool

arcgis-10.7arcmaparcpyerror-000539field-calculator

ArcMap 10.7.1 (Oracle SDE.ST_GEOMETRY):

I want to use the Calculate Field GP tool to update the coordinates of a polyline FC's shape column.


I have a script that works in a standalone Python IDE (PyScripter):

Source: Set M-values to cumulative length of line (via ArcPy)

import arcpy
connection = "Database Connections\my_conn.sde"
feature_class = connection + "\my_owner.my_fc"
spatial_reference = arcpy.Describe(feature_class).spatialReference

with arcpy.da.Editor(connection) as edit_session:
    with arcpy.da.UpdateCursor(feature_class, "SHAPE@") as cursor:
        for row in cursor:
            geometry = row[0].densify("ANGLE", 10000, 0.174533)
            parts = arcpy.Array()
            for part in geometry:
                points = arcpy.Array()
                for point in part:
                    point.M = geometry.measureOnLine(point)
                    points.append(point)
                parts.append(points)
            row[0] = arcpy.Polyline(parts, spatial_reference)
            cursor.updateRow(row)

I want to use that same logic, but in the Calculate Field GP tool:

Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
def new_shape(geometry):
    geometry = geometry.densify("ANGLE", 10000, 0.174533)
    parts = arcpy.Array()
    for part in geometry:
        points = arcpy.Array()
        for point in part:
            point.M = geometry.measureOnLine(point)
            points.append(point)
        parts.append(points)
    return arcpy.Polyline(parts)

When I run that Calculate Field code, I get an error:

"geoprocessing describe geometry object' object is not iterable"

ERROR 000539: Error running expression: new_shape( GPVARIANTOBJECT0 ) 
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 4, in new_shape
TypeError: 'geoprocessing describe geometry object' object is not iterable

Failed to execute (CalculateField).

What am I doing wrong?

The geometry was iterable when using the full blown script in PyScripter. I don't understand why it isn't iterable in the Calculate Field script too.

Best Answer

Some years ago ESRI changed the behaviour of field calculator. In the past you could directly manipulate and update the Shape field as you are attempting within a field calculator. This functionality was removed for what ever reason and you can only do it through arcpy.

So basically you can't do what you are trying. If your intention was to run the tool in model builder you could try your first code sample within the model only calculate value tool.

Related Question