Multiple strings of if/then statements to convert numbers to text

I know there has been discussion about this a few times, but I still can’t seem to figure out how to combine a horrid string of if-then statements into one that works for what I’m doing.

I am essentially trying to convert a list of numbers (ranging from 0-360) into 16 different cardinal directions. So im trying to make a string of If/then statements that convert the range of numbers into N, NNE, NE, ENE, etc.

There must be a better way to do this lol but right now all I can think of is a string of If-then statements, such as: If (((x > 0) and (x < 22.5)), “NNE”, x)

I’ve managed to get one conditional statement to work, but i’m at a loss for where to go from here. Thank you for the help!

Check if this is the result you want. IMO, Find Domainwill suit what you need.

Angle (14.2 KB) (15.5 KB) (deprecated, see below)

On second thought… I see a rounding (or “Floor”?) error in this… and a flaw in the spec.
OK, got a little ugly to fix that (purple group) but now it works correctly. Headings 11 degrees plus or minus of North (for example) will be assigned the label “North”, as with all other directions. South is 169 (180-11) through 191 (180+11) degrees.
[[360 / 16 labels / 2 (+/-) = +-11.25 degrees of each labeled direction]] (17.0 KB) (deprecated for bug fix) (16.2 KB)

Thank you Kim!! This is super helpful – I never thought to use Find Domain. I will definitely be using this on future scripts!!

Joseph, this is amazing. Thank you. You were right to make the direction ±11.25 degrees! I wasn’t sure how to formulate that into a domain because i didn’t know how to translate - degrees from the given 0-360 inputs, but you managed to figure it out. Thanks so much

I’m a sailor so the defect was obvious. :sunglasses: Notice that I use the ‘F’ output (Floor) of Round, which is an important detail. Using ‘N’ (Nearest) is wrong there.

P.S. Here is a much simpler version where ‘Nearest’ is used indirectly. The ‘i’ input to List Item rounds to nearest integer (and handles Modulus and negative value wrapping!) to automagically implement the ±11.25 degree issue: (16.3 KB)

I think more clearly in the morning…

Check this as well. If you don’t mind to try a plugin, then pufferfish plugin has a feature.(Round To Nearest)

Angle (15.2 KB)


You can do this with a Python component, something like this (not tested!)

ÉDIT : forgot the modulo!

from __future__ import division
directions= 'N NNE NE ENE E ESE SE SSE S SSW SW WSW W WNW NW NNW'.split()
bin_size = 360/ len(directions)
a = list()
for value in x:
    index = int(round( value/bin_size )) % len(directions)
1 Like

For what is worth: (17.6 KB)

I do believe the better choice is using Python :wink: . Like @Dancergraham posted.

There are countless possibilities to overcome the rounding issue, like what if you convert the angle into cartesian coordinates (polar to cartesian) this way you can better make the switch because you have two variables that get positive and negative values.

I believe that plugins and Python are both overkill for the simplicity of four standard GH components, in the purple group. (same code as before) (12.8 KB)

Three standard GH components: (14.6 KB)



Here you have two solutions. One using a custom component the bottom one uses only standard components.


The difference with the solutions above is I added additional entry in the input list [N,…,N]

Hi @ivelin.peychev,

By having 17 cardinal points in the list you have broken the solution - try V=11 and V=11.5, first should be N, second should be NNE .


1 Like

Yes, indeed.