# Using the field calculator to calculate EndX and EndY fields in a point feature class

597
1
08-25-2021 05:42 PM Occasional Contributor III

Hi all,

My scenario is I need to create a geodesic line feature from a table of coordinates which I have converted to a point feature class. The feature is described as having geodesic lines connecting each point. So if I run the basic 'points to lines' geoprocessing tool, this will not create the geodesic line feature I require.

I am familiar with creating geodesic lines through the Advanced Editing tools. I am also familiar with the 'XY to Line' geoprocessing tool which can create a geodesic line from my point feature class. What I require to create the geodesic line using 'XY to Line' tool are the following populated fields:

• StartX,
• StartY
• EndX,
• EndY

It is easy to calculate the StartX and StartY values by simply calculating the geometry in the field calculator; but how can I use the field calculator to insert the StartX+1 and StartY+1 values in the EndX and EndY fields, where "+1" represents the next row in the point feature class table?

Tags (3) by MVP Regular Contributor

In my experience, the Field Calculator is not a good tool for sequential calculations. For one, you can never be quite sure about the order in which the fields are calculated. And second, while it's easy to get the previous row's value, I have no idea how to get the next row.

This is a job for pure Python. Just edit this script and run it in the Python Window:

``````fc = "gewaesserkataster_sde.GWK_SDE_ADMIN.nullable"
fields = ["ObjectID", "SHAPE@", "StartX", "StartY", "EndX", "EndY"]  # first field determines the order the rows are calculated in!

data = [list(row) for row in arcpy.da.SearchCursor(fc, fields)]
data.sort(key=lambda d: d)

# calculate
for i in range(len(data)):
shp = data[i].firstPoint
data[i] = shp.X
data[i] = shp.Y
try:
next_shp = data[i+1].firstPoint
data[i] = next_shp.X
data[i] = next_shp.Y
except IndexError:  # last row doesn't have next_shp
data[i] = None
data[i] = None

# write
data = {d: d for d in data}
with arcpy.da.UpdateCursor(fc, fields) as cursor:
for row in cursor:
d = data[row]
cursor.updateRow(d)``````

Have a great day!
Johannes 