<?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: Calc fields in python in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218781#M16833</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;you were right! how obvious, guess thats why you shouldn't script late at night! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks a lot.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 31 Oct 2011 00:34:47 GMT</pubDate>
    <dc:creator>SylviaNiderla</dc:creator>
    <dc:date>2011-10-31T00:34:47Z</dc:date>
    <item>
      <title>Calc fields in python</title>
      <link>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218779#M16831</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Can someone tell me why these two calcs don't work? the "speed" one completes, but doesn't do anything. The one that calculates distance based on shapelength errors. Not sure if you can script calculate length based on shapelength in version 10.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;

# Description: Calculates speed and distance
#arcmap 10

 
# Import system modules
import arcpy
from arcpy import env
 
# Get user-supplied input
InputFC = arcpy.GetParameterAsText(0)

try: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate Speed based on time and distance
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(InputFC, "Speed", "[Time] / [Distance]", "VB") 

except:
 errMsg = arcpy.GetMessages(2)
 arcpy.AddError("Unexpected error: cannot calculate Speed" + errMsg)

try: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate Speed based on time and distance
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(InputFC, "Distance", "float(!SHAPE.LENGTH@kilometers)", "PYTHON")

except:
 errMsg = arcpy.GetMessages(2)
 arcpy.AddError("Unexpected error: cannot calculate Distance" + errMsg)&lt;/PRE&gt;&lt;DIV style="display:none;"&gt; &lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 30 Oct 2011 15:22:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218779#M16831</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2011-10-30T15:22:12Z</dc:date>
    </item>
    <item>
      <title>Re: Calc fields in python</title>
      <link>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218780#M16832</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;Can someone tell me why these two calcs don't work? the "speed" one completes, but doesn't do anything. The one that calculates distance based on shapelength errors. Not sure if you can script calculate length based on shapelength in version 10.&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;

# Description: Calculates speed and distance
#arcmap 10

 
# Import system modules
import arcpy
from arcpy import env
 
# Get user-supplied input
InputFC = arcpy.GetParameterAsText(0)

try: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate Speed based on time and distance
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(InputFC, "Speed", "[Time] / [Distance]", "VB") 

except:
 errMsg = arcpy.GetMessages(2)
 arcpy.AddError("Unexpected error: cannot calculate Speed" + errMsg)

try: 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Calculate Speed based on time and distance
&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(InputFC, "Distance", "float(!SHAPE.LENGTH@kilometers)", "PYTHON")

except:
 errMsg = arcpy.GetMessages(2)
 arcpy.AddError("Unexpected error: cannot calculate Distance" + errMsg)&lt;/PRE&gt;&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;The first error I will point out is that you are missing an exclamation point on your second calculation.&amp;nbsp; It should be:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; arcpy.CalculateField_management(InputFC, "Distance", "float(!SHAPE.LENGTH@kilometers&lt;/SPAN&gt;&lt;STRONG style=": ; font-style: italic; text-decoration: underline;"&gt;!&lt;/STRONG&gt;&lt;SPAN&gt;)", "PYTHON")&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For the other calculation, if Distance is 0 or Null, the calculation will fail (division by 0 or Null is not allowed and the geoprocessor skips over those records).&amp;nbsp; I would think you need to reverse the calculation order, since you should want the Distance assigned before you attempt to calculate Speed.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Using the Length field directly is touchy especially if your data is stored in SDE, but it sometimes works.&amp;nbsp; However, if you apply the fixes above the calculations you have should work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 10:40:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218780#M16832</guid>
      <dc:creator>RichardFairhurst</dc:creator>
      <dc:date>2021-12-11T10:40:48Z</dc:date>
    </item>
    <item>
      <title>Re: Calc fields in python</title>
      <link>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218781#M16833</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;you were right! how obvious, guess thats why you shouldn't script late at night! &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;thanks a lot.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 31 Oct 2011 00:34:47 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calc-fields-in-python/m-p/218781#M16833</guid>
      <dc:creator>SylviaNiderla</dc:creator>
      <dc:date>2011-10-31T00:34:47Z</dc:date>
    </item>
  </channel>
</rss>

