##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()
Solved! Go to Solution.
... 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") ...
##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()