0

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)

7
  • You don't need to use a cursor in the field calculator. It will apply the function to all or selected rows automatically. All you need in your function is the if-then statement
    – Bjorn
    Apr 21, 2018 at 13:35
  • Thanks for the tip and quick response! @Bjorn I have tried updating my code but it's still not working for me, see below: 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!) 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? Apr 21, 2018 at 15:13
  • Yes - use the edit button to heavily revise your question which is fine to do at any time as long as it has not yet received any answers.
    – PolyGeo
    Apr 21, 2018 at 20:40
  • @PolyGeo Thanks! I've added my new code block towards the end of the original post, but I'm not sure whether that's the way to do it, because you mentioned "to heavily revise your question". Do you mean the original code should be deleted and replaced with my updated code instead? Apr 22, 2018 at 2:53
  • Yes - I think potential answerers are far more likely to find the time (which they all volunteer) to read a question that gets straight to the point rather than having to read code that you have already revised. Also, please always provide errors as text rather than pictures so that they are available to future searches.
    – PolyGeo
    Apr 22, 2018 at 3:57

1 Answer 1

0

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 
2
  • Question about replacing [NEAR_ANGLE] with NearAngle: -Is it okay if I used NEAR_ANGLE (without the brackets) to replace NearAngle? 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? Apr 22, 2018 at 6:25
  • 1
    I 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.
    – PolyGeo
    Apr 22, 2018 at 10:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.