clip from multiple folders and gdb

4302
4
Jump to solution
01-21-2015 06:20 AM
PeterVersteeg
Occasional Contributor II

Hi,

With lots of help I have a script that search my folder and gdb for featureclasses and rename them. That script works great.

The next step in my workflow will by clipping. I want the clip feature to clip all my featuresclasses in all folders and gdb’s. I am using the rename script as kind of a template.

The problem now is in a unique output name (the name of the featuresclasses in the gdb are the same (not the factor only the name).

The script I have so far is wrong but it I was hoping it could do the trick if I set it right.

So far the error is in line 10 the join is not working I do not know if the unique_name will work but that is my solution for the unique name problem.

BTW I was think about a script that looks at the extend (clip feature) first and if true then clip and if not nothing.  I am over my head already and I have a script that cleans my gdb’s of empty featureclasses. So for now for my this is ok.

import os 
import arcpy  
from arcpy import env


workspace = "D:\\GIS\\Zone1\\Zone1A"  
feature_classes = []
clipfeature = "D:\\GIS\\Temp\\clip.gdb\\BorderClip"
outputdatabase = "D:\\GIS\\Output\\clip.gdb"
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="All")  
fc_output = os.path.join(outputdatabase,filenames)
unique_name = arcpy.CreateUniqueName(fc_output)


for dirpath, dirnames, filenames in walk:  
    for filename in filenames:  
        feature_classes.append(os.path.join(dirpath, filename))  
        
#    arcpy.AddMessage("clipping: " + feature_classes)
arcpy.Clip_analysis(feature_classes, clipfeature, ounique_name)




Greeting Peter

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Peter,

It looks like the code will fail at line 11 above in the 'fc_output' variable since 'filenames' is not yet defined.

Try the following:

import os, arcpy    
from arcpy import env  
  
workspace = "D:\\GIS\\Zone1\\Zone1A"    
clipfeature = "D:\\GIS\\Temp\\clip.gdb\\BorderClip"  
outputdatabase = "D:\\GIS\\Output\\clip.gdb"

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="All")
for dirpath, dirnames, filenames in walk:    
    for filename in filenames:
        inputFC = os.path.join(workspace, filename)
        fc_output = os.path.join(outputdatabase, filename)
        unique_name = arcpy.CreateUniqueName(fc_output)
        arcpy.Clip_analysis(inputFC, clipfeature, unique_name)  

print "Finished"

View solution in original post

4 Replies
PhilippeRieffel1
Occasional Contributor

Hello Peter,

just after a short look, it seems that you are referencing the variable "filenames" in line 11 before even creating it. that may cause your script to fail at this point.

Cheers,

Philippe

JakeSkinner
Esri Esteemed Contributor

Hi Peter,

It looks like the code will fail at line 11 above in the 'fc_output' variable since 'filenames' is not yet defined.

Try the following:

import os, arcpy    
from arcpy import env  
  
workspace = "D:\\GIS\\Zone1\\Zone1A"    
clipfeature = "D:\\GIS\\Temp\\clip.gdb\\BorderClip"  
outputdatabase = "D:\\GIS\\Output\\clip.gdb"

walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="All")
for dirpath, dirnames, filenames in walk:    
    for filename in filenames:
        inputFC = os.path.join(workspace, filename)
        fc_output = os.path.join(outputdatabase, filename)
        unique_name = arcpy.CreateUniqueName(fc_output)
        arcpy.Clip_analysis(inputFC, clipfeature, unique_name)  

print "Finished"
PeterVersteeg
Occasional Contributor II

Hi Jake,

thank you for the corrections. in line 11 in your script is 1 mistake workspace must by dirpath. then the scripts works great.

import os, arcpy      
from arcpy import env


workspace = "D:\\Documenten\\GIS\\Duitsland\\N47GDB3"      
clipfeature = "D:\\Documenten\\GIS\\Duitsland\\clip.gdb\\BorderClip"    
outputdatabase = "D:\\Documenten\\GIS\\Duitsland\\N47GDB3\\clip.gdb"  
  
walk = arcpy.da.Walk(workspace, datatype="FeatureClass", type="All")  
for dirpath, dirnames, filenames in walk:      
    for filename in filenames:  
        inputFC = os.path.join(dirpath, filename)  
        fc_output = os.path.join(outputdatabase, filename)  
        unique_name = arcpy.CreateUniqueName(fc_output)  
        arcpy.Clip_analysis(inputFC, clipfeature, unique_name)   


0 Kudos
PeterVersteeg
Occasional Contributor II

For those who like the script this is my model.

Greetings Peter