What is the python expression for the second, third, fourth etc etc vertex along a line feature?

546
2
08-13-2019 06:22 AM
deleted-user-iYZsZYnVovgT
New Contributor

What is the python expression for the second, third, fourth etc etc vertex along a line feature? !shape.firstpoint¡ is the first vertex and !shape.lastpoint¡ is the last vertex, but what's the expression for other vertices down the line?

0 Kudos
2 Replies
LukeWebb
Occasional Contributor III

Theres no specific way to do this, this is likely a design decision, as features can have theoretically unlimited vertexes so loading all this info to populate a dictionary etc, could be expensive and have potential to massively slow down cursors. 

Check out this article, that tells you the exact same thing I could but using many more words and much more coherently:

Reading geometries—ArcPy Get Started | ArcGIS Desktop 

The code at the bottom should allow you to do as you require.

import arcpy

infc = arcpy.GetParameterAsText(0)

# Enter for loop for each feature
#
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current multipoint's ID
    #
    print("Feature {}:".format(row[0]))
    partnum = 0

    # Step through each part of the feature
    #
    for part in row[1]:
        # Print the part number
        #
        print("Part {}:".format(partnum))

        # Step through each vertex in the feature
        #
        for pnt in part:
            if pnt:
                # Print x,y coordinates of current point
                #
                print("{}, {}".format(pnt.X, pnt.Y))
            else:
                # If pnt is None, this represents an interior ring
                #
                print("Interior Ring:")
        partnum += 1p
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

 

JoshuaBixby
MVP Esteemed Contributor

I agree with Luke that cursors are best when working with more in-depth properties or methods of geometries.  If you must work with the Field Calculator, the "Code samples -- geometry" section of Calculate Field examples—Help | ArcGIS Desktop shows how you would retrieve the geometry and then process it in a code block.