Python script/expression for field calculator for converting bearings to directions?

7098
28
Jump to solution
09-22-2016 10:03 AM
bobcostanza
New Contributor II

I have a double field with bearings/azimuths (numbers) and I need them in letter directions: N, NE, NW,etc. I would like to calculate them in  a new field.  I have searched high and low for the code to use in the field calculator but I have failed to figure it out.

Thanks,

Bob

0 Kudos
28 Replies
DanPatterson_Retired
MVP Emeritus

Darren... just shift your dictionary by 45 degrees since N-ish is 315 to 45, E-ish 45 to 135

DarrenWiens2
MVP Honored Contributor

Ahhh, yes that's right. Well, you get the idea.

0 Kudos
BlakeTerhune
MVP Regular Contributor

Yeah, I did the same thing 

0 Kudos
DanPatterson_Retired
MVP Emeritus

making digtize so much cleaner, since you can slice the 0-360 range anyway you like and the angle 'fits' in its correct position

0 Kudos
DanPatterson_Retired
MVP Emeritus

rather than messing up the original script post.

Direction (one variant of many... wiki it for the others)

Cardinal   Degree range
N   348.75 - 11.25
NNE  11.25 - 33.75
NE   33.75 - 56.25
ENE  56.25 - 78.75
E    78.75 - 101.25
ESE 101.25 - 123.75
SE  123.75 - 146.25
SSE 146.25 - 168.75
S   168.75 - 191.25
SSW 191.25 - 213.75
SW  213.75 - 236.25
WSW 236.25 - 258.75
W   258.75 - 281.25
WNW 281.25 - 303.75
NW  303.75 - 326.25
NNW 326.25 - 348.75

import numpy as np
global a
global c
a = np.arange(11.25, 360., 22.5)
c = np.array(['N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 
              'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N'])
def compass(angle):
    """ test """
    return c[np.digitize([angle], a)]‍‍‍‍‍‍‍‍‍

Expression for the field calculator

compass(!Field_with_the_angles!)  # using the python processor of course

For general testing or for single entry use and testing.

angles = np.arange(0, 360, 20) #[0, 44, 46, 134, 136, 224, 226, 314, 316, 359]
    for i in angles:
        print("{} => {}".format(i, compass(i)))‍‍‍

Result

0 => ['N']
20 => ['NNE']
40 => ['NE']
60 => ['ENE']
80 => ['E']
100 => ['E']
120 => ['ESE']
140 => ['SE']
160 => ['SSE']
180 => ['S']
200 => ['SSW']
220 => ['SW']
240 => ['WSW']
260 => ['W']
280 => ['W']
300 => ['WNW']
320 => ['NW']
340 => ['NNW']
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
bobcostanza
New Contributor II

I don't know what are appropriate responses for this new forum  but I want to thank you all for your help.  I should be able to give the scripts a test on Monday.  I do need to calculate NNE, SSW, WSW, etc.

It appears that the new forum is a collaborative effort between a bunch of people?  I'll try to read about it.

I am really surprised that my question was new.  I thought a half hour of Googling and I'd find how to do it.

Bob

0 Kudos
DarrenWiens2
MVP Honored Contributor

It depends what you were searching for. Does it ever work to search for a ready-made script that does exactly what you want? You really just need a "Python if statement" (or more likely, several), which produces endless useful search results explaining how to do what you need to do, if you look at the more general problem.

0 Kudos
bobcostanza
New Contributor II

Dan, I just got around to working with the script you provided.  Should your last expression from Sep 23, 2016 10:38 AM  work as is except for my field name with the angles?

Thanks

0 Kudos
DanPatterson_Retired
MVP Emeritus

make a text field to put the result in.... the !YourFieldNameWiththeAnglesGoesInTheExpression!

goes between the double ! marks.

0 Kudos
bobcostanza
New Contributor II

Oh, crap. I think I made it a number field!

Thanks Dan, I'll try again on Monday.

Bob

0 Kudos