2

enter image description hereI have multiple feature classes (buildings, airport, land, roads etc) that I would like to clip using a Local Authority boundary. I am trying to create a process in model builder or Arcpy that will iterate the clip operation through the feature classes and create separate output (clipped building, airport, land, roads etc) for each feature class.

Please can anyone advice me on how to automate this than having to set separate clip operations for each feature class?

I've tried using the Iterate feature class in model builder but it only selects and clips the first feature class (Airports) not all feature classes.

  • Thank you for your reply. I tried the iterate feature class with the recursive option enabled but it limits clipping to the first feature class. Is there any change I can make to the model to make it iterate through all the feature classes not just the first feature class? – user140198 Aug 16 at 13:17
  • 2
    @Bera many thanks it has worked. I noticed I omitted (Clip_) in the clip output geodatabase. Corrected as suggested 'C:\Feature.gdb\Clip_%Name%'. – user140198 Aug 16 at 16:42
3

Assuming you have all your features in the same folder or GDB you can try the following:

import arcpy

arcpy.env.workspace = r'path\to\your\folder'

feature_classes = arcpy.ListFeatureClasses()
clip_feature = r'path\to\clip_feature'  # this would have to be on a different folder

for fc in feature_classes:
    arcpy.Clip_analysis(fc, clip_feature, 'clip_{}'.format(fc))

Make sure to read the documentation.

  • Villa, thank you for the syntax. This also worked. – user140198 Aug 16 at 17:16
  • please how do I specify a different output folder in the above code than that specified in the workspace? I tried specifying an output folder with fc format function but it didn't work - 'path\output_folder\'clip_{ }'.format(fc))' – user140198 Sep 10 at 14:16
  • @user140198 That should work. Are you writing an r before the string like this: r'path\output_folder\clip_{}'.format(fc)? If that does not work feel free to create a new question and comment the link here so I can take a look. – Marcelo Villa Sep 10 at 14:19
  • Thanks @Marcelo Villa for the prompt reply and the formatted code. I noticed from your code that I had the quotation marks in the wrong place. I've made changes and it now runs as expected. Please may I ask what the 'r' before the file path does? Sorry I'm new to coding and would like to know. I notice the code still work with and without it. Thank you so much for your help. – user140198 Sep 10 at 15:17
  • Glad it is working now. The r before a string means that it is a raw string literal. This means that the string will be interpreted as it is instead of reading backslashes and specific characters as escaping sequences. For example '\n' means a new line but if you write r'\n' it will be evaluated to \n instead of a new line. Try that example in the console using print statement; it might help you understand it better. – Marcelo Villa Sep 10 at 15:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.