Select to view content in your preferred language

help with python in feild calculator

1036
8
Jump to solution
06-18-2022 07:12 AM
matu89
by
Emerging Contributor

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..

matu89_0-1655561542109.jpeg

 

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

 

thanks for the help!

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

I hate that error so much... And it could have any number of reasons.

Try a different approach:

Calculate the field, switch language to Arcade, use the script below. Be sure to clear selections!

var partner_oid = $feature.OBJECTID
if($feature.OBJECTID % 2 == 0) {
    partner_oid -= 1
} else {
    partner_oid += 1
}
var partner = First(Filter($featureset, "OBJECTID = @partner_oid"))
if(partner == null) { return null }
return Abs(partner.Z - $feature.Z)

JohannesLindner_0-1655730878193.png

 


Have a great day!
Johannes

View solution in original post

8 Replies
DonMorrison1
Frequent Contributor

I would use a SearchCursor to read all of the Z  values into a python dict - indexed by the objectid. Then loop through the feature class with an UpdateCursor calculating the Delta value for each row and writing the result

0 Kudos
matu89
by
Emerging Contributor

Hey, i dont have a clue how to start writing the code...

can you please help me with that ?

thanks..!

0 Kudos
JohannesLindner
MVP Frequent Contributor

Open the Python window:

JohannesLindner_0-1655703668866.png

 

Copy the script below, paste it into the Python window, edit the first line, run it

 

fc_path_or_layer_name = "PointsWithZValues"
fields = ["OBJECTID", "Z", "Delta"]

z_dict = {r[0]: r[1] for r in arcpy.da.SearchCursor(fc_path_or_layer_name, fields)}
with arcpy.da.UpdateCursor(fc_path_or_layer_name, fields) as cursor:
    for oid, z, delta in cursor:
        # if OBJECTID is even, the partner oid is oid - 1
        # if OBJECTID is odd, the partner id is oid + 1
        partner_oid = oid + (-1 if oid % 2 == 0 else 1)
        delta = abs(z - z_dict[partner_oid])
        cursor.updateRow([oid, z, delta])

 

Here, I used DoubleField as Z and TextField as Delta:

JohannesLindner_1-1655703793068.png

 

!!! The script assumes that your screenshot shows exactly what your table looks like, especially that the pairs are always odd and even ObjectID. If this isn't the case, the script gets a little more complicated (but not much) !!!


Have a great day!
Johannes
0 Kudos
matu89
by
Emerging Contributor

hey ,

thanks for your response.

 

i tried to run the script, but it wont run..

here is the messege :

matu89_0-1655707158327.png

what went wrong?

thanks!

0 Kudos
JohannesLindner
MVP Frequent Contributor

It couldn't find OBJECTID 2.

Clear all selections and try again. If it still doesn't work, you need the slightly more complicated script.


Have a great day!
Johannes
0 Kudos
matu89
by
Emerging Contributor

this time i get the following mmessege :

 

matu89_0-1655727179617.png

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor

I hate that error so much... And it could have any number of reasons.

Try a different approach:

Calculate the field, switch language to Arcade, use the script below. Be sure to clear selections!

var partner_oid = $feature.OBJECTID
if($feature.OBJECTID % 2 == 0) {
    partner_oid -= 1
} else {
    partner_oid += 1
}
var partner = First(Filter($featureset, "OBJECTID = @partner_oid"))
if(partner == null) { return null }
return Abs(partner.Z - $feature.Z)

JohannesLindner_0-1655730878193.png

 


Have a great day!
Johannes
matu89
by
Emerging Contributor

that worked well!

thank you so much!

but it still interesting why the python code didnt work...

 

0 Kudos