Derive Min/Max Measure from PolylineM

2313
2
08-30-2013 11:32 AM
StuartBlumberg
New Contributor II
Hi,

I am trying to derive the minimum and maximum measure for each segment from a PolylineM feature.  In the past I used the below VB code from an old colleague in ArcMap 9.3.1 which worked perfectly.  I am now trying to do this in Model Builder in 10.1, however, I understand 10.1 does not support VB codes anymore.  I am a bit weak on coding so maybe someone can help me rework this code in Python?  Or if they know a better way to do this?  Thanks!

VB Code:

Dim pGeometry As IGeometry
Dim dMMax As Double
Dim pMAware As IMAware
Dim pMColl As IMCollection
If (Not IsNull([Shape])) Then
  Set pGeometry = [Shape]
  If (Not pGeometry.IsEmpty) Then
    Set pMAware = pGeometry
    If pMAware.MAware Then
      Set pMColl = pGeometry
      dMMax = pMColl.MMax
    End If
  End If
End If
Tags (2)
0 Kudos
2 Replies
XanderBakker
Esri Esteemed Contributor
Hi Stuart,

Below you will find a snippet of Python code that can be used to calculate the maximum M for each feature in a featureclass.
It is a standalone script that can be executed from your Python IDE of choice. Before you do, you should change the location/name of the featureclass used and the name of the (existing) field where the result will be written.

In short it will:

  • access your featureclass

  • loop through the features

  • calculate per feature the maximum M, using GetMmax()

  • update the feature (row)



def main():
    import arcpy

    # settings, replace with your own dataset and existing column name:
    FClassOrTable = r'C:\Project\_tmp\test.gdb\Mawares'
    outputField = 'MMax'

    # .. or make variable
    # FClassOrTable = arcpy.GetParameterAsText(0)
    # outputField = arcpy.GetParameterAsText(1)

    # update featureclass
    fields = ['SHAPE@',outputField]
    with arcpy.da.UpdateCursor(FClassOrTable, fields) as cursor:
        for row in cursor:
            row[1] = GetMmax(row[0])
            cursor.updateRow(row)
        del row
        del cursor

def GetMmax(geom):
    # get the max(M) from a geometry
    mmax = -9999
    for part in geom.getPart():
        for point in part:
            m = point.M
            if m > mmax:
                mmax = m
    return mmax

if __name__ == '__main__':
    main()



You can also add the script to a toolbox. The tool can eventually be used in modelbuilder in case you want to combine it with additional tools. In case you want to create a tool from it, you should uncomment the "arcpy.GetParameterAsText()" lines and define the parameters properly.

  • first: the location/name of the featureclass (Type: Feature Layer)

  • second: the field that will store the max M (Type: Field, obtained from the featureclass, and apply a fields filter to only show the double or floating fields).


Links to additional information:
http://resources.arcgis.com/en/help/main/10.1/index.html#/UpdateCursor/018w00000014000000/


Cheers,

Xander
0 Kudos
JamesGraham
New Contributor III
    with arcpy.da.UpdateCursor(FClassOrTable, fields) as cursor:  
        for row in cursor:  
            row[1] = GetMmax(row[0]) 

Instead of using a function to breakdown that Mmax value, you could also just use the extent object, which comes as part of the Shape@.  Like this:

    with arcpy.da.UpdateCursor(FClassOrTable, fields) as cursor:  
        for row in cursor:  
            row[1] = row[0].extent.MMax
0 Kudos