#++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ #----------------------------------------------------------------------------------------------------------- #---------------------------------------------------------------------------------------------------- #Script Name:batch_delete_fields.py #Version: 1.0 #Release Date: 04/07/2005 #---------------------------------------------------------------------------------------------------- """ Author:Leonidas Liakos Email:leonidas_liakos@yahoo.gr URL:http://www.geocities.com/leonidasliakos City:Paramythia, Greece """ #---------------------------------------------------------------------------------------------------- #DESCRIPTION: # Batch deleting fields in featureclasses residing in one or more workspaces # Based on the Usage of DeleteField Command: DeleteField # You can: # a)Define one or more featureclasses residing in different workspaces # b)Define your file containing the names of the fields to be deleted # d)Choose the delimiter of the fields in your file # c)Choose "ON" or "OFF" Case Sensitive Option # e)Define an optional report file # #Usage: BatchDeleteFields {Report_File} # #---------------------------------------------------------------------------------------------------- #----------------------------------------------------------------------------------------------------------- #++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ # Importer les modules import win32com.client, sys, os, string, time begin = time.time() # Creation de l'objet geoprocesseur gp=win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1") gp.overwriteoutput = 1 #Parametres requis shps= sys.argv[1]#chaine de valeurs multiples avec les jeux de classes Lshp=shps.split(";")#creation d'une liste avec les jeux de classes(fullpath) filepath=sys.argv[2]#Fichier contenant les champs à surpprimerr delimiter = sys.argv[3]#Le separateur de champs dans le fichier defini casesensitive=sys.argv[4]#The Case_sensitive option reportpath=sys.argv[5]#Le chemin et le nom du fichier de rapport gp.addmessage(str(shps)) gp.addmessage(str(Lshp)) #fonctions def splitter(delimiter): if delimiter=="TAB": return "\t" if delimiter=="SPACE": return " " if delimiter=="COMMA (,)": return "," if delimiter=="LINEFEED": return "\n" if delimiter=="SEMICOLON (;)": return ";" if delimiter=="TILDE (~)": return "~" if delimiter=="POUND (#)": return "#" if delimiter=="PERIOD (.)": return "." if delimiter=="COLON (:)": return ":" if delimiter=="QUOTE (')": return "\'" if delimiter=="BANG (!)": return "!" if delimiter=="PIPE (|)": return "|" if reportpath=='#': reportfilemsg = 'Defined Report File: NO' def message(reportmsg): # - impression du message gp.addmessage(reportmsg) def warning(warningmsg): gp.AddWarning(warningmsg) def error(errormsg): gp.AddError(errormsg) else: reportfilemsg = 'Defined Report File: '+reportpath reportfile=open(reportpath,"w") def message(reportmsg): # - impression du message gp.addmessage(reportmsg);reportfile.write(reportmsg+"\n") def warning(warningmsg): gp.AddWarning(warningmsg);reportfile.write(warningmsg+"\n") def error(errormsg): gp.AddError(errormsg);reportfile.write(errormsg+"\n") def deletefield(param1,param2,param3,param4): message(param1) inputs ='' for item in param2:#Genere une chaine de valeurs multiples message("\t\t"+item) inputs = inputs+item+";" delfields=inputs[:-1] gp.deletefield_management(param3,delfields)#supprime les champs dans le jeu de classe message(param4) #Ouverture du fichier contenant les champs devant etre supprimes ofile =open(filepath,"r") rfile=ofile.read() splitrfile=rfile.split(splitter(delimiter))#liste contenant les champs dans le fichier texte ofile.close() try: #Impression du debut du rapport Date=time.asctime(time.localtime(time.time( ))) warningmsg="\t\t***************GENERATE REPORT***************"+"\n\n"+"\t\tDate:"+Date warning(warningmsg+"\n") #gp.AddWarning(Warning+"\n");reportfile.write(Warning+"\n"+"\n") warning("Case-Sensitive Option is:"+casesensitive+"\n") gp.AddWarning(reportfilemsg+"\n") shpcounter = 0 #compteur pour shp #Pour chaque jeu de classe dans l'espace de travail: for ishp in Lshp: isplit=os.path.split(ishp) workspace=isplit[0]#Obtient l espace de travail de l actuel jeu de classe #Fix path and filename.When geoprocessor finds a space in path, adds the symbol (') in the beginning and end of path. #A path like C:\\temp leonidas\\shp_1.shp will become 'C:\\temp leonidas\\shp_1.shp' and the script will return as path of workspace 'C:\\temp leonidas and featureclass shp_1.shp'.Wrong!!! #Remove the (') from the beginning of path if workspace[0]=='\'': workspace=workspace[1:] #Remove the (') from the end of filename shp=isplit[1]#get the name of the current featureclass if shp[len(shp)-1]=='\'': shp=shp[0:((len(shp))-1)] gp.Workspace=workspace warning("____________________________________") shpcounter+=1 #Increasing the counter for each featureclass warning(str(shpcounter)+". ["+shp+"] in:"+workspace) #gp.AddWarning(Warning);reportfile.write(Warning+"\n")#Print and write the counter and the featureclass fields = gp.ListFields(shp,"*", "All")# Return a list with the Fields for the FeatureClass desc=gp.Describe(shp)#Describe the Featureclass shpfieldname = desc.ShapeFieldName# The name of the Shape field oidfieldname=desc.OIDFieldName# The name of the object id field name fields.reset() field=fields.next() #Initialize some necessary lists Ldelete=[]#Final list containing the fields to be deleted Lfieldsno=[] #List with the Number of Fields in Featureclass #For each field of the Featureclass: while field: Lfieldsno.append("1") #For every field of Featureclass add an Item in Lfieldsno with the value 1 #Compare les champs dans le fichier texte et les champs du jeu de classe et ajoute les valeurs communes a la liste #Action for "ON" case-sensitive option if casesensitive=="ON": for item in splitrfile: if field.Name == item: Ldelete.append(item) #Action for "OFF" case-sensitive option elif casesensitive=="OFF": for item in splitrfile: capitalfieldname=string.upper(field.Name) capitalitem=string.upper(item) if capitalfieldname==capitalitem: Ldelete.append(field.Name) field=fields.next() if oidfieldname in Ldelete:#Verifier les id des noms de champs Ldelete.remove(oidfieldname)#Retirer de la liste Ldelete message(oidfieldname+":is special ObjectId field and will not deleted!") if shpfieldname in Ldelete:#Verifie les noms de champs Ldelete.remove(shpfieldname)#Retirer de la liste Ldelete message(shpfieldname+":is special Shape field and will not deleted!") #Longueur des listes c=len(Lfieldsno)#Nombre de champs dans le jeu de classes b=len(Ldelete)#Nombre de champs devant etre supprimes #Checking the fields to delete in Featureclass. if c==3:#Featureclass must have at least 3 fields.So the script won't deleted any field of a featureclass with only 3 fields reportmsg="\tFeatureClass has only 3 fields.No fields will be deleted!" message(reportmsg) elif c>3:#but if the Featureclass has more than 3 fields then if b!=0: #If there are one or more fields to be deleted then #Check and remove any special fields (Shape and ObjectID fields) from the list with the fields to be deleted (Ldelete list) if c-b==3 or c-b>3:#Compare the number of fields in Featureclass and the number of fields to be deleted.If the remainder is >3(the mininmun number of fields in FeatureClass)then: deletefield("\tDeleting Fields:",Ldelete,shp,"All defined fields deleted succesfully in "+shp+"!") elif c-b<3 :#Compare the number of fields in Featureclass and the number of fields to be deleted.If the remainder is <3(the mininmun number of fields in FeatureClass)then: #Generate a multivalue string and Delete the fields except the last one. deletefield("\tFeatureClass MUST have at least 3 Fields.\nThe field "+"\""+Ldelete[len(Ldelete)-1]+"\""+" will not deleted!"+"\n"+"Deleting Fields:", Ldelete[:-1],shp,"All defined fields deleted succesfully in "+shp+" except "+Ldelete[len(Ldelete)-1]+" field!") else:#If didn't find any defined field (except special fields)from the txt file in the Featureclass, then just add a message message("\tNo fields, defined in the text file, will be deleted in "+shp+"!") #Impression de la fin du rapport timemsg = "\nTotal processing time:"+str(((time.time() - begin))) + " seconds" warning(timemsg+"\n\n\t\t***************END OF REPORT***************") except:#Erreur inattendue error("Une erreur s'est produite! Essayez a nouveau ") if reportpath!='#': reportfile.close()#Ferme et supprime de la memoire le fichier de rapport