calaculate values in feild calculator ysing python

208
1
06-16-2022 08:05 AM
Labels (1)
matu89
by
New Contributor II

hello , i have 8 points with Z value .

each 2 points is a pair ( see the colors in the image attached).

i created a new feild calles "Delta" that in their i want the result of the difference between a pair..

for example the difference between point 2 to 1 is 4.66 meters..

calc2.jpg

 is there a way to do this via while or for loop ?

 

thanks for the help!

0 Kudos
1 Reply
JesseWickizer
Esri Contributor

Using the field calculator with a Python 3 expression, use this formula to populate the records with an even OBJECTID with the Delta between the current and previous Z value. 

Expression:

GetDelta(!OBJECTID!, !Z!)

Code Block:

previousZ = 0
def GetDelta(objectId, zValue):
    global previousZ
    # Don't calculate Delta for odd numbered OBJECTIDs
    if (objectId % 2 != 0):
        previousZ = zValue
        return None
    
    if (previousZ):
        #Get the absolute value of the difference
        difference = abs(zValue - previousZ)
    else:
        difference = 0
        #Update the previousZ global variable with the latest Z value
    previousZ = zValue
    return difference

This would have to be run on all records or an even number of records starting with an odd value.

0 Kudos