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

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?


0 Kudos
1 Reply
MVP Frequent 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!

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

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

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

Have a great day!
0 Kudos