2

I want to clip a polyline feature class by a polygon feature class (buffers created from points). Several polygons overlap, so I want to get each polygon feature clipped with each polyline feature.

Using the Clip_analysis tool, I get the orange lines: enter image description here

whereas I want the following red lines:

enter image description here

In a python script, I tried the following code to clip every polyline with each polygon:

        temp = arcpy.ValidateTableName("clip_temp")
    bufferCursor = arcpy.SearchCursor(buffer)
    n = 0
    for pRow in bufferCursor:
        arcpy.AddMessage(n)
        feat = pRow.Shape
        arcpy.Clip_analysis(inputPolylineLayer, feat, temp, "#")
        if n == 0:
            arcpy.Copy_management(temp, clip)
        else:
            arcpy.Append_management(temp, clip)
        n=n+1

But the execution is very very slow and it crashes before finishing.

I also tried the Intersect_analysis tool but I didn't get the right geometries: enter image description here

Do you have any idea of a better methods to do it?

1

For a lazy solution (without coding), try with the split(analysis) tool followed with the merge tool. The split tools make one clip for each feature in your buffer. You can then merge them all, just make sure that you map your field so that you don't duplicate them.

Note that your results with intersect are strange : maybe you should check the tolerance used with the tool or inside you geodatabase.

  • Unfortunately, I don't have any ArcInfo licence so I couldn't use the Split tool. – superrache May 15 '14 at 14:24
  • And yes intersect results are stranges. I used default tolerances in my geodatabase: 0,001 meters and didn't specify any tolerance in the Intersect tool. – superrache May 15 '14 at 14:26
  • Ok, then I recommend that you use an insertcursor instead of copy and append to create your clipped feature. – radouxju May 15 '14 at 14:28
  • The problem of slowness and crash is caused by the line arcpy.Clip_analysis(inputPolylineLayer, feat, temp, "#"). I just commented the Copy and Append instruction to see it. So, using an insertCursor is not solution. I also saw that my polyline feature class was in WGS84 instead of Lambert1993 (metric projection system), so the tolerance problem in Intersect comes from there. I changed the projection and Intersect runs right! It just remains one problem: the result is composed of one line for the intersection with just the polygon A, one for the polygon B and one for the insection of A/B. – superrache May 15 '14 at 15:00
  • this is probably the case, but you need to initialise temp as temp = arcpy.Geometry(). Also, your crash with the clip may be due to buffers without intersection. You could try with "if temp: " which would only make a copy when temp is non null. Finally, for the speed, insertCursor would be faster. – radouxju May 15 '14 at 18:26
1

Here's one that I did before, the script is called "Clip for each Polygon". It will create a shape file for each input polygon. It was originally for extracting contours to a 1k grid to be exported to DXF (AutoCAD couldn't handle the number of geometries, apparently they hadn't heard of spatial indexing).

It is 9.3 but would easily translate to ArcPy if required; scripts using 9.x geoprocessor still run in v10.x. License level: Basic.

import sys, string, os, arcgisscripting

# Create the Geoprocessor object
gp = arcgisscripting.create()

InClip = sys.argv[1] # What you're clipping with
InData = sys.argv[2] # your polygons
OutNameBase = sys.argv[3] # A base name, like clip will do.

ClipCur = gp.SearchCursor(InClip)
Clip = ClipCur.Next()

while Clip:
    print "[objectid] = " + str(Clip.fid)
    gp.MakeFeaturelayer_management(InClip,"ThisClip",'\"fid\" = ' + str(Clip.fid))
    cnt = gp.GetCount_management("ThisClip")
    print "Clip" + InData + " to " + os.getcwd() + "\\" + OutNameBase + "_" +  str(Clip.fid) + ".shp"
    gp.Clip_analysis (InData,"ThisClip",os.getcwd() + "\\" + OutNameBase + "_" +  str(Clip.fid) + ".shp")
    gp.delete("ThisClip")
    Clip = ClipCur.Next()

The results will end up in the folder you are calling the script from, you might want to change that, it worked fine for me at the time.

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.