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.

enter image description here

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[1].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)
                partNum += 1
            row[1] = arcpy.Polyline(partArray, spatialRef)

How can the script be improved?


1 Answer 1


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

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

  • row[1].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[1].densify("ANGLE", 10000, 0.174533)

            parts = arcpy.Array()
            for part in geometry:
                points = arcpy.Array()
                for point in part:
                    point.M = geometry.measureOnLine(point)
            row[1] = arcpy.Polyline(parts, spatial_reference)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.