It is easy to identify the middle point, so the problem simplifies itself to where the mid_point vertex should be inserted.
You can capitalize in the fact that the mid_point intersects with at least one segment of the original line. The intersection can be checked with the geometry.disjoint (returns boolean) method. If it is False, the two features intersect:
import arcpy
# Create a polyline geometry
array = arcpy.Array([arcpy.Point(459111.6681, 5010433.1285),
arcpy.Point(472516.3818, 5001431.0808),
arcpy.Point(477710.8185, 4986587.1063)])
polyline = arcpy.Polyline(array)
############# This is the Code you are looking for, you can ignore the above
# Polyline.positionAlongLine returns PointGeometry object,
# but we need Point Object to add in our array later, hense I use the centroid method.
middle_point = polyline.positionAlongLine(0.5, True).centroid
# itterate between each segments
vertices = polyline.getPart(0)
for i in xrange(len(vertices) - 1):
tmp_line = arcpy.Polyline(arcpy.Array(
[vertices[i], vertices[i + 1]]))
# The point should be added between these to segments
if not tmp_line.disjoint(middle_point):
vertices.insert(i+1, middle_point)
break
# This is your new Geom to replace the old one
geom = arcpy.Polyline(vertices)
for v in geom.getPart(0):
print v
>> 459111.6681 5010433.1285 NaN NaN
>> 472341.908022479 5001548.25012586 NaN NaN # The new point has been inserted correctly
>> 472516.3818 5001431.0808 NaN NaN
>> 477710.8185 4986587.1063 NaN NaN
Edit: Didn't notice the OP did not have access to a critical method, so the following solution is not applicable anymore. But the Algorithm is cool, so I leave it here.
My solution would be to use geometry.segmentAlongLine method which returns a new polyline segment from the designated start until the finish.
So in a sense you build two lines: line_1 from start to mid, line_2 from mid to finish. You unpack the vertices for each line and then you repack them together including the middle vertex:
Here's an example:
import arcpy
# Create a polyline geometry
array = arcpy.Array([arcpy.Point(459111.6681, 5010433.1285),
arcpy.Point(472516.3818, 5001431.0808),
arcpy.Point(477710.8185, 4986587.1063)])
# The following represents the extracted geom Object
polyline = arcpy.Polyline(array)
leg_1 = polyline.segmentAlongLine(0, 0.5, True) # from 0% distance, to 50% distance (midway), use percentages
leg_2 = polyline.segmentAlongLine(0.5, 1.0, True)
arr = arcpy.Array()
for p in leg_1, leg_2:
for _ in p.getPart(0):
# the if bellow is so we won't include duplicate vertices:
# leg_1.last_point == leg_2.first_point
# With each additional vertex the comparison becomes more and more costly.
# Maybe find a better way to check if the new vertex is already in the Array?
if not any([x.equals(_) for x in arr]):
arr.add(_)
# This your new Geom to replace the old one
geom = arcpy.Polyline(arr)
for _ in geom.getPart(0):
print _