Batch clip using arcpy script

16410
9
04-03-2013 09:47 AM
Myla-RaeBaldwin
New Contributor II

Hi, I am a GIS student, just starting out with python, and I am working on a project in which I want to create a custom script tool to clip a list of feature classes in a municipality to a single clip feature representing flood extent (FloodLevel). Can I create a list of the feature classes and then loop through the list to accomplish this? Here is what I am trying, though I cannot get the script to work at this point. I get an error that says: 'list' object is not callable. And I am not sure how to properly provide the output name for multiple features at once.

# Clip town features to a flood level polygon
# Define input parameter for floodLevel polygon
# Define infrastructure variables

FloodLevel = arcpy.GetParameterAsText(0)
FC1 = "Town_Boundary"
FC2 = "Parcels_2010"
FC3 = "Buildings_2009"
FC4 = "Trails"
FC5 = "Banff_Roads"
FC6 = "Water_Main"
FC7 = "Storm_Main"
FC8 = "Hydrant"

# Create list of feature classes and loop through the feature classes
Features = [FC1, FC2, FC3, FC4, FC5, FC6, FC7, FC8]

# Clip features in list to the FloodLevel polygon feature
for items in Features():
    arcpy.Clip_analysis(Features, FloodLevel, "Features_clip")

Any suggestions on how I could solve this scenario would be helpful!
Thanks,
Myla-Rae

(Curtis Price formatted code block 4/29/2015)

Tags (2)
0 Kudos
9 Replies
MathewCoyle
Frequent Contributor
You are trying to call your list as a function. And as your error says a list is not callable.

You should just need to modify these lines
for item in Features:
    arcpy.Clip_analysis(item, FloodLevel, "Features_clip")
0 Kudos
Myla-RaeBaldwin
New Contributor II
Thanks, this has solved my problem with the "uncallable list". I am still have difficulty clipping a list of features. I have tried a number of things, here is where I am now:

# Clip town features to a flood level polygon
# Define input parameter for floodLevel polygon
# Define infrastructure variables

FloodLevel = arcpy.GetParameterAsText(0)
FC1 = "Town_Boundary"
FC2 = "Parcels_2010"
FC3 = "Buildings_2009"
FC4 = "Trails"
FC5 = "Banff_Roads"
FC6 = "Water_Main"
FC7 = "Storm_Main"
FC8 = "Hydrant"

# Create list of feature classes and loop through the feature classes
Features = [FC1, FC2, FC3, FC4, FC5, FC6, FC7, FC8]

for fc in Features:
    Output = os.path.join("G:/Test.gdb/ImpactedFeatures", fc)
    arcpy.Clip_analysis(Features, FloodLevel, Output)

------------------------------
I get an error for the line referencing the arcpy.Clip_analysis...
as well as this error:
File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\analysis.py", line 55, in Clip
    raise e
RuntimeError: Object: Error in executing tool
0 Kudos
by Anonymous User
Not applicable
Are you using the full paths to the data for the features in your list?  You could set up an env.workspace for the full path if they are all in the same workspace.  For example:

arcpy.env.workspace = r'G:\Some\Path\file.gdb'

for item in Features:
    arcpy.Clip_analysis(item, FloodLevel, "Features_clip")


Another option is you can used the attached toolbox if you want. I wrote batch clip tool a while back, it seems to work pretty well.  There are 2 options.

1. Batch clips all feature classes in a workspace with an optional wildcard
2. Uses the multivalue parameter to let the user select feature classes from different workspaces
by Anonymous User
Not applicable
Are you using the full paths to the data for the features in your list?  You could set up an env.workspace for the full path if they are all in the same workspace.  For example:

arcpy.env.workspace = r'G:\Some\Path\file.gdb'

for item in Features:
    arcpy.Clip_analysis(item, FloodLevel, "Features_clip")


Another option is you can used the attached toolbox if you want. I wrote batch clip tool a while back, it seems to work pretty well.  There are 2 options.

1. Batch clips all feature classes in a workspace with an optional wildcard and feature type
2. Uses the multivalue parameter to let the user select feature classes from different workspaces
0 Kudos
nedamohammadi1
New Contributor
Hello every body!
My goal in my thesis is landscape metrics for Terrasar data.I need to clip (as a raster data) from the fishnet which are veriety of feature classes.Attach you can see this file.
after the parrent fishnet I made clip by building model as attach file. then By python code I made fishnet for every parent fishnet:

import arcgisscripting

gp = arcgisscripting.create(9.3)
gp.overwriteOutput = 1

# input hard-coded but you can make it a script tool parameter to make it an interactive input.
in_fc = r"X:\mohammadi\finalmunichGIS\munich.gdb\fc_parent" # this polygon feature class has several polygons.

rows = gp.SearchCursor(in_fc)
row = rows.Next()

while row:
    feat = row.shape
    ext = feat.extent
    orig = str(ext.xmin) + " " + str(ext.ymin)
    yaxis = str(ext.xmin) + " " + str(ext.ymin + 100.00)
    opp_corner = str(ext.xmax) + " " + str(ext.ymax)
    print orig, yaxis, opp_corner

    out_fc = r"X:\mohammadi\finalmunichGIS\munich.gdb\fc_parent1_" + str(row.OBJECTID)

    result = gp.CreateFishnet_management(out_fc, orig, yaxis, "0", "0", "2", "2", opp_corner,"NO_LABELS","#","POLYGON")
    print(gp.GetMessages())

    row = rows.Next()

del row, rows

the next step I need to make clip as a raster file for every child fishnet*(it means like as for child clip as a attach file).
what do you sugest a python code or modelbuilder?
I tried many python codes and models, but it didn,t work.
again I have to focus that the result of above python code was list of feature classes and beacause of that I face problem in making loops.
thanks alot
nedaaa
0 Kudos
ToddGiberson
New Contributor
Myla-Rae,

From your code:

*********************

# Create list of feature classes and loop through the feature classes
Features = [FC1, FC2, FC3, FC4, FC5, FC6, FC7, FC8]

for fc in Features:
    Output = os.path.join("G:/Test.gdb/ImpactedFeatures", fc)
    arcpy.Clip_analysis(Features, FloodLevel, Output)

*********************

Your FOR loop iterates through the list, but your call to CLIP is supplied with the whole list again as the first argument!

It should read: arcpy.Clip_analysis(fc, FloodLevel, Output)
0 Kudos
nedamohammadi1
New Contributor
Hi all!

Thank you so much for your answer to Myla!the problem which I have is my data which are feature classes inter the .gdb !so python don,t know that!
I need to make clip with data management from the files which are fishnet as feature classes!
I give you my code and .gdb file!!!

import arcgisscripting
import arcpy
gp = arcgisscripting.create(9.3)
gp.overwriteOutput = 1

# input hard-coded but you can make it a script tool parameter to make it an interactive input.
in_fc = r"X:\mohammadi\finalmunichGIS\munich.gdb\fc_parent1_" # this polygon feature class has several polygons.
fc_parent1_=[fc_parent1_1, fc_parent1_2, fc_parent1_3, fc_parent1_4, fc_parent1_5, fc_parent1_6, fc_parent1_7, fc_parent1_8
           ,fc_parent1_9,fc_parent1_10, fc_parent1_11, fc_parent1_12, fc_parent1_13, fc_parent1_14, fc_parent1_15, fc_parent1_16
           , fc_parent1_17, fc_parent1_18, fc_parent1_19, fc_parent1_20]
 
rows = gp.SearchCursor(in_fc)
row = rows.Next()

while row:
    feat = row.shape
    ext = feat.extent
    orig = str(ext.xmin) + " " + str(ext.ymin)
    yaxis = str(ext.xmin) + " " + str(ext.ymin + 100.00)
    opp_corner = str(ext.xmax) + " " + str(ext.ymax)
    print orig, yaxis, opp_corner
 
    out_fc = r"X:\mohammadi\finalmunichGIS\munich.gdb\clip" # + str(row.OBJECTID)
 
    result = arcpy.clip_management("X:\mohammadi\finalmunichGIS\Testdaten-original data\munich_subset.tif", "650923.51 5297968.73 743103.51 5375868.73" ,
     "X:\mohammadi\finalmunichGIS\munich.gdb\clip","#", "#", "NONE")
    print(gp.GetMessages())

    row = rows.Next()

del row, rows


I hope you can help me and again thank you !!
nedaaa
0 Kudos
curtvprice
MVP Esteemed Contributor
Neda,

Please post your Python code in a code block. [thread=48475]Here's how.[/thread]

1. You may want to look into the Split Raster tool. It may be a lot easier to tile your data using that instead of making fishnets and using this scripting approach.

2. I did some once off code (not tested) to help you with working out how a script would work:

# only import arcpy - don't use arcgisscripting/gp at 10.x
import arcpy
from arcpy import env
arcpy.env.overwriteOutput = True ## 1

# in_fc is your polygon feature class
in_fc = r"X:\mohammadi\finalmunichGIS\clip0ffishnet9000\shapefilesoffc\fc_parent1" 
# in_tif is your input
in_raster = "X:\mohammadi\finalmunichGIS\Testdaten-original data\munich_subset.tif"

# output location
outGDB = r"X:\mohammadi\finalmunichGIS\munich.gdb"

arcpy.MakeFeatureLayer_management(in_fc,lyrFC)
# get list of object ids
objIDs = []
objIDField = arcpy.Describe(lyrFC).OIDFieldName
Rows = arcpy.SearchCursor(lyrFC)
while Row in Rows:
    objIDs.append(Row.getValue(objIDField))
del Row, Rows

# temp polygons
tmpFC = "in_memory/tmpPoly"

# one by one clip raster to polygons
arcpy.env.workspace= outGDB
for objID in objIDs:
    where = "{0} = {1}".format(objIDField,objID)
    arcpy.SelectLayerByAttributes_management(lyrFC,where)
    arcpy.CopyFeatures_management(lyrFC,tmpFC)
    ext = arcpy.Describe(tmpFC).Extent
    extString = "{0} {1} {2} {3}".format(ext.XMin, ext.YMin, ext.XMax, ext.YMax)
    out_raster = "clip{0}".format(objID)
    arcpy.Clip_management(in_raster, extString, out_raster, tmpFC)
    print arcpy.GetMessages(0)
arcpy.Delete_management(tmpFC)
0 Kudos
ScottHawkins
New Contributor
Thanks for this toolbox. It works great.
0 Kudos