<?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: calculatefield formula in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366667#M28939</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ad 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Indeed, there was a problem with Python expression - when dividing two numbers without decimal part, the result is a number without decimal part. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That's why decimal part was cut and only 0 left.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;To correct this just multiply expression by &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;1.0&lt;/SPAN&gt;&lt;SPAN&gt; (I also corrected previous answer).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ad 2.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SHORT and LONG data types allows to store in a field only integer numbers (without decimal part). The difference is how big/small number can be.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FLOAT and DOUBLE dat types allows to store real numeric values (with decimal part). The difference is roughly how many digits can be stored. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It seem that when you created DOUBLE field with specific precision and scale, application recognised it also fits in FLOAT type which occupies less space.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can read about precision and scale for example in &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=982&amp;amp;t=75881" rel="nofollow" target="_blank"&gt;this old thread&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Wed, 27 Jun 2012 05:05:57 GMT</pubDate>
    <dc:creator>MarcinGasior</dc:creator>
    <dc:date>2012-06-27T05:05:57Z</dc:date>
    <item>
      <title>calculatefield formula</title>
      <link>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366664#M28936</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;System: ArcGIS 9.3&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Problem:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I want to calculate the ratio of cut area to all area but did not know how to make the correct expression.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly help and thank you.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;The following code has an error message:&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ExecuteError: ERROR 000539: Error running expression: cutarea_km/ allarea_km &amp;lt;type 'exceptions.NameError'&amp;gt;: name 'cutarea_km' is not defined&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="plain" name="code"&gt;##Script Name: Calculate area ratio ##Description: of polygons of a shapefile ##Created By: Elaine Kuo ##Date: 24/06/2012&amp;nbsp;&amp;nbsp; #Import standard library modules import arcgisscripting import os&amp;nbsp; #Create the Geoprocessor object gp = arcgisscripting.create(9.3)&amp;nbsp; #Set the workspace. gp.Workspace= "H:/temp/test"&amp;nbsp; #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp"&amp;nbsp; #Get a list of the featureclasses in the input folder fcs = gp.ListFeatureClasses()&amp;nbsp; # Loop through every item in the list that was just generated for fc in fcs:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object. &amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc) &amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG &amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "ratio", "double", 2,2)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Get a list of the fields in the featureclass &amp;nbsp;&amp;nbsp;&amp;nbsp; fields = gp.ListFields(fc, "C*", "String") &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; for field in fields:&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; # get areas of all and cut &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; allarea = "allarea_km" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; cutarea = "cutarea_km"&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "cutarea_km/ allarea_km" &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc, "ratio", expression, "PYTHON") &amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace. &amp;nbsp;&amp;nbsp;&amp;nbsp; OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)&amp;nbsp; &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddMessage(gp.GetMessages()) print gp.GetMessages() &lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 26 Jun 2012 09:41:45 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366664#M28936</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2012-06-26T09:41:45Z</dc:date>
    </item>
    <item>
      <title>Re: calculatefield formula</title>
      <link>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366665#M28937</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;You don't need to loop through fields as you don't use Field object later.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;When you know your fields names it's enough to put them in expression (for Python parser in Calculate field tool, field names should be enclosed in ! !).&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Here's how it can be done:&lt;/SPAN&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;...
for fc in fcs:
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type DOUBLE
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField_management(fc, "ratio", "DOUBLE")

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Build Python expression based on known fields - "cutarea_km" and "allarea_km"
&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "!cutarea_km! * 1.0 / !allarea_km!"

&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc, "ratio", expression, "PYTHON")
...&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:02:06 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366665#M28937</guid>
      <dc:creator>MarcinGasior</dc:creator>
      <dc:date>2021-12-11T17:02:06Z</dc:date>
    </item>
    <item>
      <title>Re: calculatefield formula</title>
      <link>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366666#M28938</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;Hello &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;I modified my code but the calculation result was not recorded in the field "ratio."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please kindly &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;1. advise any modification of the following code. (attached the shapefile for test)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;2. explain the difference between "double" and "short"&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;(gp.AddField (fc, "ratio", "double", 6,6) =&amp;gt; the field turned to be float)&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;(gp.AddField (fc, "ratio", "double", 2,3) =&amp;gt;&amp;nbsp; the field turned to be short)&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;##Script Name: Calculate area ratio
##Description: of polygons of a shapefile
##Created By: Elaine Kuo
##Date: 24/06/2012


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the workspace.
gp.Workspace= "H:/temp/test"

#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp"

#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()

# Loop through every item in the list that was just generated
for fc in fcs:

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Break out the name, no path or extension, using the describe object.
&amp;nbsp;&amp;nbsp;&amp;nbsp; desc = gp.describe(fc)
&amp;nbsp;&amp;nbsp;&amp;nbsp; featureName = desc.name

&amp;nbsp;&amp;nbsp;&amp;nbsp; # Add a field to this shapefile, of type LONG
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.AddField (fc, "ratio", "double", 6,6)&amp;nbsp;&amp;nbsp; 

&amp;nbsp;&amp;nbsp;&amp;nbsp; #&amp;nbsp;&amp;nbsp; Get a list of the fields in the featureclass
&amp;nbsp;&amp;nbsp;&amp;nbsp; #fields = gp.ListFields(fc, "C*", "String")
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; # Loop through every item in the list that was just generated 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #for field in fields:

&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.toolbox = "Data Management"

&amp;nbsp;&amp;nbsp;&amp;nbsp; #get areas of all and cut
&amp;nbsp;&amp;nbsp;&amp;nbsp; #allarea = "allarea_km"
&amp;nbsp;&amp;nbsp;&amp;nbsp; #cutarea = "cutarea_km"

&amp;nbsp;&amp;nbsp;&amp;nbsp; expression = "!cutarea_km!/ !allarea_km!"
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; gp.CalculateField_management(fc, "ratio", expression, "PYTHON")
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp; #Validate the new feature class name for the output workspace.
&amp;nbsp;&amp;nbsp;&amp;nbsp; OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sat, 11 Dec 2021 17:02:09 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366666#M28938</guid>
      <dc:creator>ElaineKuo</dc:creator>
      <dc:date>2021-12-11T17:02:09Z</dc:date>
    </item>
    <item>
      <title>Re: calculatefield formula</title>
      <link>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366667#M28939</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;SPAN&gt;ad 1.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Indeed, there was a problem with Python expression - when dividing two numbers without decimal part, the result is a number without decimal part. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;That's why decimal part was cut and only 0 left.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;To correct this just multiply expression by &lt;/SPAN&gt;&lt;SPAN style="font-family:Courier New;"&gt;1.0&lt;/SPAN&gt;&lt;SPAN&gt; (I also corrected previous answer).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;ad 2.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;SHORT and LONG data types allows to store in a field only integer numbers (without decimal part). The difference is how big/small number can be.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;FLOAT and DOUBLE dat types allows to store real numeric values (with decimal part). The difference is roughly how many digits can be stored. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;It seem that when you created DOUBLE field with specific precision and scale, application recognised it also fits in FLOAT type which occupies less space.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;You can read about precision and scale for example in &lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="http://forums.esri.com/Thread.asp?c=93&amp;amp;f=982&amp;amp;t=75881" rel="nofollow" target="_blank"&gt;this old thread&lt;/A&gt;&lt;SPAN&gt;.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 27 Jun 2012 05:05:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/calculatefield-formula/m-p/366667#M28939</guid>
      <dc:creator>MarcinGasior</dc:creator>
      <dc:date>2012-06-27T05:05:57Z</dc:date>
    </item>
  </channel>
</rss>

