9

How do I retrieve start and end point coordinates with ArcPy for a polyline feature class?

I expect to pass a segment identifier to a subroutine and have it pass back start and end coordinates. The Field Calculator method doesn't work for me, because I need the value for other calculations that can't be performed within it. (I also prefer not to change the data to store these coordinates as attributes.) I am attempting to calculate address ranging for a "center out" addressing scheme. The address value depends on the distance to the "county center".

3 Answers 3

15

The process for this seems to have changed between ArcGIS 10.0 and 10.1. I will include a sample for both.

Here is the help document on reading geometries in 10.1 using arcpy: Reading Geometries 10.1
This document discusses the parameters for a Polyline geometry type: Polyline (arcpy)

10.1

import arcpy

infc = arcpy.GetParameterAsText(0)

# Enter for loop for each feature
#
for row in arcpy.da.SearchCursor(infc, ["OID@", "SHAPE@"]):
    # Print the current line ID

    print("Feature {0}:".format(row[0]))

    #Set start point
    startpt = row[1].firstPoint

    #Set Start coordinates
    startx = startpt.X
    starty = startpt.Y

    #Set end point
    endpt = row[1].lastPoint

    #Set End coordinates
    endx = endpt.X
    endy = endpt.Y

10.0

Here is the help document on reading geometries in 10.0 using arcpy: Reading Geometries 10.0
This document discusses the parameters for a Geometry object: Geometry

import arcpy

infc = arcpy.GetParameterAsText(0)

# Identify the geometry field
#
desc = arcpy.Describe(infc)
shapefieldname = desc.ShapeFieldName

# Create search cursor
#
rows = arcpy.SearchCursor(infc)

# Enter for loop for each feature/row
#
for row in rows:
    # Create the geometry object
    #
    feat = row.getValue(shapefieldname)

    # Print the current line ID
    #
    print "Feature %i:" % row.getValue(desc.OIDFieldName)

    #Set start point
    startpt = feat.firstPoint

    #Set Start coordinates
    startx = startpt.X
    starty = startpt.Y

    #Set end point
    endpt = feat.lastPoint

    #Set End coordinates
    endx = endpt.X
    endy = endpt.Y

The difference between the two basically lies in how you access the feature geometry. There have been some shortcuts added in 10.1 to make it easier to get to the geometry object.

6

I have done this before and prefer to use a Search Cursor and read the geometry. It creates a loop and allows you to do calcs on each shape.

inFeatures = "Feature"
shapeName = arcpy.Describe (inFeatures).shapeFieldName
rows = arcpy.SearchCursor(inFeatures)
for row in rows:
    feat = row.getValue(shapeName)
    xy1 = feat.firstPoint
    xy2 = feat.lastPoint

That loop allows you to add some calculations with it and go shape by shape.

There is some extra help in the Help: Working with geometry in Python

1

You should be able to use the firstPoint and lastPoint properties of the Polyline object.

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