3

I need to obtain the x and y attributes of the second to last vertex for each segment and add to the attribute table using Arcpy (or some other python module).
To get the Y co-orodinate of the first vertex I can use !Shape.FirstPoint.Y!

arcpy.CalculateField_management(Flow_Paths, "Y_Cord_A", "!Shape.FirstPoint.Y!", "PYTHON_9.3", "")

In QGiS I can simply call Xat(-2) and Yat(-2) to get the second to last co-ordinate, is there an equivalent command for ArcPy?

2 Answers 2

5

Something like this should work for you at 10.0. The above link is for 10.1 arcpy.da cursors which are a little different.

rows = arcpy.SearchCursor(lyr)
for row in rows:
    geom = row.SHAPE
    for part in geom.getPart():
        for i, pnt in enumerate(part):
            if i == part.count - 2:
                print pnt.X, pnt.Y

The Working with geometry in Python topic in the ArcGIS 10.0 help should get you going.

Update: If you want to store this coordinate in fields in the feature class, just change it to an UpdateCursor and use it as described in Accessing data using cursors. Example:

rows = arcpy.UpdateCursor(lyr)
for row in rows:
    geom = row.SHAPE
    for part in geom.getPart():
        for i, pnt in enumerate(part):
            if i == part.count - 2:
                row.X = pnt.X
                row.Y = pnt.Y
                rows.updateRow(row)

Note that this only makes sense to do with singlepart features. If your lines have multiple parts, only the last part will get the values updated, e.g. last one in wins.

2

This snippet should get you started for getting vertex coordinates:

Reading polyline or polygon geometries Search cursor on a polygon or line feature class

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 {0}:".format(row[0]))
    partnum = 0

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

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

http://resources.arcgis.com/en/help/main/10.1/index.html#//002z0000001t000000

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.