Python - Start and End Coordinates of a line

4387
2
06-24-2011 06:00 AM
ManojBanga
New Contributor
Hi all!

I am looking for a python script to:

Populate startX, startY, startZ, endX, endY and endZ of a polyline shapefile using FIELD CALCULATOR

Thanks
Manoj
Tags (2)
0 Kudos
2 Replies
RDHarles
Occasional Contributor
You can get an XY pair (start and end) by doing this:

arcpy.CalculateField_management("Line.shp", "FromXY", "(!SHAPE.firstpoint!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "ToXY", "(!SHAPE.lastpoint!)", "PYTHON")
To get the Z value you need, you'd probably want to do something more like this:

arcpy.CalculateField_management("Line.shp", "FromX", "(!SHAPE.firstpoint.X!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "FromY", "(!SHAPE.firstpoint.Y!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "FromZ", "(!SHAPE.firstpoint.Z!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "ToX", "(!SHAPE.lastpoint.X!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "ToY", "(!SHAPE.lastpoint.Y!)", "PYTHON")
arcpy.CalculateField_management("Line.shp", "ToZ", "(!SHAPE.lastpoint.Z!)", "PYTHON")
0 Kudos
ManojBanga
New Contributor
Thanks Harles for such a quick and correct response.

The code works fine but with a small tweak. Instead of PYTHON we need to say PYTHON_9.3 only individual coordinates. see below:

arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "StartX", "(!SHAPE.firstpoint.X!)", "PYTHON_9.3")
arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "StartY", "(!SHAPE.firstpoint.Y!)", "PYTHON_9.3")
arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "StartZ", "(!SHAPE.firstpoint.Z!)", "PYTHON_9.3")


arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "ToX", "(!SHAPE.lastpoint.X!)", "PYTHON_9.3")
arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "ToY", "(!SHAPE.lastpoint.Y!)", "PYTHON_9.3")
arcpy.CalculateField_management("C:\GIS\ThreeD.shp", "ToZ", "(!SHAPE.lastpoint.Z!)", "PYTHON_9.3")
0 Kudos