# Using ArcGIS Calculate Field with condition?

I have a point feature class which has two fields for two different sets of geographic coordinates (called "GPS_LON", "GPS_LAT" and "SHIFT_LON", "SHIFT_LAT"). I want to calculate the approximate metric distance between the two sets (assuming that 1 deg lat is ~111000 m). If I use the VB expression is all right. If I use the Python one, I get a syntax error at the first line:

degdist = math.sqrt( (( !SHIFT_LON! - !GPS_LON! )**2) + ( (!SHIFT_LAT! - !GPS_LAT! )**2))
mdist = degdist * 111000

The point of using Python is, that I need to set a condition: not all of my "GPS_LON", "GPS_LAT" were measured, thus I have zeros, and for those points with zeros I do not want to calculate the distance but rather have values.

That was my attempt, which also returns syntax errors(!):

if !GPS_LON! == 0:
mdist = None
else:
degdist = math.sqrt( (( !SHIFT_LON! - !GPS_LON! )**2) + ( (!SHIFT_LAT! - !GPS_LAT! )**2))
mdist = degdist * 111000
end if

I am still a Python novice.

• Look closely at this example. For example you need to define the function name, and not enlose the field names with !! inside the function.
– BERA
Oct 4, 2018 at 12:20
• Keep in mind that your Python function in the Pre-Logic Script Code block needs to be valid Python. The code which calls that function is where you pack in the magic characters. Oct 4, 2018 at 12:40
• Thanks for the hints. I have changed the code as following: def distance(MagDist): if GPS_LON == 0: MagDist = None else: degdist = math.sqrt ( SHIFT_LON - GPS_LON )**2) + ( SHIFT_LAT - GPS_LAT )**2)) MagDist = degdist * 111000 but I still get a syntax error at line 5. Oct 4, 2018 at 12:41
• No, that's still not correct. If you want to use multiple fields, you need to pass them into the function. Please be sure to Edit the question with any clarifications. Comments are illegible for that purpose (and therefore discouraged) Oct 4, 2018 at 12:45
• Be careful with indentation in Python. And Python has no end if clause. You have to remove it. Oct 4, 2018 at 13:03

In order to satisfy the syntax for Field Calculator, you need to define the function and input variables in the "Pre-Logic Script Code". The output field should be left out unless values in that field are part of the function. Therefore, your pre-logic script should be

def calc(gps_lon, gps_lat, shift_lon, shift_lat): if gps_lon == 0: return None else: deg_dist = math.sqrt( ((shift_lon - gps_lon)**2) + ((shift_lat - gps_lat)**2) ) return (degdist * 111000)

where your input variables are defined in the function, and your expression should be

calc(!GPS_LON!, !GPS_LAT!, !SHIFT_LON!, !SHIFT_LAT!)

where each of the fields that corresponds with an input variable is listed in the same variable position within the function. My use of calc as the function name is just habit, and it could be set to anything you want.

In general, the help documentation pages from ESRI provide a lot of the useful information for writing these scripts (as linked in the comments on OP). Esri Documentation

• Thanks, now I see where I was wrong. However I still get an error at this step when executing my model (ERROR 999999: Error executing function. Failed to execute (Calculate MagDist)). Oct 4, 2018 at 13:02
• is this tool part of a larger model? The dreaded 999999 Error doesn't provide us with much information, but the fact that its not a specific error relative to the field calculator leads me to believe its originating elsewhere. Oct 4, 2018 at 13:05