Hi,
I'm interested in calculating the start and end points of a line in the field calculator. I've researched several boards and I cannot come up with code that successfully calculates the geometry. Does anyone have any ideas? Thanks!
Solved! Go to Solution.
This is an older thread, but since I just had to do this for one of my scripts, I thought I would share the code I used. In my case, I already created a fishnet, as polylines that are clipped by a study area, and then converted the "multipart" segments to "singlepart" (so each segment in separate) ...but it should be able to work with any line. I addfields called x1, y1, x2, y2 for my needs...but you can name whatever works for you.
fishnetFC = r"C:\Prep.gdb\FishnetIntersect_L_single" ptFields = [["X1", "!SHAPE.firstPoint.X!"], ["Y1", "!SHAPE.firstPoint.Y!"], ["X2", "!SHAPE.lastPoint.X!"], ["Y2", "!SHAPE.lastPoint.Y!"]] for field in ptFields: print("adding field {0}...".format(field[0])) arcpy.AddField_management(fishnetFC, field[0], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") print(" calcing field {0} to be {1}".format(field[0], field[1])) arcpy.CalculateField_management(fishnetFC, field[0], field[1], "PYTHON_9.3" )
In any case Jessica, you should mark helpful comments as such, and either mark this as "assumed answered" or the answer that best solved your issue.
It is built-in as a tool, open your table and check out Calculate field tool options, one of which allows you to calculate polyline properties
Hi Jessica,
You could add a new field in attribute table and use Calculate Geometry to get the desired geometric properties.
At 10.0 and above you have to use Python to do geometry calculations with the Field Calculator. The calculation to extract the Start point coordinates is:
!Shape.FirstPoint.X! (or Y, Z, or M)
The End point coordinates is similar:
!Shape.LastPoint.X! (or Y, Z or M)
See my responses in this thread for more geometry options.
This is an older thread, but since I just had to do this for one of my scripts, I thought I would share the code I used. In my case, I already created a fishnet, as polylines that are clipped by a study area, and then converted the "multipart" segments to "singlepart" (so each segment in separate) ...but it should be able to work with any line. I addfields called x1, y1, x2, y2 for my needs...but you can name whatever works for you.
fishnetFC = r"C:\Prep.gdb\FishnetIntersect_L_single" ptFields = [["X1", "!SHAPE.firstPoint.X!"], ["Y1", "!SHAPE.firstPoint.Y!"], ["X2", "!SHAPE.lastPoint.X!"], ["Y2", "!SHAPE.lastPoint.Y!"]] for field in ptFields: print("adding field {0}...".format(field[0])) arcpy.AddField_management(fishnetFC, field[0], "DOUBLE", "", "", "", "", "NULLABLE", "NON_REQUIRED", "") print(" calcing field {0} to be {1}".format(field[0], field[1])) arcpy.CalculateField_management(fishnetFC, field[0], field[1], "PYTHON_9.3" )
In any case Jessica, you should mark helpful comments as such, and either mark this as "assumed answered" or the answer that best solved your issue.
I have marked the answer of Rebecca as the correct answer since it was place in Python snippets and code was requested, although the other answers are very good ones too, since you don't need code to do this.