In a file geodatabase table, I have an attribute called 'NEAR_ANGLE' created via the "Generate Near Table" tool that I'd like to convert to a 0-360 degrees format. I've created an attribute called 'Angle_360' in order to store the converted angles. Both these attributes have double as data type.
Regarding the current angle format - I selected the Geodesic method for running the "Generate Near Table" tool, and here's what the tool help states in ArcMap regarding its angle format:
When the Geodesic method is used, the angle is within the range of -180° to 180°, with 0° to the north, 90° to the east, 180° (or -180°) to the south, and -90° to the west.
I tried doing the conversion by using a Python script in "Field Calculator" for the 'Angle_360' attribute, but I've been unsuccessful. I'm an absolute beginner in ArcPy/Python, so I'm pretty sure I have huge syntax errors. Below is what I have so far:
Pre-Logic Script Code:
def Calculate(NearAngle):
if ( [NEAR_ANGLE] < 0 and [NEAR_ANGLE] > -180):
[NEAR_ANGLE] = [NEAR_ANGLE] +360
return [NEAR_ANGLE]
else:
return [NEAR_ANGLE]
Angle_360 =
Calculate(!NEAR_ANGLE!)
Error messages are below:
ERROR 000539: Error running expression: Calculate(153.979921739005)
Traceback (most recent call last):
File "< expression>", line `, in < module>
File "< string>", line 2, in Calculate
UnboundLocalError: local variable 'NEAR_ANGLE' referenced before assignment
Failed to execute (CalculateField).
Failed at Sun Apr 22 12:47:12 2018 (Elapsed Time: 0.13 seconds)
1 Answer 1
This statement:
def Calculate(NearAngle):
creates a variable named NearAngle
but this code:
if ( [NEAR_ANGLE] < 0 and [NEAR_ANGLE] > -180):
[NEAR_ANGLE] = [NEAR_ANGLE] +360
return [NEAR_ANGLE]
else:
return [NEAR_ANGLE]
looks for a variable named NEAR_ANGLE
:
Python is highly case-sensitive and a variable named NearAngle
is not the same as a variable named NEAR_ANGLE
which is why the error message says:
local variable 'NEAR_ANGLE' referenced before assignment
Also, those square brackets will be telling Python to create lists, so drop them too, and try this:
def Calculate(NearAngle):
if ( NearAngle < 0 and NearAngle > -180):
NearAngle = NearAngle +360
return NearAngle
else:
return NearAngle
-
Question about replacing
[NEAR_ANGLE]
withNearAngle
: -Is it okay if I usedNEAR_ANGLE
(without the brackets) to replaceNearAngle
? While the code works, I'm wondering whether it's bad practice to use the same name (NEAR_ANGLE
) for a variable and the column the expression is referring to? Or maybe it does not matter?ZinogreHunter– ZinogreHunter2018年04月22日 06:25:33 +00:00Commented Apr 22, 2018 at 6:25 -
1I like to use different names for the variable in the code block and the field name in the expression so that the distinction is emphasized but it is purely a preference.2018年04月22日 10:37:26 +00:00Commented Apr 22, 2018 at 10:37
Explore related questions
See similar questions with these tags.
if-then
statementdef Calculate(NearAngle): if ( [NEAR_ANGLE] < 0 and [NEAR_ANGLE] > -180): [NEAR_ANGLE] = [NEAR_ANGLE] +360 return [NEAR_ANGLE] else: return [NEAR_ANGLE]
**Angle_360 = **Calculate(!NEAR_ANGLE!)
How can I fix this? EDIT: I'm also new to stackexchange, so I don't know where I should post this code update, perhaps in the original question?