##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)
Solved! Go to Solution.
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_
#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")
#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)
##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()
# Break out the name, no path or extension, using the describe object. desc = gp.describe(fc) featureName = desc.name
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_
#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")
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_