calculatefield formula

1774
3
Jump to solution
06-26-2012 02:41 AM
ElaineKuo
Occasional Contributor
System: ArcGIS 9.3

Problem:
I want to calculate the ratio of cut area to all area but did not know how to make the correct expression.
Please kindly help and thank you.

The following code has an error message:
ExecuteError: ERROR 000539: Error running expression: cutarea_km/ allarea_km <type 'exceptions.NameError'>: name 'cutarea_km' is not defined

##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:      # Break out the name, no path or extension, using the describe object.     desc = gp.describe(fc)     featureName = desc.name      # Add a field to this shapefile, of type LONG     gp.AddField (fc, "ratio", "double", 2,2)         #   Get a list of the fields in the featureclass     fields = gp.ListFields(fc, "C*", "String")               # Loop through every item in the list that was just generated      for field in fields:          gp.toolbox = "Data Management"          # get areas of all and cut         allarea = "allarea_km"         cutarea = "cutarea_km"          expression = "cutarea_km/ allarea_km"                  gp.CalculateField_management(fc, "ratio", expression, "PYTHON")                  #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)       gp.AddMessage(gp.GetMessages()) print gp.GetMessages() 
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
MarcinGasior
Occasional Contributor III
ad 1.
Indeed, there was a problem with Python expression - when dividing two numbers without decimal part, the result is a number without decimal part.
That's why decimal part was cut and only 0 left.
To correct this just multiply expression by 1.0 (I also corrected previous answer).

ad 2.
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.
FLOAT and DOUBLE dat types allows to store real numeric values (with decimal part). The difference is roughly how many digits can be stored.
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.

You can read about precision and scale for example in this old thread.

View solution in original post

0 Kudos
3 Replies
MarcinGasior
Occasional Contributor III
You don't need to loop through fields as you don't use Field object later.
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 ! !).
Here's how it can be done:
...
for fc in fcs:
    # Add a field to this shapefile, of type DOUBLE
    gp.AddField_management(fc, "ratio", "DOUBLE")

    # Build Python expression based on known fields - "cutarea_km" and "allarea_km"
    expression = "!cutarea_km! * 1.0 / !allarea_km!"

    gp.CalculateField_management(fc, "ratio", expression, "PYTHON")
...
0 Kudos
ElaineKuo
Occasional Contributor
Hello

I modified my code but the calculation result was not recorded in the field "ratio."

Please kindly
1. advise any modification of the following code. (attached the shapefile for test)
2. explain the difference between "double" and "short"
Thank you

(gp.AddField (fc, "ratio", "double", 6,6) => the field turned to be float)
(gp.AddField (fc, "ratio", "double", 2,3) =>  the field turned to be short)

##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:

    # Break out the name, no path or extension, using the describe object.
    desc = gp.describe(fc)
    featureName = desc.name

    # Add a field to this shapefile, of type LONG
    gp.AddField (fc, "ratio", "double", 6,6)   

    #   Get a list of the fields in the featureclass
    #fields = gp.ListFields(fc, "C*", "String")
    
    
    # Loop through every item in the list that was just generated 
    #for field in fields:

    gp.toolbox = "Data Management"

    #get areas of all and cut
    #allarea = "allarea_km"
    #cutarea = "cutarea_km"

    expression = "!cutarea_km!/ !allarea_km!"
        
    gp.CalculateField_management(fc, "ratio", expression, "PYTHON")
      
    #Validate the new feature class name for the output workspace.
    OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)

gp.AddMessage(gp.GetMessages())
print gp.GetMessages()

0 Kudos
MarcinGasior
Occasional Contributor III
ad 1.
Indeed, there was a problem with Python expression - when dividing two numbers without decimal part, the result is a number without decimal part.
That's why decimal part was cut and only 0 left.
To correct this just multiply expression by 1.0 (I also corrected previous answer).

ad 2.
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.
FLOAT and DOUBLE dat types allows to store real numeric values (with decimal part). The difference is roughly how many digits can be stored.
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.

You can read about precision and scale for example in this old thread.
0 Kudos