Select to view content in your preferred language

Creating line segments

485
6
08-06-2024 11:15 AM
CCWeedcontrol
Frequent Contributor

I have this polygon that I need to cut into pieces, 10 horizontal and 10 vertical blocks. My though was converting the polygon to polylines, (pic 1) then break the polyline into 4 parts,  then creating the 10 horizontal and 10 vertical lines, then converting those into polygons. The issue I am having is that when creating the segments, the west and south boundary have 20 segments.

Not know why 20 segments were being created for the west and south boundary. I did a arcpy.management.FeatureVerticesToPoints and that it was creating two segments for the west and south polylines (pic 2).

I am not sure how this can happen given that the polyline comes from the polygon.

 

 

 

import arcpy

# Define your workspace and input feature class
arcpy.env.workspace = r"C:\Temp\Sec.gdb"
input_fc = "Sec_1N2W30"
output_fc = "Polygons_Output"  # This will store the polygons
segment_fc = "Segmented_Polylines"  # This will store the segmented polylines

# Convert features to polygons
arcpy.management.FeatureToPolygon(input_fc, output_fc)

# Create an empty Polyline feature class for segmented polylines
arcpy.CreateFeatureclass_management(arcpy.env.workspace, segment_fc, "POLYLINE")

# Create a cursor to insert segmented polylines
with arcpy.da.InsertCursor(segment_fc, ["SHAPE@"]) as insert_cursor:
    # Iterate over each polygon in the output_fc
    with arcpy.da.SearchCursor(output_fc, ["SHAPE@"]) as search_cursor:
        for row in search_cursor:
            polygon = row[0]
            
            # Get the exterior ring of the polygon
            exterior_ring = polygon.boundary()

            # Iterate over each part of the exterior ring
            for part in exterior_ring:
                if len(part) < 2:
                    continue
                for i in range(len(part) - 1):
                    p1 = part[i]
                    p2 = part[i + 1]
                    line = arcpy.Polyline(arcpy.Array([p1, p2]))
                    length = line.length
                    segment_length = length / 10

                    # Create points at each segment interval
                    points = [p1]
                    for j in range(1, 10):  # 9 additional points to create 10 segments
                        segment_point = line.positionAlongLine(j * segment_length)
                        points.append(segment_point.firstPoint)
                    points.append(p2)

                    # Ensure no duplicate points by slightly adjusting them
                    unique_points = []
                    for point in points:
                        if not unique_points or unique_points[-1] != point:
                            unique_points.append(point)

                    # Create polylines from the unique points and insert them
                    for k in range(len(unique_points) - 1):
                        segment_line = arcpy.Polyline(arcpy.Array([unique_points[k], unique_points[k + 1]]))
                        insert_cursor.insertRow([segment_line])

print("Segmented polylines created successfully in", segment_fc)

 

 

 

0 Kudos
6 Replies
RPGIS
by
Frequent Contributor

Hi @CCWeedcontrol 

You can use the Grid Index Feature Tool to automatically create a grid over any feature. If this isn't what you are looking for then you might try the Fishnet tool.

CCWeedcontrol
Frequent Contributor

I did try that and create fishnet, but every time I run those the grid doesn't stay within the polygon. It seems to be rotated and not aligned.

 

In the attached picture, the tan is the original polygon and the red grid is what is created by the Grid Index Features tool, it doesn't follow the original polygon.

0 Kudos
RPGIS
by
Frequent Contributor
0 Kudos
CCWeedcontrol
Frequent Contributor

I have, I get the follow. The top and bottom row are offset for some reason. It could be because not a symmetrical square.

0 Kudos
RPGIS
by
Frequent Contributor

That is what your issue is. If you are looking for a perfected grid, then the grid to index is your best option. Otherwise, the subdivide polygon is the only tool that would get your polygon to divide as proportionately as possible.

0 Kudos
TonyAlmeida
Frequent Contributor

Ya, the Grid to index is not going to work.

0 Kudos