<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Use Calculate Field GP tool to update feature coordinates in ArcMap Questions</title>
    <link>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1144342#M3461</link>
    <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;Solved:&lt;/H2&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For whatever reason,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;U&gt;&lt;EM&gt;for point in part:&lt;/EM&gt;&lt;/U&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;doesn't seem to work in the Field Calculator. It runs without errors, but it doesn't actually loop through the points. So the script returns an empty shape, which isn't what we want. I'm not sure what the root cause is.&lt;/P&gt;&lt;P&gt;But if we replace it with:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;for&lt;/SPAN&gt; j &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(part.count):
    point = part.getObject(j)&lt;/PRE&gt;&lt;P&gt;...then that works as expected. It loops through the points.&lt;/P&gt;&lt;HR /&gt;&lt;PRE&gt;Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geom&lt;/SPAN&gt;):
    spatial_reference = geom.spatialReference
    geom = geom.densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; i &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(geom.partCount):
        part = geom.getPart(i)                      &lt;SPAN class=""&gt;#I think "part" is an ArcPy array: https://desktop.arcgis.com/en/arcmap/10.7/analyze/arcpy-classes/array.htm&lt;/SPAN&gt;
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; j &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(part.count):                 &lt;SPAN class=""&gt;#Alternatively, this would work in the Calculate Field GP tool too: "for j in range(len(part)):"   ...and this as well: "for j, point in enumerate(part):"&lt;/SPAN&gt;
                                                    &lt;SPAN class=""&gt;#...but "for point in part:"  doesn't work in the Calculate Field GP tool. It runs without errors, but it doesn't actually loop through the points. So the script returns an empty shape, which isn't what we want.&lt;/SPAN&gt;

                                                    &lt;SPAN class=""&gt;#Note: this doesn't work in the Calculate Field GP tool: "point = part[j]"  ...although that might work in newer versions of ArcPy. See a newer version of the ArcPy Array docs: "The getObject method is equivalent to indexing an object; that is, obj.getObject(0) is equivalent to obj[0]."&lt;/SPAN&gt;
            point = part.getObject(j)               &lt;SPAN class=""&gt;#But this does work: ".getObject(j)"&lt;/SPAN&gt;
            point.M = geom.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts, spatial_reference)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://i.stack.imgur.com/g5PUN.png" target="_blank" rel="nofollow noopener noreferrer"&gt;Screenshot&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;of using the field calculator on the shape column.&lt;/P&gt;</description>
    <pubDate>Tue, 15 Feb 2022 22:13:48 GMT</pubDate>
    <dc:creator>Bud</dc:creator>
    <dc:date>2022-02-15T22:13:48Z</dc:date>
    <item>
      <title>Use Calculate Field GP tool to update feature coordinates</title>
      <link>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1143859#M3458</link>
      <description>&lt;P&gt;&lt;EM&gt;ArcMap 10.7.1 (Oracle SDE.ST_GEOMETRY):&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;I'm hoping to use the&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;STRONG&gt;Calculate Field GP tool&lt;/STRONG&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;to update the coordinates of a polyline FC's shape column.&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;I have a script that works in a standalone Python IDE (PyScripter):&lt;/P&gt;&lt;P&gt;Source:&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;A href="https://gis.stackexchange.com/a/423171/62572" target="_blank" rel="noopener"&gt;Set M-values to cumulative length of line (via ArcPy)&lt;/A&gt;&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; arcpy
connection = &lt;SPAN class=""&gt;"Database Connections\my_conn.sde"&lt;/SPAN&gt;
feature_class = connection + &lt;SPAN class=""&gt;"\my_owner.my_fc"&lt;/SPAN&gt;
spatial_reference = arcpy.Describe(feature_class).spatialReference

&lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.Editor(connection) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; edit_session:
    &lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.UpdateCursor(feature_class, &lt;SPAN class=""&gt;"SHAPE@"&lt;/SPAN&gt;) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; cursor:
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; row &lt;SPAN class=""&gt;in&lt;/SPAN&gt; cursor:
            geometry = row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;].densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
            parts = arcpy.Array()
            &lt;SPAN class=""&gt;for&lt;/SPAN&gt; part &lt;SPAN class=""&gt;in&lt;/SPAN&gt; geometry:
                points = arcpy.Array()
                &lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
                    point.M = geometry.measureOnLine(point)
                    points.append(point)
                parts.append(points)
            row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;] = arcpy.Polyline(parts, spatial_reference)
            cursor.updateRow(row)&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;I want to use that same logic, but in the Calculate Field GP tool:&lt;/P&gt;&lt;P&gt;&lt;A href="https://i.stack.imgur.com/D5zAC.png" target="_blank" rel="nofollow noopener noreferrer"&gt;Screenshot&lt;/A&gt;&lt;/P&gt;&lt;PRE&gt;Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geometry&lt;/SPAN&gt;):
    geometry = geometry.densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; part &lt;SPAN class=""&gt;in&lt;/SPAN&gt; geometry:
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
            point.M = geometry.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts)&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;But when I run that Calculate Field code, I get an error:&lt;/P&gt;&lt;BLOCKQUOTE&gt;&lt;P&gt;"geoprocessing describe geometry object' object is not iterable"&lt;/P&gt;&lt;/BLOCKQUOTE&gt;&lt;PRE&gt;ERROR 000539: Error running expression: new_shape( GPVARIANTOBJECT0 ) 
Traceback (most recent call last):
  File &lt;SPAN class=""&gt;"&amp;lt;expression&amp;gt;"&lt;/SPAN&gt;, line &lt;SPAN class=""&gt;1&lt;/SPAN&gt;, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &amp;lt;module&amp;gt;
  File &lt;SPAN class=""&gt;"&amp;lt;string&amp;gt;"&lt;/SPAN&gt;, line &lt;SPAN class=""&gt;4&lt;/SPAN&gt;, &lt;SPAN class=""&gt;in&lt;/SPAN&gt; new_shape
TypeError: &lt;SPAN class=""&gt;'geoprocessing describe geometry object'&lt;/SPAN&gt; &lt;SPAN class=""&gt;object&lt;/SPAN&gt; &lt;SPAN class=""&gt;is&lt;/SPAN&gt; &lt;SPAN class=""&gt;not&lt;/SPAN&gt; iterable

Failed to execute (CalculateField).&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;&lt;STRONG&gt;Question:&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;What am I doing wrong?&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;HR /&gt;&lt;P&gt;&lt;STRONG&gt;Edit&lt;/STRONG&gt;:&lt;/P&gt;&lt;P&gt;I can get &lt;EM&gt;part&lt;/EM&gt; of the script work. I can densify the shape:&lt;/P&gt;&lt;P&gt;&lt;A href="https://i.stack.imgur.com/njkMo.png" target="_blank" rel="nofollow noopener noreferrer"&gt;Screenshot&lt;/A&gt;&lt;/P&gt;&lt;PRE&gt;Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
def new_shape(geometry):&lt;BR /&gt;    geometry = geometry.densify("ANGLE", 10000, 0.174533)&lt;BR /&gt;    return geometry&lt;/PRE&gt;&lt;P&gt;&lt;EM&gt;(And if I wanted to, I could also set the shape to None via the Calculate Field GP tool.)&lt;/EM&gt;&lt;/P&gt;&lt;P&gt;But when I try to loop through the geometry's parts, I get the aforementioned error.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any ideas? Thanks!&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 01:30:16 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1143859#M3458</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-15T01:30:16Z</dc:date>
    </item>
    <item>
      <title>Re: Use Calculate Field GP tool to update feature coordinates</title>
      <link>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1143907#M3460</link>
      <description>&lt;P&gt;The following code&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;EM&gt;almost&lt;/EM&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;works.&lt;/P&gt;&lt;P&gt;It runs without errors in the Calculate Field GP tool.&lt;/P&gt;&lt;P&gt;But unfortunately, the shape column gets set to null (not what I want).&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the problem is: When the function is used by the Calculate Field gp tool, the points aren't being iterated (this bit is being skipped: &lt;EM&gt;&lt;U&gt;for point in part:&lt;/U&gt;&lt;/EM&gt;). So the shape that is returned is empty/invalid...that's why the shape is being set to null.&lt;/P&gt;&lt;PRE&gt;Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geom&lt;/SPAN&gt;):
    spatial_reference =  geom.spatialReference
    geom = geom.densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; i &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(geom.partCount):
        part = geom.getPart(i)
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
            point.M = geom.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts, spatial_reference)&lt;/PRE&gt;&lt;HR /&gt;&lt;P&gt;For what it's worth, the Python function above works fine, when run within a full-blown script in an IDE like PyScripter:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;import&lt;/SPAN&gt; arcpy
connection = &lt;SPAN class=""&gt;"Database Connections\my_conn.sde"&lt;/SPAN&gt;
feature_class = connection + &lt;SPAN class=""&gt;"\my_owner.my_fc"&lt;/SPAN&gt;
&lt;SPAN class=""&gt;&lt;BR /&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geom&lt;/SPAN&gt;):
    spatial_reference =  geom.spatialReference
    geom = geom.densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; i &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(geom.partCount):
        part = geom.getPart(i)
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; point &lt;SPAN class=""&gt;in&lt;/SPAN&gt; part:
            point.M = geom.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts, spatial_reference)

&lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.Editor(connection) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; edit_session:
    &lt;SPAN class=""&gt;with&lt;/SPAN&gt; arcpy.da.UpdateCursor(feature_class, [&lt;SPAN class=""&gt;"SHAPE@"&lt;/SPAN&gt;,&lt;SPAN class=""&gt;"OID@"&lt;/SPAN&gt;]) &lt;SPAN class=""&gt;as&lt;/SPAN&gt; cursor:
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; row &lt;SPAN class=""&gt;in&lt;/SPAN&gt; cursor:
            &lt;SPAN class=""&gt;if&lt;/SPAN&gt; row[&lt;SPAN class=""&gt;1&lt;/SPAN&gt;] == &lt;SPAN class=""&gt;1928&lt;/SPAN&gt;:
                geom = new_shape(row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;])

                row[&lt;SPAN class=""&gt;0&lt;/SPAN&gt;] = geom
                cursor.updateRow(row)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;But when I use the function in the Calculate Field tool (in ArcMap--&amp;gt;Calculate Field GP, or as a standalone Calculate Field GP script in PyScripter), the shape gets set to null.&lt;/P&gt;&lt;P&gt;I think the problem is: When the function is used by the Calculate Field gp tool, the points aren't being iterated properly (in other words, this bit is being skipped:&amp;nbsp;&lt;U&gt;&lt;EM&gt;for point in part:&lt;/EM&gt;&lt;/U&gt;). So the shape that is returned is invalid and that's why the shape is being set to null.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 22:13:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1143907#M3460</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-15T22:13:27Z</dc:date>
    </item>
    <item>
      <title>Re: Use Calculate Field GP tool to update feature coordinates</title>
      <link>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1144342#M3461</link>
      <description>&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;H2&gt;Solved:&lt;/H2&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For whatever reason,&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;U&gt;&lt;EM&gt;for point in part:&lt;/EM&gt;&lt;/U&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;doesn't seem to work in the Field Calculator. It runs without errors, but it doesn't actually loop through the points. So the script returns an empty shape, which isn't what we want. I'm not sure what the root cause is.&lt;/P&gt;&lt;P&gt;But if we replace it with:&lt;/P&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;for&lt;/SPAN&gt; j &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(part.count):
    point = part.getObject(j)&lt;/PRE&gt;&lt;P&gt;...then that works as expected. It loops through the points.&lt;/P&gt;&lt;HR /&gt;&lt;PRE&gt;Expression:
-----------
new_shape( !SHAPE! )

Code Block:
-----------
&lt;SPAN class=""&gt;def&lt;/SPAN&gt; &lt;SPAN class=""&gt;new_shape&lt;/SPAN&gt;(&lt;SPAN class=""&gt;geom&lt;/SPAN&gt;):
    spatial_reference = geom.spatialReference
    geom = geom.densify(&lt;SPAN class=""&gt;"ANGLE"&lt;/SPAN&gt;, &lt;SPAN class=""&gt;10000&lt;/SPAN&gt;, &lt;SPAN class=""&gt;0.174533&lt;/SPAN&gt;)
    parts = arcpy.Array()
    &lt;SPAN class=""&gt;for&lt;/SPAN&gt; i &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(geom.partCount):
        part = geom.getPart(i)                      &lt;SPAN class=""&gt;#I think "part" is an ArcPy array: https://desktop.arcgis.com/en/arcmap/10.7/analyze/arcpy-classes/array.htm&lt;/SPAN&gt;
        points = arcpy.Array()
        &lt;SPAN class=""&gt;for&lt;/SPAN&gt; j &lt;SPAN class=""&gt;in&lt;/SPAN&gt; &lt;SPAN class=""&gt;range&lt;/SPAN&gt;(part.count):                 &lt;SPAN class=""&gt;#Alternatively, this would work in the Calculate Field GP tool too: "for j in range(len(part)):"   ...and this as well: "for j, point in enumerate(part):"&lt;/SPAN&gt;
                                                    &lt;SPAN class=""&gt;#...but "for point in part:"  doesn't work in the Calculate Field GP tool. It runs without errors, but it doesn't actually loop through the points. So the script returns an empty shape, which isn't what we want.&lt;/SPAN&gt;

                                                    &lt;SPAN class=""&gt;#Note: this doesn't work in the Calculate Field GP tool: "point = part[j]"  ...although that might work in newer versions of ArcPy. See a newer version of the ArcPy Array docs: "The getObject method is equivalent to indexing an object; that is, obj.getObject(0) is equivalent to obj[0]."&lt;/SPAN&gt;
            point = part.getObject(j)               &lt;SPAN class=""&gt;#But this does work: ".getObject(j)"&lt;/SPAN&gt;
            point.M = geom.measureOnLine(point)
            points.append(point)
        parts.append(points)
    &lt;SPAN class=""&gt;return&lt;/SPAN&gt; arcpy.Polyline(parts, spatial_reference)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://i.stack.imgur.com/g5PUN.png" target="_blank" rel="nofollow noopener noreferrer"&gt;Screenshot&lt;/A&gt;&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;of using the field calculator on the shape column.&lt;/P&gt;</description>
      <pubDate>Tue, 15 Feb 2022 22:13:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcmap-questions/use-calculate-field-gp-tool-to-update-feature/m-p/1144342#M3461</guid>
      <dc:creator>Bud</dc:creator>
      <dc:date>2022-02-15T22:13:48Z</dc:date>
    </item>
  </channel>
</rss>

