##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 07/18/2015 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test" #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, "All", "Long", 10,10) # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr", "D*", "Long") # Loop through every item in the list that was just generated for field in fields: gp.toolbox = "Data Management" # Select records to be copied (C*, i.e. C7658) query = "\"%s\" = 1" % field.Name gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query) # copy values in existing fields to the new field gp.CalculateField("lyr", "All", "1", "PYTHON_9.3") #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
Solved! Go to Solution.
##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 07/18/2015 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test" #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, "All", "Long", 10,10) # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr", "D*", "Long") # Loop through every item in the list that was just generated for field in fields: # Select records to use in a field calculation (C*, i.e. C7658) query = "\"%s\" = 1" % field.Name gp.SelectLayerByAttribute_management("lyr", "ADD_TO_SELECTION", query) # code the 'All' field of the net selected set with a value of 1 gp.CalculateField_management("lyr", "All", "1", "PYTHON_9.3") gp.SelectLayerByAttribute_management("lyr", "SWITCH_SELECTION") gp.CalculateField_management("lyr", "All", "0", "PYTHON_9.3") #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 07/18/2015 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test" #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, "All", "Long", 10,10) # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr", "D*", "Long") # Loop through every item in the list that was just generated for field in fields: gp.toolbox = "Data Management" # Select records to be copied (C*, i.e. C7658) query = "\"%s\" = 1" % field.Name gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query) # copy values in existing fields to the new field gp.CalculateField("lyr", "All", "1", "PYTHON_9.3") #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()
You forget the 2nd parameter in Calculate Field names the field to calculate values for. You have named this "ALL"; so what your inner 'for' loop does is add to an already selected set on your feature layer (creates a new selection on the 1st iteration), then for whatever species ID was found to be populated (1), "ALL" is then calculated as 1.
The subtlety I think you're missing here is your field calculation doesn't do a sum of any kind. It doesn't matter if performing the calculation inside or outside the loop (except it is more efficient to do it outside, after the selection is 'compiled', for lack of a better word).
An example with a single row:
speciesA, speciesB, speciesC, ALL
1, 0, 1, 1
Of course, the value of ALL for this row is 1, correct? (This is not a sum.)
So you can see, if you ran the calculation within your 'for' loop, you'd be calculating the value to 1 twice, once for speciesA and once for speciesC. (which of course is not necessary)
Wayne
##Script Name: calculate sum ##Description: calculate sum of merged range sizes of a taxonomy of migratory birds ##Created By: Elaine Kuo ##Date: 07/18/2015 #Import standard library modules import arcgisscripting import os #Create the Geoprocessor object gp = arcgisscripting.create(9.3) #Set the workspace. gp.Workspace= "H:/temp_D/test_1" #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D/test" #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, "All", "Long", 10,10) # Make temporary featureclasses gp.MakeFeatureLayer(fc,"lyr") # Get a list of the fields in the featureclass fields = gp.ListFields("lyr", "D*", "Long") # Loop through every item in the list that was just generated for field in fields: # Select records to use in a field calculation (C*, i.e. C7658) query = "\"%s\" = 1" % field.Name gp.SelectLayerByAttribute_management("lyr", "ADD_TO_SELECTION", query) # code the 'All' field of the net selected set with a value of 1 gp.CalculateField_management("lyr", "All", "1", "PYTHON_9.3") gp.SelectLayerByAttribute_management("lyr", "SWITCH_SELECTION") gp.CalculateField_management("lyr", "All", "0", "PYTHON_9.3") #Validate the new feature class name for the output workspace. #OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace) # clear memory of layers gp.Delete("lyr") gp.AddMessage(gp.GetMessages()) print gp.GetMessages()