<?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 difficulty calculating field in Python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1541#M177</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm having difficulty calculating a field in my Python script.&amp;nbsp; I have a bunch of polylines and I want to get the azimuth for each line in degrees.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is where my code crashes (the math module has already been imported): &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;fieldName3 = "Azimuth"
expression2 = "180+math.atan2(( !Shape.firstpoint.X! - !Shape.lastpoint.X! ),( !Shape.firstpoint.Y! - !Shape.lastpoint.Y! ) ) * (180 / math.pi )"
arcpy.CalculateField_management(outFeatureClass, "Azimuth", expression2, "PYTHON")&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get an arcgisscripting.ExecuteError: ERROR 000539: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The confusing thing is I can paste the same code in the field calculator and it works fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance for you help,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Fri, 02 May 2014 18:54:32 GMT</pubDate>
    <dc:creator>JasonTrook</dc:creator>
    <dc:date>2014-05-02T18:54:32Z</dc:date>
    <item>
      <title>difficulty calculating field in Python</title>
      <link>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1541#M177</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hi All,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I'm having difficulty calculating a field in my Python script.&amp;nbsp; I have a bunch of polylines and I want to get the azimuth for each line in degrees.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;This is where my code crashes (the math module has already been imported): &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;fieldName3 = "Azimuth"
expression2 = "180+math.atan2(( !Shape.firstpoint.X! - !Shape.lastpoint.X! ),( !Shape.firstpoint.Y! - !Shape.lastpoint.Y! ) ) * (180 / math.pi )"
arcpy.CalculateField_management(outFeatureClass, "Azimuth", expression2, "PYTHON")&amp;nbsp; &lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I get an arcgisscripting.ExecuteError: ERROR 000539: &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The confusing thing is I can paste the same code in the field calculator and it works fine.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks in advance for you help,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Jason&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 02 May 2014 18:54:32 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1541#M177</guid>
      <dc:creator>JasonTrook</dc:creator>
      <dc:date>2014-05-02T18:54:32Z</dc:date>
    </item>
    <item>
      <title>Re: difficulty calculating field in Python</title>
      <link>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1542#M178</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I suggest you do not use the CalculateField tool in a Python script.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It is much better to use an UpdateCursor which is just as fast or faster because CalculateField wraps a cursor around the expression anyway.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can also handle unexpected input such as null values, put in conditional tests and generally make the process more readable.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;But answering your question, it looks like you have the exclamation marks around the whole expression, not just the field name, so I am very surprised that it works interactively at all.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;fieldName3 = "Azimuth"
expression2 = "180+math.atan2(( !Shape!.firstpoint.X - !Shape!.lastpoint.X ),( !Shape!.firstpoint.Y - !Shape!.lastpoint.Y ) ) * (180 / math.pi )"
arcpy.CalculateField_management(outFeatureClass, "Azimuth", expression2, "PYTHON")&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:04:59 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1542#M178</guid>
      <dc:creator>KimOllivier</dc:creator>
      <dc:date>2021-12-10T20:04:59Z</dc:date>
    </item>
    <item>
      <title>Re: difficulty calculating field in Python</title>
      <link>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1543#M179</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Here's one method I put together for this.&amp;nbsp; I'm not sure if there's a built in method in arcpy's Geometry object to get bearing or not.&amp;nbsp; In my case, I pulled all the coordinates out of the geometry object into a list.&amp;nbsp; Then iterated through the list, using a little trigonometry to get the bearings.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;My formulate was adapted from here:&lt;/SPAN&gt;&lt;BR /&gt;&lt;A href="http://www.movable-type.co.uk/scripts/latlong.html" rel="nofollow noopener noreferrer" target="_blank"&gt;http://www.movable-type.co.uk/scripts/latlong.html&lt;/A&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;def getBearing(latFrom,lonFrom,latDest,lonDest):
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Returns the initial bearing in degrees from north following the great
&amp;nbsp;&amp;nbsp;&amp;nbsp; # circle path to the destination coordinates.
&amp;nbsp;&amp;nbsp;&amp;nbsp; lat1 = math.radians(float(latFrom))
&amp;nbsp;&amp;nbsp;&amp;nbsp; lat2 = math.radians(float(latDest))

&amp;nbsp;&amp;nbsp;&amp;nbsp; dLon = math.radians(float(lonDest)-float(lonFrom))

&amp;nbsp;&amp;nbsp;&amp;nbsp; y = math.sin(dLon) * math.cos(lat2)

&amp;nbsp;&amp;nbsp;&amp;nbsp; x = math.cos(lat1) * math.sin(lat2) - \
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; math.sin(lat1) * math.cos(lat2) * math.cos(dLon)

&amp;nbsp;&amp;nbsp;&amp;nbsp; brng = math.atan2(y, x)

&amp;nbsp;&amp;nbsp;&amp;nbsp; return (math.degrees(brng) + 360) % 360;


line_coordinates = []
# Get your geometry object
geoms = [row[0] for row in arcpy.da.SearchCursor(yourFeature_Class,["SHAPE@"])]

# Iterate through all the points in your geomtry, appending them to the "line_coordinates" list
for geom in geoms:

&amp;nbsp;&amp;nbsp;&amp;nbsp; partCount = geom.partCount

&amp;nbsp;&amp;nbsp;&amp;nbsp; for n in xrange(partCount):
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; part = geom.getPart(n)
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for point in part:
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; line_coordinates.append((point.Y, point.X))

# Iterate through the "line_coordinates" list, printing the bearings
n = 1
while n &amp;lt; len(line_coordinates):
&amp;nbsp;&amp;nbsp;&amp;nbsp; print "%0.4f" % getBearing(line_coordinates[n-1][0],line_coordinates[n-1][1],line_coordinates&lt;N&gt;[0],line_coordinates&lt;N&gt;[1])
&amp;nbsp;&amp;nbsp;&amp;nbsp; n += 1&lt;/N&gt;&lt;/N&gt;&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;This method may be a little roundabout, but it's gotten it done for me in the past.&amp;nbsp; Worth noting though, that the bearing is from the &lt;/SPAN&gt;&lt;STRONG&gt;starting coordinate&lt;/STRONG&gt;&lt;SPAN&gt;, as bearing may change along a longer great circle.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Dec 2021 20:05:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/difficulty-calculating-field-in-python/m-p/1543#M179</guid>
      <dc:creator>MattEiben</dc:creator>
      <dc:date>2021-12-10T20:05:01Z</dc:date>
    </item>
  </channel>
</rss>

