ArcPy 10.0 – Iterating Through Shapefile1 Clip Shapefile2 to Shapefile1’s Polygons

arcgis-10.0arcmaparcpypython-2.7

I know that this question has been answered several times before, but for some reason I can't get this script to run… So I was hoping someone could point out where I'm going wrong.

I have two shapefiles, shapefile 1 has polygon boundaries and shapefile 2 has a single polygon that overlaps the firsts polygonal boundaries.

I would like to clip shapefile 2 according to each polygon of shapefile 1.

I have seen several sites and examples, but for some reason I must be overlooking a detail in getting this to work

Some of the examples I've seen are:
How to Batch Clip in ArcGIS Desktop 10 using Python/ArcPy?

Batch clip feature class to preserve area field

Clipping buffers to census tracts

import arcpy
arcpy.env.workspace = "path\\to\\workspace\\MaintenanceDivisions"
arcpy.env.overwriteOutput=True
#variables
md = "MaintenanceDivisions.shp"
out = arcpy.CreateUniqueName( "clipped.shp" ) 
mfp = "dissolved.shp"
try:
    #clip polygons from selected area
    rows = arcpy.SearchCursor(md)
    row = rows.next()
    for row in rows:
        feat = row.Shape
        arcpy.Clip_analysis([mfpd], feat, out, "")
        row = rows.next()   
except arcpy.ExecuteError as e:
    print(e)    
del rows

Best Answer

I can see your syntax is off in the clip command. I would take the following approach:

import arcpy, os

outws = r'C:\temp'
poly_single = r'C:\temp\poly_single.shp'  # The polygon to be clipped
poly_multi = r'C:\temp\poly_multi.shp'    # The clip features

rows = arcpy.SearchCursor(poly_multi)

count = 0     # Start a counter to name output polygons
for row in rows: # Loop through individual features of "poly_multi"
    out_poly = os.path.join(outws, "out_poly" + str(count))  # Assemble the output poly name and path
    arcpy.Clip_analysis(poly_single, row.Shape, out_poly)
    count = count + 1

# Clean up...Not necessary using "with" statement used in arcpy.da module 
del row
del rows
Related Question