3

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
1
  • 1
    Why not just use one of the overlay tools such as Intersect or Union?
    – blah238
    Commented Mar 21, 2014 at 18:37

2 Answers 2

4

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
0
0
import arcpy

p1 = "C:/p1.shp" #Multiple polygons
p2 = "C:/p2.shp" #Single polygon

with arcpy.da.SearchCursor(p1, ("FID", "SHAPE@")) as p1_cur:                  
    with arcpy.da.SearchCursor(p2, "SHAPE@") as p2_cur:                   
        for r2 in p2_cur:
            for r1 in p1_cur:
                intersect = r2[0].intersect(r1[1], 4)
                arcpy.CopyFeatures_management(intersect, "C:/{0}.shp".format(r1[0]))
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.