Skip to content

Instantly share code, notes, and snippets.

@RobertSudwarts
Created June 7, 2015 16:18
Show Gist options
  • Star 28 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save RobertSudwarts/acf8df23a16afdb5837f to your computer and use it in GitHub Desktop.
Save RobertSudwarts/acf8df23a16afdb5837f to your computer and use it in GitHub Desktop.
[python] degrees to cardinal directions
def degrees_to_cardinal(d):
'''
note: this is highly approximate...
'''
dirs = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
"S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"]
ix = int((d + 11.25)/22.5)
return dirs[ix % 16]
@sooswastaken
Copy link

Thank you, I am going to use this is my project

@STG-fisheries
Copy link

Thank you. Will use this in my project.

@AndreLobato
Copy link

In case someone want's to apply for a Numpy Array of angles I've adapted as such...

def degrees_to_cardinal(d):
    '''
    note: this is highly approximate...
    '''
    dirs = np.array(["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
                     "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"], dtype='U')
    ix = np.round(d / (360. / len(dirs))).astype('i')
    return dirs[ix % 16]

@ptcane
Copy link

ptcane commented Jul 5, 2023

I did the following:

DIRECTIONS = [
    'N', 'NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW',
    'W', 'WNW', 'NW', 'NNW', 'N'
]

x = 155 # anything 0 - 360
cardinal = DIRECTIONS[round(x / 22.5)]

Note the extra N at the end of the list.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment