English

How To: Automate creating feature classes

Summary

GIS managers and administrators sometimes need to automate the creation of feature classes based on a template. The steps below describe how to do this.

Procedure

The below Python script can be used to automate the creation of feature classes based on specified fields given in the script. As an example, the script below creates three feature classes.
 

  1. Modify the below variables as needed:
    Code:
    #Database connection file path
    conn = r"Database Connections\Loop.sde"
    
    #Name of feature classes to be created
    FCList = ["State_Px1","Ozonex1","Birajx1"]
    
    #Name of fields to be populated for first feature class
    
    fields1 = ['State', 'State_Code', 'Xfield']
    
    #Name of fields to be populated for second feature class
    
    fields2 = ['Areax', 'Ozone_Amt', 'S3']
    
    #field types for feature class one
    fieldType1 = ["TEXT", "DOUBLE", "LONG"]
    
    #field types for feature class two
    fieldType2 = ["TEXT", "DOUBLE", "LONG"]
  2. Python script:
    Code:
    import arcpy, string, os
    def CreateFeatureClass(conn, fc,logWorkspace,logFC):
     outLog = open(os.path.join(logWorkspace, logFC), 'w')
    
     arcpy.CreateFeatureclass_management(conn,fc,"POLYGON","","DISABLED","DISABLED","GEOGCS['GCS_WGS_1984',DATUM'D_WGS_1984',SPHEROID['WGS_1984',6378137.0,298.257223563]],PRIMEM['Greenwich',0.0],UNIT['Degree',0.0174532925199433]];-400 -400 1000000000;-100000 10000;-100000 
    10000;8.98315284119522E-09;0.001;0.001;IsHighPrecision","","0","0","0")
     print "created fc {0}".format(fc)
     outLog.write("Created feature class {0}".format(fc))
     outLog.write("\n")
     outLog.close()
    
    def AddFields(conn_fc, fieldList, fieldType):
    
     for field, ftype in zip(fieldList,fieldType):
    
     arcpy.AddField_management(conn_fc,field,ftype,"","","","","NULLABLE","NON_REQUIRED","")
     print "Added field {0} for fc: {1}".format(field, conn_fc)
    
    
    if __name__=="__main__":
    
     #Variables to populate
     conn = r"Database Connections\Loop.sde"
     FCList = ["State_Px1","Ozonex1","Birajx1"]
     fields1 = ['State', 'State_Code', 'Xfield']
     fields2 = ['Areax', 'Ozone_Amt', 'S3']
     fieldType1 = ["TEXT", "DOUBLE", "LONG"]
     fieldType2 = ["TEXT", "DOUBLE", "LONG"]
    
     logFC = "CreatedFC_log.txt"
     logWorkspace = r"C:\temp"
    
    
     fcpy_list = []
    
     #Create Feature Classes
     cnt = 0
     for fc in FCList:
     CreateFeatureClass(conn, fc, logWorkspace,str(cnt) + logFC )
     fcpy_list.append(fc)
     cnt += 1
     del fc
    
     conn1_fc = conn + os.sep + fcpy_list[0]
     conn2_fc = conn + os.sep + fcpy_list[1]
     conn3_fc = conn + os.sep + fcpy_list[2]
     AddFields(conn1_fc, fields1,fieldType1)
     AddFields(conn2_fc, fields2,fieldType2)
     AddFields(conn3_fc, fields2,fieldType2)