0

I have a file geodatabase which includes a lot of feature datasets and each feature dataset has lots of Features. I am going to do clipping through all the Features using ArcPy Code. However, the clipped Features are created in the same feature datsets. I would like to know how I can chnage this script to have a new FGDB with the same feature datasets but just include the clipped Features.

import arcpy, os
database = r'C:\Users\...\WEB_clip_4half.gdb' 
clip_feature = r'C:\...\Kapsel_.shp

try:

    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:
            if '_clipped' in feature:
                arcpy.Delete_management(in_data=feature)
        else:    
             arcpy.Clip_analysis(in_features=feature,clip_features=clip_feature, out_feature_class=feature+'_clipped')
except Exception as e:
  print("Found an error - {0}".format(e))
  • Do you have got new data sets or need to create them? – JuniorPythonNewbie Nov 14 '18 at 13:27
1

Looks like you have some indentation issues in your code-snippet, else statement should be aligned with the if statement. As far as creating the geodatabase and feature datasets you may use these arcpy methods:

Create File GDB

Create Feature Dataset

Within your logic before you create the geodatabase and feature dataset in your destination geodatabase you will want to include the Exists method to evaluate if these files exist already or not. You may also want to consider using Walk to better traverse through the source geodatabase to pull out feature dataset names to hold as a variable for creation of new feature datasets in destination geodatabase.

0

I assumed you have empty GDB and your final dataset wil have same spatial reference as a clip shp.

Script creates new dataset and then does clip.

Change new_database (and sr (spatial reference) if necessary):

More info about sr in dataset you can find below:

http://pro.arcgis.com/en/pro-app/tool-reference/data-management/create-feature-dataset.htm

Your code little fixed:

import arcpy, os
database = r'C:\Users\...\WEB_clip_4half.gdb' 
new_database = r'NEW'
clip_feature = r'C:\...\Kapsel_.shp'
sr = r'C:\...\Kapsel_.shp'
try:
    arcpy.env.workspace = database
    datasets = arcpy.ListDatasets('Feature')
    for dataset in datasets:
        features = arcpy.ListFeatureClasses()
        for feature in features:
            if '_clipped' in feature:
                arcpy.Delete_management(feature)
            else:
                CreateFeatureDataset_management(new_database, dataset, sr)
                arcpy.Clip_analysis(feature, clip_feature, os.path.join(new_database, dataset, feature))
except Exception as e:
    print("Found an error - {0}".format(e))
  • Thank you for your Feedback. May I ask what is the "NEW" for GDB? is that the Name of that or how it is going to be created? – Chiki2025 Nov 15 '18 at 11:05
  • I run this Code but I do not get any Feedback. where could be the Problem? – Chiki2025 Nov 15 '18 at 11:25
  • Just add path to the new GDB. – JuniorPythonNewbie Nov 15 '18 at 11:38
  • I actually did it but still I donot get any new GDB – Chiki2025 Nov 15 '18 at 12:56
  • I assumed you have empty GDB -- please read what I wrote. Just make new gdb, fill path and should it work. – JuniorPythonNewbie Nov 15 '18 at 13:12

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.