3

Is there a way to automatically delete vertices in line features using a polygon file in ArcGIS so that it wouldn't cut up the line but just straightens the line inside the polygon?

The lines are lines and the box represents a polygon that covers them. I want to delete line vertices that are inside the polygon so the line would be straight inside the polygon boundary like on the illustration.

I use ArcGIS 10.1 but have access to other software also if something offers the solution.

Lines are lines, box is polygon.

2
  • 1
    As a new user please take the Tour. Would you be able to edit your question to include a picture of your before and after, please?
    – PolyGeo
    Oct 22, 2015 at 10:42
  • Do you have an Advanced license? The geoprocessing tools available will help out.
    – John
    Oct 28, 2015 at 23:28

2 Answers 2

6

The key is the difference method on geometry objects. The following code is far from efficient, since a new update cursor is opened for each polygon. But it has the added benefit of supporting multiple polygons on the same line:

import itertools, arcpy
arcpy.env.overwriteOutput = True
line_FC = r'<path to lines>'
poly_FC = r'<path to polygons>'
output = r'<path to result>'

#clone input lines to memory for fast processing
temp = arcpy.CopyFeatures_management(line_FC, "in_memory/temp")

polygons = arcpy.CopyFeatures_management(poly_FC, arcpy.Geometry())

for poly in polygons:
    with arcpy.da.UpdateCursor(temp, ["SHAPE@"]) as uCursor:
        for line in uCursor:
            line = line[0]
            diff = line.difference(poly)
            #if the two lines are not equal, that means it intersected the polygon
            if not line.equals(diff):
                #the result of geometry.difference() is a multipart line of only those
                #parts that lie outside the polyon
                parts = diff.getPart()

                #if parts is empty that means the line is completely within the polygon
                #i.e., no difference
                if parts:
                    #We'll need to "join" the end of part1 to the beginning of part2
                    #so we'll just flatten the list of lists
                    joined = list(itertools.chain.from_iterable(parts))

                    #and create a new polyline object to update the shape
                    poly_trimmed = arcpy.Polyline(arcpy.Array(joined))
                    uCursor.updateRow([poly_trimmed])

arcpy.CopyFeatures_management(temp, output)

I haven't tested it with edge cases, but this should get you started. enter image description here

1
  • Is there a way to do the same but not create vertex on intersections?
    – Losbaltica
    Feb 4, 2017 at 18:43
0

Here's where an Advanced license will be useful.

  1. Use the Clip tool to clip the "Line" feature class (FC) by the "Polygon" FC. Name the output "Lines_Inside".
  2. Use the Erase tool to do the opposite, and erase the lines that fall inside the polygons. Name the output "Lines_Outside".
  3. For the "Lines_Inside" FC, use the Feature Vertices to Points tool specifying the "BOTH_ENDS" option to create a point for the start and end node of each line. Create a field called "LineID" that uniquely identifies which line each start and end point belonged to (you can do this using a Join or Spatial Join or Selection or otherwise). Unfortunately I don't have an Advanced license in front of me to test the exact steps.
  4. Use the Points to Line tool on your results from Step #3 to create straight-line segments out of the start and end points of the original "Lines_Inside" FC. You will have to specify your "LineID" field you created in Step #3 for the "Line_Field" parameter.
  5. Merge/Append/Data Load the results of Step #4 to your "Lines_Outside" FC to get the two FCs back together.
  6. Use the Unsplit Line tool or Dissolve tool to merge the parts back together into single lines. You may have to do a Spatial Join to get the attributes back in the middle segments (which you generated) prior to running the tools.

Hope this helps. I can add screenshots when I'm able to access an Advanced license if you need them. The only other solution I can think of if these steps don't work would be to mess with the list of vertices in the arcpy.array object for each arcpy.polyline in your FC, which would be doable without the Advanced license but significantly harder without some Python skills.

Your Answer

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

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