batch cliptool with multivalue cliptheme and multivalue outputfolders with Python

Discussion created by RheinNeckar226 on Oct 5, 2011
Hello dear Python experts !

I need some kind of Python batchClip script with some additional properties.

Description of the problem:
Almost 100 input features (different feature classes in an ArcGIS mxd) should be clipped by a list of clip features (54 polygon shape files in a defined folder or in the mxd) and the resulting shapes should be stored in prepared output folders (list of 54 folders) in the file system. Could this challenge be taken easily by changing an existing Pyhton script ?

Batch clipping can easily be done by an existing tool in the toolbox run in the batch mode but only with one clip feature and in one single output folder.

I already tried to deal it with the ModelBuilder but at least the model doesn't work stable, takes very long time to process and has to be repeated as much as Clip Feature there are, that means in my case 54 times. So this is not the kind of automation I  had in mind before.

Maybe somebody can help or give advices. Unfortunately there will be syntax changes in Python scripting from Version 9.3 to 10. At the moment I have still the choice of the versions.

There is an existing script by Dan Patterson for the Version 9.3, which can be modified by adding some loops. I tried it out but didn't get any further. Anyway I put Dan???s original code into this message. My knowledge of Python is not sufficient to get through with my foreseen adaption.

Maybe there is somebody who solved a similar problem ???Thanks.


#  Dan Patterson
#  Dept of Geography and Environmental Studies
#  Carleton University, Ottawa, Canada
#  Batch clips feature classes (eg shapefiles) in a project
#  and writes them to a folder.
#  Many batch clip examples exist, this is compiled for demonstration
#  purpose for a class
#Properties (right-click on the tool and specify the following)
#  Name   BatchClipFCs
#  Label  Batch Clip Feature Classes
#  Desc   Batch clip feature classes (eg shapefiles) and saves
#         them to a folder.
#Source  BatchClipFCs.py
#Parameter list
#                                                Parameter Properties
#           Display Name          Data type      Type      Direction  MultiValue
#  argv[1]  Feature(s) to clip    Feature Layer  Required  Input      Yes
#  argv[2]  Polygon clip layer    Feature Layer  Required  Input      No
#  argv[3]  Append to output      String         Optional  Input      No
#           filename
#  argv[4]  Output folder         Folder         Required  Input      No
#Import the standard modules
#  Get a list of feature classes (shapefiles)
#  Get the clip feature class
#  Get the optional text to append to the filename output filename
#  Get the output folder

# Usage: BatchClipFCs <Input_Features> <ClipWith> <optional_text> <Output_Workspace>
# ---------------------------------------------------------------------------
# Import system modules, Create the Geoprocessor object and
# Load required toolboxes...
import sys, string, os, win32com.client
gp = win32com.client.Dispatch("esriGeoprocessing.GpDispatch.1")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")
#gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Data Management Tools.tbx")
# Script arguments...
FCsToClip = gp.GetParameterAsText(0)    #a list of features
fcs = string.split(FCsToClip, ";")      #split into a list
clipWith = sys.argv[2]                  #clip layer
desc = gp.Describe(clipWith)
clipWithType = desc.ShapeType
addToName = sys.argv[3]                 #optional text to append to output name
if(addToName == "#"):
  addToName = ""
outputFolder = sys.argv[4]              #a folder
gp.Addmessage("\n" + "Batch Clip Feature layers" + "\n" )
if (clipWithType == "Polygon"):
  for fc in fcs:
      fc=fc.replace("'","")             #check if a layers name has been changed
      desc = gp.Describe(fc)
      fcDataType = desc.DataType
      gp.AddMessage("Data type = " + fcDataType)
      if(fcDataType == "FeatureClass"): # Feature class on disk
        theName = str(os.path.split(fc)[1])
        outFile = outputFolder + "\\" + theName + addToName + ".shp"
      else:                             # Feature layer
        FullName = desc.CatalogPath
        theName = (os.path.split(FullName)[1]).replace(".shp","")
        theName = str(fc).replace(" ","_")
        outFile = outputFolder + "\\" + theName + addToName + ".shp"
        gp.AddMessage("Output file from layer " + outFile)
      gp.AddMessage("Clipping " + str(fcDataType) + ": " + fc + " Saving to: " + outFile + "\n")
        gp.Clip_analysis(fc, clipWith, outFile, "")
        gp.AddMessage("Could not clip " + fc + " with " + clipWith + " to " + outFile)
      gp.AddMessage("cannot describe" + fc)
  gp.AddMessage (clipWith + " is not a polygon layer, clipping terminated" )