Select to view content in your preferred language

Clip feature classes by feature classes

997
6
06-16-2010 06:52 AM
bogdanpalade1
Deactivated User
Hello,

There are a lot of examples on the web on how to clip a lot of layers by a single feature class, and the python code would be more or less like below... But what happens when you want to clip each feature class in the input folder by more than one feature classs located for example in a "Clip_features" folder? Hor do you iterate at the same time over the input feature classes and the clip feature classes?

 
import arcgisscripting, sys, os
gp = arcgisscripting.create()
gp.workspace = "D:\inWorkspace"
clipFeatures = "D:\shape.shp"
outWorkspace = "D:\outWorkspace"
fcs = gp.ListFeatureClasses()
fcs.Reset()
fc = fcs.Next()
while fc:
          outFeatureClass = outWorkspace + os.sep + fc.split('_')[0] + "_clip.shp"
          gp.Clip_analysis(fc, clipFeatures, outFeatureClass, "")
          fc = fcs.Next()


Thank you in advance,

Bogdan
0 Kudos
6 Replies
RDHarles
Regular Contributor
Here's a simple script that would do what you want.  It assumes you're using shapefiles.

import arcgisscripting, os
gp = arcgisscripting.create()

clippers = "c:/junk/clippers/"
toBeClipped = "c:/junk/toBeClipped/"
output = "c:/junk/toBeClipped/output/"

# Get a list of the "clippers"
for fc in os.listdir(clippers):
    if fc.endswith('.shp'):
        # Get a list of the files to be clipped.
        for shp in os.listdir(toBeClipped):
            if shp.endswith('.shp'):
                print "Clipping "+shp+" with "+fc
                # NOTE: add the name of the "clipper" onto the output name so it's unique.
                gp.Clip_analysis(toBeClipped+shp, clippers+fc, output+shp[:-4]+fc)
0 Kudos
bogdanpalade1
Deactivated User
Thank you very much for the code, it works perfectly. As I have a lot of data to be clipped, sometimes, when the output would be empty (there was no data to be clipped) I get the "Extent of the overlay operation will be empty. " error. Searching the forums I've seen that it was a common issue in the past for the ArcGIS 9.3 environment.
I'll try to put
gp.extent = "MAXOF" 
in the script to see if it works.

The funny thing is that sometimes it generates the empty shapefile (when there's no data to be clipped) but othertimes it raises the above mentioned error.

Thank you again for your help,

Bogdan
0 Kudos
RDHarles
Regular Contributor
I get the "Extent of the overlay operation will be empty. " error.


One way to avoid the script stopping on error (when you don't want it to) is to put the tool in a try/except like this.
It will print the error message in the "except:" part and just keep on going...

import arcgisscripting, os
gp = arcgisscripting.create()

clippers = "c:/junk/clippers/"
toBeClipped = "c:/junk/toBeClipped/"
output = "c:/junk/toBeClipped/output/"

# Get a list of the "clippers"
for fc in os.listdir(clippers):
    if fc.endswith('.shp'):
        # Get a list of the files to be clipped.
        for shp in os.listdir(toBeClipped):
            if shp.endswith('.shp'):
                print "Clipping "+shp+" with "+fc
                # NOTE: add the name of the "clipper" onto the output name so it's unique.
                try:
                    gp.Clip_analysis(toBeClipped+shp, clippers+fc, output+shp[:-4]+fc)
                except:
                    print "ERROR output emtpy: "+output

print "\nDone.\n"  

0 Kudos
bogdanpalade1
Deactivated User
Well,

In the end we decided that even if the clip result would be empty, we still would want to have a empty feature class with the attribute table present. We found that by putting the gp.extent to "MAXOF" it does just that.
Thank you for all your help
Bogdan
0 Kudos
SusanZwillinger
Frequent Contributor
I have a single shapefile that I need to clip multiple times with different shapefiles.  It seems like the code in this thread should work, but I tried the script and did not get any results.  No error messages, just nothing in the output folder.  How should I change the script to work with version 10 and my current workflow?
0 Kudos
RDHarles
Regular Contributor
I have a single shapefile that I need to clip multiple times with different shapefiles.  It seems like the code in this thread should work, but I tried the script and did not get any results.  No error messages, just nothing in the output folder.  How should I change the script to work with version 10 and my current workflow?


Susan,
It could be many things:
1.) Have you set up the paths to your shapefiles correctly?
2.) Are all the shapefiles in the same coordinate system?
* Whether you are using ARC 9.x or 10 shouldn't make any difference in this case.
** I'd recommend posting the exact code you are trying to use
0 Kudos