1

I have Python code for clipping Features in a File GDB which is basically correct and works nicely. However, I have a big File GDB in which there are a lot of featuredatsets including many Features. Some of them are already clipped so when I run the Code over whole GDB , it would stop when it reaches the already clipped data but I cannot check them one by one and eliminate them and the error does not tell me which one and just stops. Is there any solution that I tell in the in case the function is already done, just move on to the next featuredataset?

import arcpy, os
database = r'C:\Users\...\WEB_clip.gdb' 
clip_feature = r'C:\Users\...\160606_Kapsel_.shp' 

arcpy.env.workspace = database
datasets = arcpy.ListDatasets(feature_type='Feature')

for dataset in datasets:
    arcpy.env.workspace = os.path.join(database,dataset)
    features = arcpy.ListFeatureClasses()
    for feature in features:
        arcpy.Clip_analysis(in_features=feature, clip_features=clip_feature, out_feature_class=feature+'_clipped')
  • 1
    Did you give a special common name for the clipped feature datasets? For example, did you add something like '_Clip' in the names of the features that were already clipped? – ahmadhanb Nov 13 '18 at 12:16
  • 1
    It is usually possible to prevent a script from performing unnecessary steps, but without the relevant code, the answer to your question can only be "Probably." Please Edit the question to provide more details (exact software in use) and an example of the code you are having difficulty altering. – Vince Nov 13 '18 at 12:19
2

You can also use the following:

for feature in features:
    if "_clipped" in feature:
        continue
    else:
        arcpy.Clip_analysis(in_features=feature, clip_features=clip_feature, out_feature_class=feature+'_clipped')

The continue statement in Python returns the control to the beginning of the for loop skipping the features which have _clipped in their names and moves the control back to the top of the loop to clip other features that are not yet clipped.

@BERA

Here is the code I tested on shapefile not geodatabase:

import arcpy,os
from arcpy import env
env.workspace = r"F:\Ahmad\Test\Test4\New Folder"
clipFeature = r"F:\Ahmad\Test\Test4\New Folder\ClipFeature\Clip_Feature.shp"

features = arcpy.ListFeatureClasses()
for feature in features:
    name,ext = os.path.splitext(feature)
    if "_clipped" in name:
        continue
    else:
        arcpy.Clip_analysis(feature,clipFeature,name+'_clipped'+ext)

Here is the output:

enter image description here

The one with red arrow was already exit, and only Test_Polygon1.shp was clipped.

  • Thank you very much for your guidance. I have updated the Code and it works better now. but still there is a Problem that the Code would pass through some Features. It means it doesnot Clip some of the Features. I have to do the check after running the script and see what remains and Clip them manually. Is there any solution for this Problem? – Chiki2025 Nov 14 '18 at 12:21
  • @Chiki2025 it depends on how the features are named. Could you share the feature names? – ahmadhanb Nov 14 '18 at 14:07
2

You can replace the last two lines with:

for feature in features:
    if '_clipped' in feature:
        arcpy.Delete_management(in_data=feature)

Which will delete clipped fc outputs.

Then rerun the clip script. This is easier than checking for unclipped fc and clipped fc and clip if clipped fc is not found. Will be slower though since all fcs are clipped again.

  • I am still strugling with this Code. I hvae applied the chnages you offered but still it gives error. It stocks in a feature and says that the table does not exist. Athough it has to create that table not Looking for that. What should I do now? – Chiki2025 Nov 15 '18 at 10:48

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.