<?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: update cursor fails to work in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554124#M73126</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/640285"&gt;@BrandonMcAlister&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks i tried that and it still failed, i even recreated the data and re-ran it incase the data had been corupted&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid in (4011345,4011328,4011327,4011326,4011325,4011324,4011323,4011346)") as Cur:
    for row in Cur:

        # read in the data

        print row[0]

        SA_Filter = "OBJECTID = {0}".format(row[0])
        print SA_Filter
        try:
            arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
            annocount = arcpy.management.GetCount(fc)
            print "tmp count: {0}".format(annocount[0])
            if int(annocount[0]) == 1:
                print "selected one anno"

                arcpy.management.SelectLayerByLocation(fc, "INTERSECT", fc, "", "NEW_SELECTION")
                tmpcount = arcpy.management.GetCount(fc)
                print "tmp count: {0}".format(tmpcount[0])
                if int(tmpcount[0]) &amp;gt;= 2 and int(tmpcount[0]) &amp;lt; 74:
                    print "more than 1"
                    with arcpy.da.UpdateCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid = {0}".format(row[0])) as UPDCur:
                        for upd in UPDCur:
                            print "entered Update cursot"
                            upd[2] = float(upd[2]) + 90
                            upd[6] = "te22st"
                            UPDCur.updateRow(upd)
        except Exception as e:
            print "Error with message: {0}".format(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_0-1730365975858.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118535iCE853A471DEEA71C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_0-1730365975858.png" alt="StuartMoore_0-1730365975858.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 31 Oct 2024 09:13:06 GMT</pubDate>
    <dc:creator>StuartMoore</dc:creator>
    <dc:date>2024-10-31T09:13:06Z</dc:date>
    <item>
      <title>update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553790#M73115</link>
      <description>&lt;P&gt;so this is to check if any annotation overlaps and if it does it rotates one by 90 degrees&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this just loops through the features and works fine&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid in (4007789,4007788,4007787,4007786,4007791,4007790)") as Cur:
    for row in Cur:

        # read in the data

        print row[0]

        SA_Filter = "OBJECTID = {0}".format(row[0])
        try:
            arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
            annocount = arcpy.management.GetCount(fc)
            print "tmp count: {0}".format(annocount[0])
            if int(annocount[0]) == 1:
                print "selected one anno"

                arcpy.management.SelectLayerByLocation(fc, "INTERSECT", fc, "", "NEW_SELECTION")
                tmpcount = arcpy.management.GetCount(fc)
                print "tmp count: {0}".format(tmpcount[0])
                if int(tmpcount[0]) &amp;gt;= 2 and int(tmpcount[0]) &amp;lt; 74:
                    print "more than 1"
                    row[2] = float(row[2]) + 90
        except:
            print "Error"&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_0-1730301393285.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118458i1EDDE2DD66D8DC8A/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_0-1730301393285.png" alt="StuartMoore_0-1730301393285.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;but when i add in a simple update to a text field it fails&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.UpdateCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid in (4007789,4007788,4007787,4007786,4007791,4007790)") as Cur:
    for row in Cur:

        # read in the data

        print row[0]

        SA_Filter = "OBJECTID = {0}".format(row[0])
        try:
            arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
            annocount = arcpy.management.GetCount(fc)
            print "tmp count: {0}".format(annocount[0])
            if int(annocount[0]) == 1:
                print "selected one anno"

                arcpy.management.SelectLayerByLocation(fc, "INTERSECT", fc, "", "NEW_SELECTION")
                tmpcount = arcpy.management.GetCount(fc)
                print "tmp count: {0}".format(tmpcount[0])
                if int(tmpcount[0]) &amp;gt;= 2 and int(tmpcount[0]) &amp;lt; 74:
                    print "more than 1"
                    row[2] = float(row[2]) + 90
                    row[6] = "updated"
                    Cur.updateRow(row)
        except:
            print "Error"&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_1-1730301572174.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118459i92E3BE70FD472C41/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_1-1730301572174.png" alt="StuartMoore_1-1730301572174.png" /&gt;&lt;/span&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_2-1730301588452.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118460i5BA786A0A5D335FC/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_2-1730301588452.png" alt="StuartMoore_2-1730301588452.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 15:20:10 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553790#M73115</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2024-10-30T15:20:10Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553805#M73116</link>
      <description>&lt;P&gt;Hey&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/159798"&gt;@StuartMoore&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;In your try except error, can you add a message that prints the error inside? It may be more descriptive and point out which part is causing an issue.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;except Exception as e:
   print(f"Error with message: {e}")&lt;/LI-CODE&gt;&lt;P&gt;From the surface, it's slightly difficult to see what's going on, especially since the error is saying the select by attribute is causing the issue. I would also print SA_Filter to see what it's printing.&lt;/P&gt;&lt;P&gt;Cody&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 15:44:30 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553805#M73116</guid>
      <dc:creator>CodyPatterson</dc:creator>
      <dc:date>2024-10-30T15:44:30Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553850#M73117</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/712076"&gt;@CodyPatterson&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;this is what i get:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_0-1730306670750.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118473iA5F41B1B0F76F02F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_0-1730306670750.png" alt="StuartMoore_0-1730306670750.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;i think the issue is with this line as it works in terms of selecting the features by their objectid and also the select by location part.&lt;/P&gt;&lt;PRE&gt;Cur.updateRow(row)&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;i'm running this in Desktop 10.2.1 on a versioned dataset. i'm very confident that it was working yesterday but today its being a pain...&lt;/P&gt;&lt;P&gt;i've used similar update cursors and its worked but this time after the first feature it seems to loose connection with the database as the features disappear from the map and this pops up:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_1-1730307197378.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118476i4B47A2BA5218F926/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_1-1730307197378.png" alt="StuartMoore_1-1730307197378.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 16:54:00 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553850#M73117</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2024-10-30T16:54:00Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553950#M73118</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/159798"&gt;@StuartMoore&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think the issue lies with the fact that you are using an update cursor as a search cursor. I would create a search cursor to get object ids and evaluate their position and information with that. Then when I run into a feature that needs to be turned create an update cursor with a where clause that updates that object id.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 30 Oct 2024 19:51:37 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1553950#M73118</guid>
      <dc:creator>BrandonMcAlister</dc:creator>
      <dc:date>2024-10-30T19:51:37Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554124#M73126</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/640285"&gt;@BrandonMcAlister&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks i tried that and it still failed, i even recreated the data and re-ran it incase the data had been corupted&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid in (4011345,4011328,4011327,4011326,4011325,4011324,4011323,4011346)") as Cur:
    for row in Cur:

        # read in the data

        print row[0]

        SA_Filter = "OBJECTID = {0}".format(row[0])
        print SA_Filter
        try:
            arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
            annocount = arcpy.management.GetCount(fc)
            print "tmp count: {0}".format(annocount[0])
            if int(annocount[0]) == 1:
                print "selected one anno"

                arcpy.management.SelectLayerByLocation(fc, "INTERSECT", fc, "", "NEW_SELECTION")
                tmpcount = arcpy.management.GetCount(fc)
                print "tmp count: {0}".format(tmpcount[0])
                if int(tmpcount[0]) &amp;gt;= 2 and int(tmpcount[0]) &amp;lt; 74:
                    print "more than 1"
                    with arcpy.da.UpdateCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid = {0}".format(row[0])) as UPDCur:
                        for upd in UPDCur:
                            print "entered Update cursot"
                            upd[2] = float(upd[2]) + 90
                            upd[6] = "te22st"
                            UPDCur.updateRow(upd)
        except Exception as e:
            print "Error with message: {0}".format(e)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_0-1730365975858.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118535iCE853A471DEEA71C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_0-1730365975858.png" alt="StuartMoore_0-1730365975858.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2024 09:13:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554124#M73126</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2024-10-31T09:13:06Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554126#M73127</link>
      <description>&lt;P&gt;but oddly if i reconcile my session and refresh the map it has updated the first one&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_1-1730366196484.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118536iC596F8DB9768E8D8/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_1-1730366196484.png" alt="StuartMoore_1-1730366196484.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;i'm thinking the issue is the arcfm session side&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2024 09:17:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554126#M73127</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2024-10-31T09:17:27Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554139#M73129</link>
      <description>&lt;P&gt;i change it to use the field calculator instead of the update cursor, it works but the downside is its significantly slower&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":pensive_face:"&gt;😔&lt;/span&gt;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;with arcpy.da.SearchCursor(fc, ["objectid", "FeatureID","Angle" ,"SHAPE@", "SHAPE@X", "SHAPE@Y","LEGACY_TABLE_NAME"],"objectid in (4011345,4011328,4011327,4011326,4011325,4011324,4011323,4011346)") as Cur:
    for row in Cur:

        # read in the data

        print row[0]

        SA_Filter = "OBJECTID = {0}".format(row[0])
        print SA_Filter
        try:
            arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
            annocount = arcpy.management.GetCount(fc)
            print "tmp count: {0}".format(annocount[0])
            if int(annocount[0]) == 1:
                print "selected one anno"

                arcpy.management.SelectLayerByLocation(fc, "INTERSECT", fc, "", "NEW_SELECTION")
                tmpcount = arcpy.management.GetCount(fc)
                print "tmp count: {0}".format(tmpcount[0])
                if int(tmpcount[0]) &amp;gt;= 2 and int(tmpcount[0]) &amp;lt; 74:
                    print "more than 1"
                    arcpy.management.SelectLayerByAttribute(fc, "NEW_SELECTION", SA_Filter)
                    arcpy.CalculateField_management(fc, "Angle", 
                                "!ANGLE!+90",
                                "PYTHON_9.3")
        except Exception as e:
            print "Error with message: {0}".format(e)&lt;/LI-CODE&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="StuartMoore_0-1730371627898.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/118540i40E92DA058065F6D/image-size/medium?v=v2&amp;amp;px=400" role="button" title="StuartMoore_0-1730371627898.png" alt="StuartMoore_0-1730371627898.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2024 10:48:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554139#M73129</guid>
      <dc:creator>StuartMoore</dc:creator>
      <dc:date>2024-10-31T10:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554220#M73130</link>
      <description>&lt;P&gt;&lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/159798"&gt;@StuartMoore&lt;/a&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I understand your frustration I just wrote a piece of code that was going to take 60+ days....&lt;BR /&gt;&lt;BR /&gt;It is weird that the OBJECTID exists but returns an invalid expression, it is almost like the fc variable is being rewritten to equal a different map layer and that why the expression fails&lt;/P&gt;</description>
      <pubDate>Thu, 31 Oct 2024 13:33:29 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1554220#M73130</guid>
      <dc:creator>BrandonMcAlister</dc:creator>
      <dc:date>2024-10-31T13:33:29Z</dc:date>
    </item>
    <item>
      <title>Re: update cursor fails to work</title>
      <link>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1556013#M73143</link>
      <description>&lt;P&gt;Nesting Cursors is generally pretty bad, but it can be useful when you want the spatial query to be done in SQL instead of iterating the whole dataset for each feature in python:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;import arcpy
from arcpy.da import SearchCursor, UpdateCursor, Editor
import os

def rotate_annotations(feature_class: os.PathLike, *, angle_field: str = 'Angle') -&amp;gt; None:
    field_names = ('OID@', 'SHAPE@', angle_field)
    
    with UpdateCursor(feature_class, field_names) as upd_cursor:
        for row in upd_cursor:
            oid, shape, angle = row
            intersecting_features = \
                [
                    feature
                    for feature in SearchCursor(feature_class, field_names, spatial_filter=shape)
                    if feature[0] != oid
                ]
            if intersecting_features:
                angle += 90.0
                upd_cursor.updateRow([oid, shape, angle])
    return

if __name__ == '__main__':
    
    fc = r"path\to\your\feature_class"
    wsp = arcpy.Describe(fc).workspace.catalogPath
    
    with Editor(wsp) as edit:
        try:
            rotate_annotations(fc)
        except Exception as e:
            print(e)
            edit.stopEditing(False)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also threw the operation in an Editor session, which should prevent locks. Also since the rotation code is now a function call, you can call it multiple times on the data if you want to continue rotating annotations that entered a collision after the initial rotation was applied.&lt;/P&gt;</description>
      <pubDate>Thu, 07 Nov 2024 14:12:23 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/update-cursor-fails-to-work/m-p/1556013#M73143</guid>
      <dc:creator>HaydenWelch</dc:creator>
      <dc:date>2024-11-07T14:12:23Z</dc:date>
    </item>
  </channel>
</rss>

