Select to view content in your preferred language

ArcGIS 10 Batch Clip

5663
6
11-05-2010 12:32 PM
JoshuaFerguson
Deactivated User
I'm new to python and just upgraded ArcGIS 10. I've been experimenting with python and I'm trying to batch clip several layers in ArcMaps TOC. The number of layers can change depending on the user.  The script runs without any errors, however nothing happens. I can make a clip of a single layer based on another layer work, but when I change the code and try to implement a loop to batch the process I'm getting stuck.

here's my code


import arcpy.mapping

arcpy.AddToolbox("C:\Program Files\ArcGIS\Desktop10.0\ArcToolbox\Toolboxes\Analysis Tools.tbx")
clip = arcpy.GetParameterAsText(0) # input, required, single value
outFC = arcpy.GetParameterAsText(1) # output, required, single value
toclip = arcpy.mapping.MapDocument("Current")
df = arcpy.mapping.ListLayers(toclip)
try:
    fcs = arcpy.mapping.ListLayers(toclip)
    fcs.Reset()
    fc = fcs.Next()
    while fc:
        outFeatureClass = outFC +"/" + arcpy.ValidateTableName(fc, outFC)
        arcpy.Clip_analysis(fc, clip, outFeatureClass, "")
    fc = fcs.Next()
except:
    arcpy.GetMessages(2)

Thanks!
0 Kudos
6 Replies
AmyRose
Deactivated User
Hello,
By chance did you find an answer to your problem?  I am trying to do the same thing or at least batch clip selected feature classes by a single polygon shapefile.
I had VBA code that did this for me, but I need to now use python.  I will admit I am a python newbie.
Any help or advice appreciated,
Amy
0 Kudos
DarrenWiens2
MVP Honored Contributor
Rather than reinvent the wheel, why not just right-click the Clip tool, choose Batch, and drag and drop what you need?

edit: if you've got your heart set on using, or have a reason to use, Python, reply.
0 Kudos
DennisJarrard
Esri Contributor
Not too long ago, I created a custom model that can clip an entire workspace:

Workspace Clip
http://resources.arcgis.com/gallery/file/geoprocessing/details?entryID=F9507173-1422-2418-7FC3-DC15A...

"This model extracts a specific geography, and then clips an entire workspace to a specified directory. This model is useful for quickly clipping an entire workspace (folder, geodatabase, or feature dataset). SQL query statements (optional) make it easy to define the constraining geography without having to extract it to a new file first. The model also uses inline variable substitution to ensure that the output filenames can be quickly recognized after the process completes. "

Of course, there may be other reasons you specifically want to use Python.
0 Kudos
curtvprice
MVP Esteemed Contributor
We can't check your loop unless you [thread=48475]post your code in a code block![/thread] Python indentation is significant!
0 Kudos
ConstanceDavisson
Emerging Contributor
A group of genius:D
0 Kudos
JohannesBierer
Frequent Contributor

I use this as a toolbox script?

arcpy.env.overwriteOutput = True
input = arcpy.GetParameterAsText(0)
clipfeature = arcpy.GetParameterAsText(1)
output = arcpy.GetParameterAsText(2)
arcpy.env.workspace = output
inputSp = input.split(";")
for i in inputSp:

    arcpy.AddMessage(i)
    arcpy.Clip_analysis(i, clipfeature, i)
0 Kudos