# Loop through polyline vertices and update coordinate

I have a Python 2.7 script that loops through GIS polylines and updates a coordinate.

The coordinate is called an "M" coordinate (aka a "Measure-value"). M coordinates are similar to X and Y coordinates, but are used for specialized linear referencing purposes. import arcpy
connection = "Database Connections\my_conn.sde"
fc = connection + "\my_owner.my_fc"
spatialRef = arcpy.Describe(fc).spatialReference

with arcpy.da.Editor(connection) as editSession:
with arcpy.da.UpdateCursor(fc, ["ASSET_ID", "SHAPE@"]) as cursor:
for row in cursor:
feature = row.densify ("ANGLE", 10000, 0.174533)
partNum = 0
partArray = arcpy.Array()
for part in feature:
pointArray = arcpy.Array()
n = len(part)
for i in range(n):
point = part.getObject(i)
point.M = feature.measureOnLine(point)
pointArray.append(point)
partArray.append(pointArray)
partNum += 1
row = arcpy.Polyline(partArray, spatialRef)
cursor.updateRow(row)


How can the script be improved?

Unfortunately, I have neither Python 2.7 nor some test data.

However, I would like to point out a few things:

• row.densify("ANGLE", 10000, 0.174533) returns a geometry (object), you may want to rename the variable to geometry
• partNum is not really used, you may want to get rid of it altogether
• There is no need to use range(n) since part is an array (arcpy.Array) and iterable
• Personally, I would call partArray just parts and pointArray just points (and use snake_case for variable names following the PEP8 style guide)

Below an example of the script with the suggested improvements. Note: Script is not tested but should work.

import arcpy

connection = "Database Connections\my_conn.sde"
feature_class = connection + "\my_owner.my_fc"

spatial_reference = arcpy.Describe(feature_class).spatialReference

with arcpy.da.Editor(connection) as edit_session:
with arcpy.da.UpdateCursor(feature_class, ["ASSET_ID", "SHAPE@"]) as cursor:

for row in cursor:

geometry = row.densify("ANGLE", 10000, 0.174533)

parts = arcpy.Array()
for part in geometry:
points = arcpy.Array()
for point in part:
point.M = geometry.measureOnLine(point)
points.append(point)
parts.append(points)

row = arcpy.Polyline(parts, spatial_reference)

cursor.updateRow(row)