ArcPy M-values – How to Set M-values to Cumulative Length of Line Using ArcPy

arcgis-10.7arcpygeometrylinear-referencingm-values

ArcMap 10.7.1 — Oracle 18c EGDB — SDE.ST_GEOMETRY


I have existing SDE.ST_GEOMETRY polylines that have M-values (aka 'measure values' for linear referencing purposes).

I want to update the M-values for all the lines via an ArcPy script.

In my case, the M-values of lines should be the same as the cumulative length of the line. In other words, if we look at the image below, the M-values should be the same as the length of the line at a given vertex.

enter image description here


Question:

Using ArcPy, is there a way to set the M-values to the cumulative length of the line?

Note: Some of the lines have arcs. And some of the lines are multi-part.


Related:

Python: Working with Feature Data using ArcPy [YouTube Video from ESRI]

Best Answer

I came up with a script seems to 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, "SHAPE@") as cursor:
        for row in cursor:
            geometry = row[0].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[0] = arcpy.Polyline(parts, spatial_reference)
            cursor.updateRow(row)

Notes:

Related Question