Hi, I'm trying create 2 new line feature classes based on just the first vertex to vertex line segment and last vertex to vertex segment of an existing line feature class. This is for calculating upstream and downstream gravity main horizontal angles for manhole turbulence modelling. Some of the existing lines are a single straight segment (just 2 vertices), but most have multiple vertices. I've already densified the true curves and none of the lines are multipart. If the new upstream and downstream line segments are identical for straight lines, that is ok. I will be joining the calculated horizontal angle back to new upstream and downstream horizontal angle fields in the original gravity main line feature class.
How do I return the coordinate geometry of the first and second vertex in one pass, and then the last and second to last vertex in another pass using Python in the ArcPy pane in ArcGIS Pro (3.1). Everything I've seen only returns the first and last, or the centroid coordinates of the feature. Can I use some sort of indexing of the vertices (i.e: [0] and [1], then [-1] and [-2])?
Here is the code I have so far that returns ALL of the vertex coordinates for each row and part. I need to create the upstream and downstream array objects from the 2 coordinates at each end.
# Python script to create upstream and downstream line segments
# based on the first and last segment of lines with multiple vertices
import arcpy
# Set your workspace and feature class paths
workspace = r"C:\path\to\your\geodatabase.gdb"
input_feature_class = "your_input_feature_class"
output_upstream_feature_class = "upstream_lines"
output_downstream_feature_class = "downstream_lines"
# Set Unique ID field name from input feature class
ID = "CSDFeatureID"
# Get the spatial reference of the input feature class
spatial_reference = arcpy.Describe(input_feature_class).spatialReference
# Create feature classes with the same spatial reference
arcpy.CreateFeatureclass_management(workspace, output_upstream_feature_class, "POLYLINE", spatial_reference=spatial_reference)
arcpy.CreateFeatureclass_management(workspace, output_downstream_feature_class, "POLYLINE", spatial_reference=spatial_reference)
# Add Facility ID field to the result feature classes
arcpy.AddField_management(output_upstream_feature_class, ID, "TEXT")
arcpy.AddField_management(output_downstream_feature_class, ID, "TEXT")
# Start an edit session
edit = arcpy.da.Editor(workspace)
edit.startEditing(False, True)
edit.startOperation()
# Open a search cursor to iterate through the input feature class
with arcpy.da.SearchCursor(input_feature_class, [ID, "SHAPE@"]) as cursor:
for row in cursor:
print("Line Feature {}:".format(row[0]))
for part in row[1]:
for pnt in part:
print("Vertex coordinates: {}, {}".format(pnt.X, pnt.Y))
# Get the first two vertex coordinates and store in upstream_array
# Get the last two vertex coordinates and store in downstream_array
# Create new features from the upstream and downstream array coordinates
with arcpy.da.InsertCursor(output_upstream_feature_class, ["SHAPE@", ID]) as up_cursor:
up_line = arcpy.Polyline(upstream_array)
up_cursor.insertRow((up_line, line_id))
with arcpy.da.InsertCursor(output_downstream_feature_class, ["SHAPE@", ID]) as down_cursor:
down_line = arcpy.Polyline(downstream_array)
down_cursor.insertRow((down_line, line_id))
# Stop the edit session
edit.stopOperation()
edit.stopEditing(True)
print("Script completed.")