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".

closed as off-topic by PolyGeo Jul 14 at 23:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
If this question can be reworded to fit the rules in the help center, please edit the question.

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.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

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