3

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:

enter image description here

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:

enter image description here

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.

  • 1
    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. – radouxju 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 – FelixIP Aug 21 '17 at 9:53
  • 1
    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. – radouxju 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
  • 1
    @PolyGeo Will do it tomorrow – FelixIP Aug 23 '17 at 4:12
1

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:

enter image description here

This the weirdest thing I ever saw in years of coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.