最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I have wind direction data coming from OpenWeatherMap API, and the data is represented in 0 to 360 degrees. I want

programmeradmin1浏览0评论
function  toTextualDescription(degree){
    if ((degree>337.5 && degree<360)|| (degree>22.5 && degree<22.5))
    {return 'Northerly';}
    else if(degree>22.5 && degree<67.5){return 'North Easterly';}
    else if(degree>67.5 && degree<112.5){return 'Easterly';}
    else if(degree>122.5 && degree<157.5){return 'South Easterly';} 
    else if(degree>157.5 && degree<202.5){return 'Southerly';}
    else if(degree>202.5 && degree<247.5){return 'South Westerly';}
    else if(degree>247.5 && degree<292.5){return 'Westerly';}
    else if(degree>292.5 && degree<337.5){return 'North Westerly';}
}

.pdf on the above link you can find the direction representation, i want to change the degree to textual for as, North, NorthEast, East, SouthEast, South, south west, west , and Northwest could you suggest a better way of doing this.how can i improve it? i am using Javascript.

function  toTextualDescription(degree){
    if ((degree>337.5 && degree<360)|| (degree>22.5 && degree<22.5))
    {return 'Northerly';}
    else if(degree>22.5 && degree<67.5){return 'North Easterly';}
    else if(degree>67.5 && degree<112.5){return 'Easterly';}
    else if(degree>122.5 && degree<157.5){return 'South Easterly';} 
    else if(degree>157.5 && degree<202.5){return 'Southerly';}
    else if(degree>202.5 && degree<247.5){return 'South Westerly';}
    else if(degree>247.5 && degree<292.5){return 'Westerly';}
    else if(degree>292.5 && degree<337.5){return 'North Westerly';}
}

https://compuweather.com/files/2009/10/CompuWeather-Wind-Direction-Compass-Chart.pdf on the above link you can find the direction representation, i want to change the degree to textual for as, North, NorthEast, East, SouthEast, South, south west, west , and Northwest could you suggest a better way of doing this.how can i improve it? i am using Javascript.

Share Improve this question asked Apr 7, 2016 at 11:46 ipkissipkiss 571 silver badge8 bronze badges
Add a comment  | 

4 Answers 4

Reset to default 10

You do not need so many checks in if statements. Also, you do not need else if because return will end function execution in proper places.

function  toTextualDescription(degree){
    if (degree>337.5) return 'Northerly';
    if (degree>292.5) return 'North Westerly';
    if (degree>247.5) return 'Westerly';
    if (degree>202.5) return 'South Westerly';
    if (degree>157.5) return 'Southerly';
    if (degree>122.5) return 'South Easterly';
    if (degree>67.5) return 'Easterly';
    if (degree>22.5) return 'North Easterly';
    return 'Northerly';
}
let compassSector = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW", "N"];

weather.windDirection = compassSector[(data.wind.deg / 22.5).toFixed(0)];

Here's a way to do it with an array of sector names. This will also work for values < 0 and > 360

function toTextualDescription(degree) {
  var sectors = ['Northerly','North Easterly','Easterly','South Easterly','Southerly','South Westerly','Westerly','North Westerly'];
  
  degree += 22.5;

  if (degree < 0) 
    degree = 360 - Math.abs(degree) % 360;
  else 
    degree = degree % 360;
  
  var which = parseInt(degree / 45);
  return sectors[which];
}

console.log("0: " + toTextualDescription(0));
console.log("-30: " + toTextualDescription(-30));
console.log("900: " + toTextualDescription(900));
console.log("22.4999: " + toTextualDescription(22.4999));
console.log("22.5: " + toTextualDescription(22.5));
console.log("359: " + toTextualDescription(359));

This is from one of my projects, so the text is a bit different:

function deg_to_hdg( degrees ){
degrees = Math.floor( degrees / 22.5 )

return Array(
    'N',
    'NE',
    'NE',
    'E',
    'E',
    'SE',
    'SE',
    'S',
    'S',
    'SW',
    'SW',
    'W',
    'W',
    'NW',
    'NW',
    'N'
)[degrees];

}

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论