Iterating through a feature class.

4263
5
07-27-2015 12:56 PM
DeidreA
New Contributor

Hello everyone!

I am a Python newbie, so forgive me if this question is pretty simple.

I'm trying to iterate through a shapefile feature class. The shapefile contains a bunch of points that I'm trying to clip based on another layer. This is what the script looks like when I do it:

arcpy.Clip_analysis('RF_Acreage', 'Lake_Ontario_Plains_FDRA_Boundary', r'L:\OPP\Rangers\Central Office\GIS\Deidre GIS Reporting Files\testmodel\Modeloutputs.zxgdb')

It works fine on it's own, but I need to do it for multiple regions. so it would be very tedious to clip one by one. So basically my input features will stay as 'RF_Acreage' while I clip it too different features.

Should I be using if statements and for loops? If so, how do I go about stringing these together?

Hopefully I'm making sense. Thanks in advance!

Tags (2)
0 Kudos
5 Replies
DarrenWiens2
MVP Honored Contributor

Check out the examples and documentation for ListFeatureClasses.

Basically, get the feature classes into a list, then loop through the list.

IanMurray
Frequent Contributor

Also make sure you are naming out of your output features a unique name, otherwise it will either crash or overwrite the features you are creating.

If you have a relatively small number of features, it might be easier to run clip in batch mode rather than write out a script.  As long as you set parameters that stay the same before you add new rows in batch, it will keep the parameters the same for each row.

DeidreA
New Contributor

But I only have 1 feature class I'm working with. I'm trying to clip it into multiple feature classes. Sorry if I wasn't clear. Do I create a list of the one feature class and to a for loop to clip it to different layers?

0 Kudos
IanMurray
Frequent Contributor

You would make a list of the feature class you want to clip with and leave the input the same for the 1 feature class you want clipped.  You would then loop through each of the feature classes you want to clip with and use each individual one as the clipping layer for the tool.  Your input would stay the same each time.

BlakeTerhune
MVP Regular Contributor

With the suggestions posted so far and the example posted in the Clip Analysis Help​, you should be able to get started. Here's what the list and the for loop might look like...

clip_features = [
    'Lake_Ontario_Plains_FDRA_Boundary',
    'Another_FDRA_Boundary',
    'Some_FDRA_Boundary',
    'Other_FDRA_Boundary'
]

for fc in clip_features:
    out_feature_class = in_features+fc  ## Naming is up to you
    arcpy.Clip_analysis(in_features, fc, out_feature_class)
0 Kudos