I have many polylines that represent the GPS derived route of a vehicle. I have created a layer with all route segments that meet a certain wind speed threshold (not all of these line segments touch), and have made an additional field with segment grouping values that I want to dissolve segments on (segments share group values based on the time they were generated). Unfortunately, when I dissolve segments together based on group value, the new polylines don't maintain the vertex order I need, as seen below:
Note the end vertex is on one of the middle lines; however, the vehicle was travelling West, so I need it to be on the far left segment. It seems that dissolve randomly chooses start/end line segments when creating the new vertex order. (Note, all original segments have correct vertex orders. I.e. for this image, each segment vertex ends on the Western vertex.)
I need to figure out if there is any way to control the vertex order of a dissolved polyline?
I have a time associated with each line segment that I can use to order them properly, I just am not sure how to apply this in dissolve. Context: I need a proper vertex order so that I can generate segment angle changes along the route.
Here is a link to some sample data (pre-dissolve). If you dissolve these segments by the "segment_group" field you will see the issue. https://github.com/Austin-Willoughby/Projects/blob/master/example_segments.zip
EDIT:
I've updated the sample data to include more segment group types. I've also written some code that utilizes the Array method @Hornbydd suggested and generated the segments seen below.
(Original segments are pink, generated segments are blue)
The newly generated lines seem to have the desired vertex order; however, the vertex coordinates seem to be off slightly. Additionally, the standalone segments (i.e. segment_group = 0 which shouldn't be combined with other lines) don't appear in the new lines. Perhaps the issue is that I try to create polylines from these single segments in the same manor as the multipart lines?
See code below:
python version 3.7.10, arcpy version 2.8
import arcpy
import os
from arcpy import env
arcpy.env.workspace= os.getcwd() + "\\Wind_Speed_Analysis.gdb"
# sort feature class by segment_group then startepoch (i.e. start time) for desired segment order
arcpy.Sort_management("sample_data", "sample_data_sort", [["segment_group", "ASCENDING"], ["startepoch", "ASCENDING"]])
fc = r"sample_data_sort"
fc_len = int(arcpy.GetCount_management(fc).getOutput(0))
# Enter for loop for each feature
with arcpy.da.SearchCursor(fc, ['SHAPE@','segment_group']) as cursor:
row_indx = 0
segments_wind_exceedance_merged = []
for row in cursor: #iterate through rows
segment_group = row[1]
# can't access prev row at line 0
if row_indx == 0:
polyline = []
prev_group = 0
for part in row[0]:
polyline.append(arcpy.Array(part))
segments_wind_exceedance_merged.append(arcpy.Polyline(arcpy.Array(polyline)))
# last row will always have the same segment_group directly above
elif row_indx == fc_len-1:
for part in row[0]:
polyline.append(arcpy.Array(part))
segments_wind_exceedance_merged.append(arcpy.Polyline(arcpy.Array(polyline)))
# if not first row, previous row can be used
elif row_indx > 0:
if segment_group == 0: # segment_group = 0 denotes standalone segments that will not be merged
polyline = []
prev_group = 0
for part in row[0]:
polyline.append(arcpy.Array(part))
segments_wind_exceedance_merged.append(arcpy.Polyline(arcpy.Array(polyline)))
# segment_group = 0 are stand alone lines. segment_group > 0 will occur in at least two rows for each group
elif segment_group > 0:
if segment_group != prev_group: # this will occur when loop accesses a new segment_group
if prev_group > 0:
segments_wind_exceedance_merged.append(arcpy.Polyline(arcpy.Array(polyline))) #append previous segments
polyline = [] # create empty polyline list for new segment
for part in row[0]:
polyline.append(arcpy.Array(part))
prev_group = segment_group
elif segment_group == prev_group:
for part in row[0]:
polyline.append(arcpy.Array(part))
prev_group = segment_group
row_indx+=1
arcpy.CopyFeatures_management(segments_wind_exceedance_merged, "Wind_Speed_Analysis.gdb\sample_polylines")