# Understanding ArcGIS field calculator behaviour with geometries?

In Getting Properties of Next Record Using Calculate Field Tool in ModelBuilder? the OP asks for suggestion on calculation of distances between pairs of points in a feature class.

I thought of alternative solution using field calculator with a very basic logic:

• for first record memorise point geometry from Shape field into global variable pG and return negative distance
• for other records calculate distance between current Shape and one in memory, assign pG = current Shape, return distance.

Expression:

``````i=0
def FirstOrOthers(shp):
global pG,i
if i==0:  i, dist, pG = 1, -1, shp
else: dist, pG=shp.distanceTo(pG),shp
return dist
#------
FirstOrOthers( !Shape! )
``````

Output shown in field DISTANCE: When I changed expression slightly, to memorise first point of point geometry:

``````i=0
def FirstOrOthers(shp):
global pG,i
if i==0:  i, dist, pG = 1, -1, shp.firstPoint
else: dist, pG=shp.distanceTo(pG),shp.firstPoint
return dist
#------
FirstOrOthers( !Shape! )
``````

It worked as expected: It means that field calculator refuse to compute distance between 2 point geometries, but does it for point geometry and a point. This is risky, because point does not have spatial reference defined. This is why in scripts I always use 2 point geometries to compute distance between 2 points.

Any thoughts on that odd behaviour of field calculator?

I tested using ArcGIS Desktop 10.3.

• thanks for mentioning this issue, but aren't we back to your first post ? I do not understand what you expect from your question apart from the odd facts that you are pointing out. But maybe an more robust workaround would be to compute the length of the line segment that joins the two points. Aug 21 '17 at 9:39
• I don't expect much. It is more like a warning to others who use calculator, because instead of expected 5 minutes, I spent 30 attempting to find flaw in this tiny script Aug 21 '17 at 9:53
• Then I suggest that you rewrite your post in a question/answer structure, like "how to use field calculator to compute distance to a point geometry" with your "non working" example in the question, and the working solution in the answer. Aug 21 '17 at 10:05
• I agree with @radouxju that posting "a warning to others" as a question and not expecting much in the way of an answer is not conducive to focused Q&A whereas structuring it as a self-answered Q&A would be fine. That way it is far less likely to add another one to our 19,000+ unanswered questions.
– PolyGeo
Aug 22 '17 at 23:35
• @PolyGeo Will do it tomorrow Aug 23 '17 at 4:12

Further experiment with expression:

``````i=0
def FirstOrOthers(shp):
global pG,i
if i==0:  i, pG = 1, shp; result=-1
else:  result= pG.firstPoint.X; pG = shp
return result
#---------
FirstOrOthers(!Shape!)
``````

demonstrates issues with storing point geometrty in memory. Note that X coordinate returned is coordinate of current geometry instead of (as intended !) one stored in memory: This the weirdest thing I ever saw in years of coding.