Sunday, 1 July 2018

arcgis desktop - Calculating X and Y of line start and line end using ArcPy?



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.



Answer



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"

No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...