1

I'm having trouble creating a looping function that looks through a geodatabase, clips each feature class to a "study_area" polygon, and puts the clipped files in a separate geodatabase. I keep getting "Could not clip feature classes". The features can all be clipped individually but trying to run this loop doesn't work.

What am I missing here?

I tried to search for similar questions but only found answers regarding raster clips. Code below.

from arcpy import env


# setting my source and target geodatabase 
SourceWorkspace = r"E:\UFD Commercial Corridor\Roosevelt.gdb"
TargetWorkspace = r"E:\UFD Commercial Corridor\Clipped.gdb"

#setting my clip feature (which is a polygon)
clipfeature = r"E:\UFD Commercial Corridor\Clip area.gdb\study_area"

env.workspace = SourceWorkspace

try:
    fclist = arcpy.ListFeatureClasses()

    for fc in fcList:
        outClipFeatureClass = targetWorkspace + "//Studyarea_" + fc

        arcpy.Clip_analysis(fc,clipfeature,outClipFeatureClass)

except: 
    arcpy.AddError("Could not clip feature classes")
    print ("could not clip feature classes")
    print arcpy.GetMessages() 
2
  • 3
    It's bad practice to wrap things in a bare try/except block because it obscures any errors that you weren't expecting. If you remove that then at least one of the issues @Aaron mentions should become obvious – mikewatt Sep 4 '20 at 23:44
  • I removed that block and revised in accordance with @Aaron's guidance, but the clip is still failing. I'm getting the following error "Runtime error Traceback (most recent call last): File "<string>", line 20, in <module> File "c:\program files (x86)\arcgis\desktop10.7\arcpy\arcpy\analysis.py", line 62, in Clip raise e ExecuteError: ERROR 999998: Unexpected Error." Lines 17-20 are as follows: for fc in fcList: outClipFeatureClass = os.path.join(TargetWorkspace, "study_area_" + fc) arcpy.Clip_analysis(fc, clipfeature, outClipFeatureClass) – Mark Sep 7 '20 at 17:14
3

I see two problems:

  1. you are defining TargetWorkspace though you are calling targetWorkspace. Python is case-sensitive.

  2. You need to properly join together strings to create paths. I highly recommend becoming familiar with os.path.join(). For example

Instead of:

outClipFeatureClass = targetWorkspace + "//Studyarea_" + fc

Use:

outClipFeatureClass = os.path.join(TargetWorkspace, "Studyarea_" + fc)

import arcpy
import os

# setting my source and target geodatabase 
SourceWorkspace = r"E:\UFD Commercial Corridor\Roosevelt.gdb"
TargetWorkspace = r"E:\UFD Commercial Corridor\Clipped.gdb"

#setting my clip feature (which is a polygon)
clipfeature = r"E:\UFD Commercial Corridor\Clip area.gdb\study_area"

env.workspace = SourceWorkspace

fclist = arcpy.ListFeatureClasses()

for fc in fcList:
    outClipFeatureClass = os.path.join(TargetWorkspace, "Studyarea_" + fc)
    arcpy.Clip_analysis(fc,clipfeature,outClipFeatureClass)

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.