7

I have a series of polyline shapefiles and I am trying to automate the process of calculating the X and Y coordinates of the line start and line end using Python.

I have added the 4 fields (startx, starty, endx, endy) in Python with the following code:

Arcpy.addfield_management ("Polyline","startx","DOUBLE")

I can perform this task by right-clicking the attribute and calculating geometry, but I want to automate this process for about 30 polylines.

I have the following code I've found that might be able to perform the desired task with some modification:

xExpression = "float(!SHAPE.CENTROID!.split()[0])"
yExpression = "float(!SHAPE.CENTROID!.split()[1])"

arcpy.CalculateField_management("Polyline", "startx", xExpression, "PYTHON")
arcpy.CalculateField_management("Polyline", "starty", yExpression, "PYTHON")

This code only calculates the coordinate at the centre of the shape I believe. Are their any expressions I can add to this code to calculate the X and Y coordinates of the start and end points of the polyline?

I am using ArcGIS 10.1 with ArcInfo license.

3
  • I would start here with ESRI's discussion of arcpy Point object; code snipped at end of page is very much what you want esri help Commented Feb 28, 2014 at 13:44
  • Are these simple polyline features or complicated, braching features?
    – Aaron
    Commented Feb 28, 2014 at 13:54
  • All polyline features are simple lines with one start and one end, no branching involved.
    – JC11
    Commented Feb 28, 2014 at 14:12

4 Answers 4

14

I agree with Barbarossa that accessing the power of the da module would be beneficial. Here is very clean scripting approach:

import arcpy

fc = r'C:\Users\OWNER\Documents\ArcGIS\Default.gdb\samplePolyline'

fields = ['x1','x2','y1','y2']

# Add fields to your FC
for field in fields:
    arcpy.AddField_management(fc,str(field),"DOUBLE")

with arcpy.da.UpdateCursor(fc, ('x1','x2','y1','y2', "SHAPE@")) as cursor:
    for row in cursor:
        row[0] = row[4].firstPoint.X
        row[1] = row[4].lastPoint.X
        row[2] = row[4].firstPoint.Y
        row[3] = row[4].lastPoint.Y
        cursor.updateRow(row)
1
  • In my case, I had to add row = list(row) after the for row in cursor: line in order to not get a runtime error on the cursor.updateRow(row) line.
    – spaine
    Commented Feb 17, 2017 at 0:05
10

The field calcuation expression to do this is:

Start of line:

"!Shape!.positionAlongLine(0.0,True).firstPoint.X"
"!Shape!.positionAlongLine(0.0,True).firstPoint.Y"

End of line:

"!Shape!.positionAlongLine(1.0,True).firstPoint.X"
"!Shape!.positionAlongLine(1.0,True).firstPoint.Y"
3
  • I'm having problems with ArcGIS10 and this expression: "Error:The row contains a bad value." I'm trying to do it in a Polyline shapefile. Any suggestion?
    – juasmilla
    Commented Jun 2, 2014 at 15:44
  • 1
    @juasmilla - hopefully you already found a solution, but for other 10.0 users, you can use: "!shape.firstPoint.X!"
    – Cyrus
    Commented Jun 12, 2014 at 4:32
  • 4
    Just wish to point out that starting at ArcGIS 10.2, this can easily been done using the LINE_START_MID_END geometry properties in the Add Geometry Attributes tool. This tool is available for all license level. Commented Jun 30, 2014 at 15:39
4

I would loop through each polyline with arcpy.da.SearchCursor, reading the "SHAPE@" field. Then read the firstPoint and lastPoint from the polyline geometry.

3

I don't have experience coding but i modified one of the esri resources examples and it worked for me...

# Name: CalculateField_COORDINATES.py
# Description: Use CalculateField to calculate start and end points of a line

# Import system modules
import arcpy

try:
  # Set environment settings
  arcpy.env.workspace = "D:\SIG_MPIOS\MPIOS14.mdb"

  # Set local variables
  inFeatures = "RED_DISTRIBUCION"
  fieldName1 = "X_INI"
  fieldName2 = "X_FIN"
  fieldName3 = "Y_INI"
  fieldName4 = "Y_FIN"
  fieldPrecision = 18
  fieldScale = 11

  # Add fields
  arcpy.AddField_management(inFeatures, fieldName1, "DOUBLE",
                          fieldPrecision, fieldScale)
  arcpy.AddField_management(inFeatures, fieldName2, "DOUBLE",
                          fieldPrecision, fieldScale)
  arcpy.AddField_management(inFeatures, fieldName3, "DOUBLE",
                          fieldPrecision, fieldScale)
  arcpy.AddField_management(inFeatures, fieldName4, "DOUBLE",
                          fieldPrecision, fieldScale)

  # Calculate centroid
  arcpy.CalculateField_management(inFeatures, fieldName1,
                                "!shape.firstpoint.x!",
                                "PYTHON_9.3")
  arcpy.CalculateField_management(inFeatures, fieldName2,
                                "!shape.firstpoint.y!",
                                "PYTHON_9.3")
  arcpy.CalculateField_management(inFeatures, fieldName3,
                                "!shape.lastpoint.x!",
                                "PYTHON_9.3")
  arcpy.CalculateField_management(inFeatures, fieldName4,
                                "!shape.lastpoint.y!",
                                "PYTHON_9.3")
except Exception as e:
  # If an error occurred, print line number and error message
  import traceback
  import sys
  tb = sys.exc_info()[2]
  print("Line {0}".format(tb.tb_lineno))
  print(e.message)

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.