AnsweredAssumed Answered

calculatefield formula

Question asked by helenak on Jun 26, 2012
Latest reply on Jun 26, 2012 by m.gasior
System: ArcGIS 9.3

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 =      # 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()