Converting wind direction in angles to text words

后端 未结 15 767
执笔经年
执笔经年 2021-01-30 13:16

I have wind direction data coming from a weather vane, and the data is represented in 0 to 359 degrees.

I want to convert this into text format (compass rose) with 16 di

相关标签:
15条回答
  • 2021-01-30 13:56

    I would probably just do simple division of degrees to get a position in an array or an enum value or something that would give you the text you need. Just round down on all your division. 360/16 = 22.5, so you would want to divide by 22.5 to get the position.

    String[] a = [N,NNW,NW,WNW,...,NNE]

    0 讨论(0)
  • 2021-01-30 13:57

    Wanted to use @eudoxos but needed to pull all the parts together:

    def deg_to_compass(d):
      return ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE",
            "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"] [math.floor(((d+(360/16)/2)%360)/(360/16))]
    

    Borrrowed @Hristo markow to check the results:

    for i in range(0,360):
      print (i,deg_to_compass(i) == wind_deg_to_str2(i))
    
    0 讨论(0)
  • 2021-01-30 14:00

    I checked this and it works very good and seems accurate. Source: http://www.themethodology.net/2013/12/how-to-convert-degrees-to-cardinal.html by Adrian Stevens

        public static string DegreesToCardinal(double degrees)
        {
            string[] caridnals = { "N", "NE", "E", "SE", "S", "SW", "W", "NW", "N" };
            return caridnals[(int)Math.Round(((double)degrees % 360) / 45)];
        }
    
        public static string DegreesToCardinalDetailed(double degrees)
        {
            degrees *= 10;
    
            string[] caridnals = { "N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N" };
            return caridnals[(int)Math.Round(((double)degrees % 3600) / 225)];
        }
    
    0 讨论(0)
  • 2021-01-30 14:04

    I use R heavily and needed a solution for this. This is what I came up with and works well for all possible combinations I have fed it:

    degToCardinal <- function(degrees) {
      val <- as.integer((degrees / 22.5) + 0.5)
      arr <- c("N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW")
      return(arr[((val+1) %% 16)])
    }
    
    0 讨论(0)
  • 2021-01-30 14:09

    This JavaScript will work for anyone who only needs 8 cardinal directions and would like corresponding arrows.

    function getCardinalDirection(angle) {
        const directions = ['↑ N', '↗ NE', '→ E', '↘ SE', '↓ S', '↙ SW', '← W', '↖ NW'];
        return directions[Math.round(angle / 45) % 8];
    }
    
    0 讨论(0)
  • 2021-01-30 14:09

    To do the reverse conversion (compass letter abbreviations to degrees):

    function getDir($b)
    {
    
       $dirs = array('N'=>0, 'NNE'=>22.5,"NE"=>45,"ENE"=>67.5, 'E'=>90,'ESE'=>112.5, 'SE'=>135,'SSE'=>157.5, 'S'=>180,'SSW'=>202.5, 'SW'=>225,'WSW'=>247.5, 'W'=>270,'WNW'=>292.5,'NW'=>315,'NNW'=>337.5, 'N'=>0,'North'=>0,'East'=>90,'West'=>270,'South'=>180);
       return $dirs[$b];
    }
    
    0 讨论(0)
提交回复
热议问题