我有来自风向标的风向数据,数据以0到359度表示。
我希望将其转换为16种不同方向的文本格式(罗盘)。
基本上我想知道是否有一种快速光滑的方式将角度读数缩放到16弦阵列以打印出正确的风向而不使用一堆if语句并检查角度范围
可以找到风向here。
谢谢!
答案 0 :(得分:65)
编辑:
由于每22.5度有一个角度变化,方向应该在11.25度后交换手。
因此:
349-360//0-11 = N
12-33 = NNE
34-56 = NE
使用327-348(整个NNW谱图)中的值未能产生eudoxos答案的结果。 在给出一些想法之后,我无法找到他逻辑中的缺陷,所以我重写了自己的...
def degToCompass(num):
val=int((num/22.5)+.5)
arr=["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"]
print arr[(val % 16)]
>>> degToCompass(0)
N
>>> degToCompass(180)
S
>>> degToCompass(720)
N
>>> degToCompass(11)
N
>>> 12
12
>>> degToCompass(12)
NNE
>>> degToCompass(33)
NNE
>>> degToCompass(34)
NE
步骤:
答案 1 :(得分:26)
这是一个jve实现的steve-gregory的答案,对我有用。
function degToCompass(num) {
var val = Math.floor((num / 22.5) + 0.5);
var arr = ["N", "NNE", "NE", "ENE", "E", "ESE", "SE", "SSE", "S", "SSW", "SW", "WSW", "W", "WNW", "NW", "NNW"];
return arr[(val % 16)];
}
有关逻辑的解释,请参阅his answer。
答案 2 :(得分:11)
注意舍入,349 ... 11之间的角度应为“N”,因此首先添加半扇区(+(360/16)/ 2),然后处理溢出超过360%360,然后除以360 / 16:
["N","NNW",...,"NNE"][((d+(360/16)/2)%360)/(360/16)]
答案 3 :(得分:4)
我检查了这个并且效果非常好并且看起来很准确。 来源:Adrian Stevens的http://www.themethodology.net/2013/12/how-to-convert-degrees-to-cardinal.html
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)];
}
答案 4 :(得分:2)
我相信它更容易:
DirTable = ["N","NNE","NE","ENE","E","ESE", "SE","SSE","S","SSW","SW","WSW", "W","WNW","NW","NNW",**"N"**];
wind_direction= DirTable[Math.floor((d+11.25)/22.5)];
答案 5 :(得分:1)
我可能只是进行简单的度数除法,以获得数组中的位置或枚举值或能够为您提供所需文本的内容。只需向下划分所有部门。 360/16 = 22.5,所以你想要除以22.5来获得该位置。
String [] a = [N,NNW,NW,WNW,...,NNE]
答案 6 :(得分:1)
Javascript功能100%正常工作
function degToCompass(num) {
while( num < 0 ) num += 360 ;
while( num >= 360 ) num -= 360 ;
val= Math.round( (num -11.25 ) / 22.5 ) ;
arr=["N","NNE","NE","ENE","E","ESE", "SE",
"SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"] ;
return arr[ Math.abs(val) ] ;
}
步骤
希望有所帮助
答案 7 :(得分:1)
相反:
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'=>237.5, 'N'=>0,'North'=>0,'East'=>90,'West'=>270,'South'=>180);
return $dirs[$b];
}
答案 8 :(得分:1)
这很好用
#!/usr/bin/env python
def wind_deg_to_str1(deg):
if deg >= 11.25 and deg < 33.75: return 'NNE'
elif deg >= 33.75 and deg < 56.25: return 'NE'
elif deg >= 56.25 and deg < 78.75: return 'ENE'
elif deg >= 78.75 and deg < 101.25: return 'E'
elif deg >= 101.25 and deg < 123.75: return 'ESE'
elif deg >= 123.75 and deg < 146.25: return 'SE'
elif deg >= 146.25 and deg < 168.75: return 'SSE'
elif deg >= 168.75 and deg < 191.25: return 'S'
elif deg >= 191.25 and deg < 213.75: return 'SSW'
elif deg >= 213.75 and deg < 236.25: return 'SW'
elif deg >= 236.25 and deg < 258.75: return 'WSW'
elif deg >= 258.75 and deg < 281.25: return 'W'
elif deg >= 281.25 and deg < 303.75: return 'WNW'
elif deg >= 303.75 and deg < 326.25: return 'NW'
elif deg >= 326.25 and deg < 348.75: return 'NNW'
else: return 'N'
def wind_deg_to_str2(deg):
arr = ['NNE', 'NE', 'ENE', 'E', 'ESE', 'SE', 'SSE', 'S', 'SSW', 'SW', 'WSW', 'W', 'WNW', 'NW', 'NNW', 'N']
return arr[int(abs((deg - 11.25) % 360)/ 22.5)]
i = 0
while i < 360:
s1 = wind_deg_to_str1(i)
s2 = wind_deg_to_str2(i)
print '%5.1f deg -> func1(%-3s), func2(%-3s), same:%s' % (i, s1, s2, ('ok' if s1 == s2 else 'different'))
i += 0.5
答案 9 :(得分:1)
此JavaScript适用于只需要8个基本方向并想要相应箭头的任何人。
function getCardinalDirection(angle) {
const directions = ['↑ N', '↗ NE', '→ E', '↘ SE', '↓ S', '↙ SW', '← W', '↖ NW'];
return directions[Math.round(angle / 45) % 8];
}
答案 10 :(得分:0)
在Excel中使用它: VLOOKUP(MROUND(N12,22.5),N14:O29,2,FALSE)
单元格N12是指向需要答案的度数的方向。 范围N14:O29正在查找扇区(A到R):
风行业 0 A. 22.5 B. 45 C. 67.5 D. 90 E. 112.5 F. 135 G 157.5 H 180 J 202.5 K. 225升 247.5米 270 N. 292.5 P. 315问 337.5 R
答案 11 :(得分:0)
如果你到了这里,只想将你的学位分成8个方向之一。
function degToCompass(num){
const val = Math.floor((num / 45) + 0.5);
const arr = ["N","NE","E", "SE","S","SW","W","NW"];
return arr[(val % 8)]
答案 12 :(得分:0)
我大量使用R,因此需要解决方案。这是我想出的,并且可以很好地用于我喂食的所有可能的组合:
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)])
}
答案 13 :(得分:0)
想使用@eudoxos,但需要将所有部分拉在一起:
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))]
借用@Hristo markow来检查结果:
for i in range(0,360):
print (i,deg_to_compass(i) == wind_deg_to_str2(i))
答案 14 :(得分:0)
这是单行python函数:
def deg_to_text(deg):
return ["N","NNE","NE","ENE","E","ESE", "SE", "SSE","S","SSW","SW","WSW","W","WNW","NW","NNW"][round(deg/22.5)%16]
显然,为了便于阅读/ pep8,它可以分为多行