<?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: Error when trying to calculate a field in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753860#M58158</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I assume it is this line you are having the problem with?&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")&lt;/PRE&gt;&lt;BR /&gt;Have you tried doing that manually in ArcMap to see if your results vary?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, that's the line. I was successful running the entire script using the same test data set before converting to a script tool.&amp;nbsp; Seems to be behaving differently in the script tool. Strange that if the latTemp filed is already in the attribute table it will work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Mon, 25 Feb 2013 17:12:12 GMT</pubDate>
    <dc:creator>MichaelMiller2</dc:creator>
    <dc:date>2013-02-25T17:12:12Z</dc:date>
    <item>
      <title>Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753858#M58156</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Anyone tell me why this code is returning this error?&amp;nbsp; It creates the latTemp field, and I can see it after it crashes, but it will not calculate it.&amp;nbsp;&amp;nbsp; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ERROR:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;&amp;lt;class 'arcgisscripting.ExecuteError'&amp;gt;: Failed to execute. Parameters are not valid. ERROR 000728: Field latSTR does not exist within table Failed to execute (CalculateField).&amp;nbsp; Failed to execute (ConvertDMS2DD). Failed at Mon Feb 25 11:25:18 2013 (Elapsed Time: 9.00 seconds)&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;CODE:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;import arcpy, sys, os arcpy.env.overwriteOutput = 1&amp;nbsp; # select the feature class&amp;nbsp; fc = arcpy.GetParameter(0) # select the latitude field to convert latField2 = arcpy.GetParameter(1) latField = "%s%s%s" % ('!', latField2, '!') arcpy.AddMessage("latField2 = " + str(latField2) +"&amp;nbsp;&amp;nbsp; latField = " +latField) lonField2 = arcpy.GetParameter(2) lonField = "%s%s%s" % ('!', lonField2, '!') arcpy.AddMessage("lonField2 = " + str(lonField2) +"&amp;nbsp; lonField = " +lonField) # set lattitude output field latFieldDD = "latDD" # set longitude output field lonFieldDD = "lonDD" # set temp lattitude field, used to store lat field as string used in function latTemp = "latSTR" # set temp longitude field, used to store lon field as string used in function lonTemp = "lonSTR"&amp;nbsp; latExp = "lat(%s%s%s)" % ('!',latTemp,'!') codeblockLAT = """def lat(dms): &amp;nbsp;&amp;nbsp;&amp;nbsp; latdeg = float(dms[0:2]) &amp;nbsp;&amp;nbsp;&amp;nbsp; latmin = float(dms[2:4]) &amp;nbsp;&amp;nbsp;&amp;nbsp; latsec = float(dms[4:6]) &amp;nbsp;&amp;nbsp;&amp;nbsp; latdd = latdeg + (latmin / 60) + (latsec / 3600) &amp;nbsp;&amp;nbsp;&amp;nbsp; return latdd""" lonExp = "lon(%s%s%s)" % ('!',lonTemp,'!') codeblockLON = """def lon(dms): &amp;nbsp;&amp;nbsp;&amp;nbsp; londeg = float(dms[0:3]) &amp;nbsp;&amp;nbsp;&amp;nbsp; lonmin = float(dms[3:5]) &amp;nbsp;&amp;nbsp;&amp;nbsp; lonsec = float(dms[5:7]) &amp;nbsp;&amp;nbsp;&amp;nbsp; londd = (londeg + (lonmin / 60) + (lonsec / 3600)) * -1 &amp;nbsp;&amp;nbsp;&amp;nbsp; return londd"""&amp;nbsp; #copy the lat numeric to a string arcpy.AddField_management(fc, latTemp, "TEXT", "", "", 16) arcpy.AddField_management(fc, latFieldDD, "FLOAT", 16, 6, 16) arcpy.AddMessage("added lat fields: %s and %s" % (latTemp, latFieldDD))&amp;nbsp; #copy the lon numeric to a string arcpy.AddField_management(fc, lonTemp, "TEXT", "", "", 16) arcpy.AddField_management(fc, lonFieldDD, "FLOAT", 16, 6, 16) arcpy.AddMessage("added lon fields: %s and %s" % (lonTemp, lonFieldDD))&amp;nbsp; # do calculations arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON") arcpy.CalculateField_management(fc, latFieldDD, latExp, "PYTHON", codeblockLAT) arcpy.DeleteField_management(fc, latTemp)&amp;nbsp; arcpy.CalculateField_management(fc, lonTemp, lonField, "PYTHON") arcpy.CalculateField_management(fc, lonFieldDD, lonExp, "PYTHON", codeblockLON) arcpy.DeleteField_management(fc, lonTemp)&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 16:32:25 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753858#M58156</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2013-02-25T16:32:25Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753859#M58157</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;I assume it is this line you are having the problem with?&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")&lt;/PRE&gt;&lt;BR /&gt;&lt;SPAN&gt;Have you tried doing that manually in ArcMap to see if your results vary?&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 17:01:33 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753859#M58157</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-25T17:01:33Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753860#M58158</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I assume it is this line you are having the problem with?&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.CalculateField_management(fc, latTemp, latField, "PYTHON")&lt;/PRE&gt;&lt;BR /&gt;Have you tried doing that manually in ArcMap to see if your results vary?&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, that's the line. I was successful running the entire script using the same test data set before converting to a script tool.&amp;nbsp; Seems to be behaving differently in the script tool. Strange that if the latTemp filed is already in the attribute table it will work.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 17:12:12 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753860#M58158</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2013-02-25T17:12:12Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753861#M58159</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;What data types are you passing as parameters? You may want to consider using astext instead of parameter.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE __default_attr="plain" __jive_macro_name="code" class="jive_macro_code jive_text_macro"&gt;arcpy.GetParameterAsText()&lt;/PRE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I'd wager your problem as more to do with your latField variable than your latTemp variable.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 17:18:52 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753861#M58159</guid>
      <dc:creator>MathewCoyle</dc:creator>
      <dc:date>2013-02-25T17:18:52Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753862#M58160</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;I was successful running the entire script using the same test data set before converting to a script tool.&amp;nbsp; Seems to be behaving differently in the script tool. &lt;/BLOCKQUOTE&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You are using GetParameter(), which returns an arcpy Field object, not a field name -- if you set it up as a script tool. From the command line, or IDLE, etc., the parameter is always a string, but from a toolbox, ArcGIS can pass the argument as a bona-fide arcpy object.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;If this is a Field parameter you should use GetParameterAsText() if you want the field &lt;/SPAN&gt;&lt;SPAN style="font-style:italic;"&gt;name&lt;/SPAN&gt;&lt;SPAN&gt; as text - for example, for use in a Calculate Field expression. Python's&amp;nbsp; sys.argv and arcpy.GetParameterAsText are fairly equivalent, although I am pretty sure GetParameterAsText can handle a longer string. (In code examples, you usually see GetParameterAsText because this allows the script to used easily inside or outside ArcGIS.)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Note, there is a new tool in 10x that does DMS to DD conversion, at least many of the conversions you'd want: &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://resources.arcgis.com/en/help/main/10.1/0017/0017000000tw000000.htm" rel="nofollow" target="_blank"&gt;ConvertCoordinateNotation_management&lt;/A&gt;&lt;SPAN&gt;. (I had been asking for this tool since the 1980's!)&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 18:18:22 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753862#M58160</guid>
      <dc:creator>curtvprice</dc:creator>
      <dc:date>2013-02-25T18:18:22Z</dc:date>
    </item>
    <item>
      <title>Re: Error when trying to calculate a field</title>
      <link>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753863#M58161</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;You are using GetParameter(), which returns an arcpy Field object, not a field name -- if you set it up as a script tool. From the command line, or IDLE, etc., the parameter is always a string, but from a toolbox, ArcGIS can pass the argument as a bona-fide arcpy object.&lt;BR /&gt;&lt;BR /&gt;If this is a Field parameter you should use GetParameterAsText() if you want the field &lt;SPAN style="font-style:italic;"&gt;name&lt;/SPAN&gt; as text - for example, for use in a Calculate Field expression. Python's&amp;nbsp; sys.argv and arcpy.GetParameterAsText are fairly equivalent, although I am pretty sure GetParameterAsText can handle a longer string. (In code examples, you usually see GetParameterAsText because this allows the script to used easily inside or outside ArcGIS.)&lt;BR /&gt;&lt;BR /&gt;Note, there is a new tool in 10x that does DMS to DD conversion, at least many of the conversions you'd want: &lt;A href="http://resources.arcgis.com/en/help/main/10.1/0017/0017000000tw000000.htm"&gt;ConvertCoordinateNotation_management&lt;/A&gt;. (I had been asking for this tool since the 1980's!)&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;SPAN&gt; &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BLOCKQUOTE class="jive-quote"&gt;What data types are you passing as parameters? You may want to consider using astext instead of parameter.&lt;BR /&gt;&lt;BR /&gt;Code:&lt;BR /&gt;arcpy.GetParameterAsText()&lt;BR /&gt;I'd wager your problem as more to do with your latField variable than your latTemp variable.&lt;/BLOCKQUOTE&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Matt and Curtis,&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you very much for pointing this out to me. Yes, it now works as expected.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thanks again.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Also, Curtis thanks for pointing out the new tool to do this and much&amp;nbsp; much more.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 25 Feb 2013 18:49:55 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/error-when-trying-to-calculate-a-field/m-p/753863#M58161</guid>
      <dc:creator>MichaelMiller2</dc:creator>
      <dc:date>2013-02-25T18:49:55Z</dc:date>
    </item>
  </channel>
</rss>

