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.
1 Answer 1
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)).Serena– Serena2018年10月04日 13:02:37 +00:00Commented 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.Lovette– Lovette2018年10月04日 13:05:26 +00:00Commented Oct 4, 2018 at 13:05
Explore related questions
See similar questions with these tags.
end if
clause. You have to remove it.