copyfield based on Cxxxx

1177
7
Jump to solution
06-25-2013 03:10 PM
ElaineKuo
Occasional Contributor
System: Windows Vista and ArcGIS 9.3

Hello,

I have 100 shapefiles called geocXXXX.shp (geoc0782, geoc9838, and geoc6728 etc)

In each shapefile, there is a field call CXXXX (C0782, C9838, and C6728 etc).
The field property is string, length =6.

Now I would like to make a new field call CXXXXD (C0782D, C9838D, and C6728D etc).
The field property is long integer, length =9.

Please kindly advise how to modify the follow code and thank you.

##Script Name: Copy field ##Description: Copy field using CXXXX ##Created By: Elaine Kuo ##Date: 06/25/2013   #Import standard library modules import arcgisscripting import os  #Create the Geoprocessor object gp = arcgisscripting.create(9.3)  #Set the input workspace #GP.workspace = sys.argv[1] #Set the workspace. gp.Workspace= "H:/temp_D/test"  #Set the output workspace #outWorkspace = sys.argv[2] #Set the workspace. List all of the feature classes in the dataset outWorkspace= "H:/temp_D"  #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      #Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      #get file name     fcName, fcExt = os.path.splitext(fc)          #   Get a list of the fields in the featureclass     fields = gp.listFields(fc, "C*", "String")      # replace the strings you want to     fcName = fcName.replace("D","")     fcName = fcName + fcExt     fcName = outWorkspace + os.sep + fcName          #get file name     fcName, fcExt = os.path.splitext(fc)      fieldName = fcName + "D"  #build field name      #Add field to each SHP     gp.AddField_management(fc, fieldName, "LONG", 9, 3)
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
RDHarles
Occasional Contributor
I see this snippet in the code, but don't know what it is used for as it looks like you get the name from the os.path.split section:

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


I don't see anywhere else that you are using the desc or featureName variables.  If so, the describe is fairly heavy operation, especially within a for loop.

If it really isn't needed, would probably decrease your run time substatially if removed.

R_



Nor do these lines look necessary:

#Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      #get file name     fcName, fcExt = os.path.splitext(fc)         #Get a list of the fields in the featureclass     fields = gp.listFields(fc, "C*", "String")

View solution in original post

0 Kudos
7 Replies
RDHarles
Occasional Contributor
If the code in the shapefile name is the same as the code you want in your new field name, simply do this:

#Import standard library modules
import arcgisscripting, os


#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)


#Set the input workspace
gp.Workspace= "H:/temp_D/test"


#Get a list of the featureclasses in the input folder
fcs = gp.ListFeatureClasses()


#Loop through the fc's
for fc in fcs:


    # Get the numeric code from the shapefile name
    numCode = fc[4:8]


    # Build the field name
    fieldName = "C"+numCode+"D"    


    #Add field to each SHP
    print "Updating file "+fc+" with fieldname "+fieldName+"..."
    gp.AddField_management(fc, fieldName, "LONG", 9, 3)
0 Kudos
ElaineKuo
Occasional Contributor
Thank you.
The code worked wonderfully.

The complete code (create a field (CXXXX) and copy a number to it)
is
##Script Name: Copy field
##Description: Copy field using CXXXX
##Created By: Elaine Kuo
##Date: 06/25/2013


#Import standard library modules
import arcgisscripting
import os

#Create the Geoprocessor object
gp = arcgisscripting.create(9.3)

#Set the input workspace
#GP.workspace = sys.argv[1]
#Set the workspace.
gp.Workspace= "H:/temp_D/test"

#Set the output workspace
#outWorkspace = sys.argv[2]
#Set the workspace. List all of the feature classes in the dataset
outWorkspace= "H:/temp_D"

#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

    #Validate the new feature class name for the output workspace.
    OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)

    #get file name
    fcName, fcExt = os.path.splitext(fc) 

  
    #   Get a list of the fields in the featureclass
    fields = gp.listFields(fc, "C*", "String")

     # Get the numeric code from the shapefile name
    numCode = fc[4:8]


    # Build the field name
    fieldName = "C"+numCode+"D"    


    #Add field to each SHP
    print "Updating file "+fc+" with fieldname "+fieldName+"..."
    gp.AddField_management(fc, fieldName, "LONG", 9)


     # Make temporary featureclasses
    gp.MakeFeatureLayer(fc,"lyr")
    
    #   Get a list of the fields in the featureclass
    fields = gp.ListFields("lyr", "C*", "String")
    
    # 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\" = 'R'" % field.Name
        gp.SelectLayerByAttribute("lyr", "ADD_TO_SELECTION", query)

        
        # copy values in existing fields to the new field 
        gp.CalculateField("lyr", fieldName, "1", "PYTHON_9.3")



gp.AddMessage(gp.GetMessages())
print gp.GetMessages()
   
0 Kudos
RDHarles
Occasional Contributor
Glad I could help.
R.D.
0 Kudos
RhettZufelt
MVP Frequent Contributor
I see this snippet in the code, but don't know what it is used for as it looks like you get the name from the os.path.split section:

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



I don't see anywhere else that you are using the desc or featureName variables.  If so, the describe is fairly heavy operation, especially within a for loop.

If it really isn't needed, would probably decrease your run time substatially if removed.

R_
0 Kudos
RDHarles
Occasional Contributor
I see this snippet in the code, but don't know what it is used for as it looks like you get the name from the os.path.split section:

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


I don't see anywhere else that you are using the desc or featureName variables.  If so, the describe is fairly heavy operation, especially within a for loop.

If it really isn't needed, would probably decrease your run time substatially if removed.

R_



Nor do these lines look necessary:

#Validate the new feature class name for the output workspace.     OutFeatureClass = outWorkspace + os.sep + gp.ValidateTableName(fc,outWorkspace)      #get file name     fcName, fcExt = os.path.splitext(fc)         #Get a list of the fields in the featureclass     fields = gp.listFields(fc, "C*", "String")
0 Kudos
RhettZufelt
MVP Frequent Contributor
Didn't even recognize that.  Guess I really wasn't trying to pick apart the code, just that I have been "bitten" by describe statements within a loop before, so that really stood out to me.

R_
0 Kudos
RDHarles
Occasional Contributor
Didn't even recognize that.  Guess I really wasn't trying to pick apart the code, just that I have been "bitten" by describe statements within a loop before, so that really stood out to me.

R_


Good to know about the Describe, didn't know it was so "computer intensive".
0 Kudos