Programming‎ > ‎Python‎ > ‎

Batch Processing

One of the main advantages of using Python is the ability to do batch processing.  Batch processing allows for running one or many geoprocessing steps on several files without user interaction.  Below are sample scripts to solve a number of different problems.  Any geoprocessing task can be automated to process large amounts of data files.

Compacting a batch of .mdb workspaces in a given directory:

# ---------------------------------------------------------------------------
# Doug Browning (dbrowni)
# batch_compact.py
# Created on: Sep 15 2009 
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting

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

# Get the directory to use. 
dir = sys.argv[1]
gp.workspace = dir

try:
    # Get a list of workspaces
    files = gp.ListWorkspaces("*.mdb")
    files.Reset()
    oneFile = files.Next()    

    while oneFile:    
        print "Compacting " + oneFile

        # Check size
        size = os.path.getsize(oneFile)
        print "File size before compact:", size

        # Compact the file 
        gp.compact_management(oneFile)

        # Check size
        size = os.path.getsize(oneFile)
        print "File size AFTER compact:", size
        print "\n"

        oneFile = files.Next()        
except:
    # If an error occurred when running the tool,
    # print out the error message.
    print gp.GetMessages( ) 


Perform Polygon Simplification on all Shapefiles in a given directory:

# ---------------------------------------------------------------------------
# Doug Browning (dbrowni)
# batch_simplifyPoly.py
# Created on: Sep 15 2009 
# ---------------------------------------------------------------------------

# Import system modules
import sys, string, os, arcgisscripting, C:/temp/add.py

# Create the Geoprocessor object
gp = arcgisscripting.create()
gp.workspace = "C:/temp"

try:
    # Get a list of all Polygon shapefiles
    fcs = gp.ListFeatureClasses("*","Polygon")
    fcs.Reset()
    fc = fcs.Next()

    while fc:
        # Set output file name
        outFile = fc.split(".")[0] + "_simplified." + fc.split(".")[1]
        # Run simplifypolygon tool        
        gp.simplifypolygon_Management(fc, outFile, "POINT_REMOVE", "50") 
        fc = fcs.Next()

except:
    gp.GetMessages()


Running the FeaturetoRaster tool for all files in a given directory:

# ---------------------------------------------------------------------------
# Doug Browning (dbrowni) 
# batch_feature2raster.py
# Created on: Sep 17 2009 
# ---------------------------------------------------------------------------

# ---------------------------------------------------------------------------
# F2R-Model.py
# Created on: Thu Aug 14 2008 10:26:47 AM
# (generated by ArcGIS/ModelBuilder)
#Modified on: Thu Aug 14 2008 13:12:43 AM
# ---------------------------------------------------------------------------
# Import system modules
import arcgisscripting, os, sys 
# Create the Geoprocessor object
gp = arcgisscripting.create() 
gp.workspace = "C:/Temp/ncshape.mdb"

outdir = "C:/Temp/output/"
if not os.path.exists(outdir):
         os.mkdir(outdir)

InField = "cat"         

try:
    ###Get a list of feature class files in the input folder 'path' that start with the word 'bus'
    fList = gp.ListFeatureClasses("bus*")
    fList.Reset()
    feat = fList.Next()
    while feat:     ### Begin the loop (Check the looping condition)
        OutRaster = outdir + feat
        print feat
        print InField
        print OutRaster
        gp.FeatureToRaster_conversion(feat, InField, OutRaster) #Process: Feature to Raster for each feature class
        feat = fList.Next()    ### update the looping condition
        #End of the loop 

except:
    gp.GetMessages()

print "All done." 


Merge all independent DBASE tables in a given directory into one table:

# ---------------------------------------------------------------------------
# Doug Browning (dbrowni)
# mergeTableLoop.py
# Created on: Sep 22 2009 
# ---------------------------------------------------------------------------

# Import system modules
import arcgisscripting, os, sys 
# Create the Geoprocessor object
gp = arcgisscripting.create()

inputDir = sys.argv[1]
gp.workspace = inputDir
gp.overwriteoutput = 1
     

try:
    # Get a list of dbase tables - found out this returns stand alone dbf tables for me!
    dbfList = gp.ListTables("*", "dBASE")
    dbfList.Reset()
    dbfFile = dbfList.Next()

    mergeString = ""    
    
    while dbfFile:
        if dbfFile != "output.dbf":  # do not add output.dbf to the list if it is already in the dir
            mergeString = mergeString + dbfFile + ";"      
         
        dbfFile = dbfList.Next()    ### update the looping condition
        #End of the loop 

    if mergeString:
        # if there are dbfs to merge then merge them
        gp.Merge_management(mergeString, "output.dbf")
        print "Merged - " + mergeString
    else:
        print "Sorry nothing to merge."

except:
    gp.GetMessages()
    
print "All done."