Quantcast
Channel: Forums - Python
Viewing all articles
Browse latest Browse all 2485

Calculate Field loop

$
0
0
Hello

System Windows Vista ArcGIS 9.3

Now I have a shape file.
Its attribute table consisted of fields of GridCell ID (GID) and
species ID starting with D.
(Dxxxx (such D8729, D6745, D2765))
The rows are GridCell ID (0-4800).
Each cell in the attribute table has either 1 and 0.

I would like to create a new field called All.
In the field All (Long 9, 9), the cell would should be assigned 1
when the cell of Dxxxx is 1. Otherwise, the cell would be 0.

The code can run the requirement above for one field.
Please kindly advise how to add the loop process for the multiple fields of Dxxxx
Thank you in advance.

the code

Code:

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


Viewing all articles
Browse latest Browse all 2485

Trending Articles