梅登黑德定位Python代码

这个周末做了一点不那么难的事情,用Python计算一个经纬度的梅登黑德(Maidenhead)定位代码,也提供根据梅登黑德代码计算所在区域中心经纬度的函数。

梅登黑德定位系统是业余无线电最常用的定位系统,通常由两个字母-两个数字-再两个字母表示,这样六个字符可以定位到5’’(经度)×2.5’’(纬度)的网格上,便于通过摩尔斯电码拍发。

截图自http://f5len.org/tools/locator/(截图自http://f5len.org/tools/locator/

下面的程序提供了所有常用的函数,给出计算我所在的位置的例子(main函数)。

# Longitude and Latitude transform into Maidenhead Locator

# Class for Degree-Minute-Second Type
# var.degree    Degree of var
# var.minute    Minute of var
# var.second    Second of var
class DMS(object):
    def __init__(self,degree,minute,second):
        self.degree = degree
        self.minute = minute
        self.second = second

    # overload __str_ for print()
    def __str__(self):
        return str(self.degree) + '\u00B0' + str(self.minute) + '\u02CA' + str(int(self.second)) + '\u02DD'
    
    # return data in float format
    def DMS_2_float(self):
        return self.degree + self.minute/60 + self.second/3600

# transform float into DMS format
def float_2_DMS(float_var):
    degree = int(float_var)
    float_var = (float_var - degree) * 60
    minute = int(float_var)
    float_var = (float_var - minute) * 60
    second = float_var
    return DMS(degree,minute,second)

# transform (Latitude,Longitude) in floats into Maidenhead Locator
def LL_2_Maidenhead(float_Latitude,float_Longitude):
    a = float_Longitude/20 + 9
    b = int(a)
    str_maid = chr(b + 65) # the first character of Maidenhead code, from Longitude
    c = float_Latitude/10 + 9
    d = int(c)
    str_maid = str_maid + chr(d + 65) # the second character of Maidenhead code, from Latitude
    a = (a-b) * 10
    b = int(a)
    str_maid = str_maid + chr(b + 48)
    c = (c-d) * 10
    d = int(c)
    str_maid = str_maid + chr(d + 48)
    a = (a-b) * 24
    b = int(a)
    str_maid = str_maid + chr(b + 65)
    c = (c-d) * 24
    d = int(c)
    str_maid = str_maid + chr(d + 65)
    return str_maid

# transform (Latitude,Longitude) in DMS format into Maidenhead Locator
def LL_DMS_2_Maidenhead(DMS_Latitude,DMS_Longitude):
    float_Latitude = DMS_Latitude.DMS_2_float()
    float_Longitude = DMS_Longitude.DMS_2_float()
    return LL_2_Maidenhead(float_Latitude,float_Longitude)

# transform Maidenhead Locator into float (Latitude,Longitude)
def Maidenhead_2_LL(str_maid):
    float_Latitude = -90
    float_Longitude = -180
    chrA = str_maid[0]
    float_Longitude = float_Longitude + (ord(chrA)-65)*20
    chrA = str_maid[1]
    float_Latitude = float_Latitude + (ord(chrA)-65)*10
    chrA = str_maid[2]
    float_Longitude = float_Longitude + (ord(chrA)-48)*2
    chrA = str_maid[3]
    float_Latitude = float_Latitude + (ord(chrA)-48)*1
    chrA = str_maid[4]
    float_Longitude = float_Longitude + (ord(chrA)-65)*2/24
    chrA = str_maid[5]
    float_Latitude = float_Latitude + (ord(chrA)-65)*1/24
    float_Longitude + float_Longitude + 1/24
    float_Latitude = float_Latitude + 1/48      # set to the center of this area
    return float_Latitude, float_Longitude

# transform Maidenhead Locator into DMS format (Latitude,Longitude)
def Maidenhead_2_LL_DMS(str_maid):
    float_Latitude, float_Longitude = Maidenhead_2_LL(str_maid)
    DMS_Latitude = float_2_DMS(float_Latitude)
    DMS_Longitude = float_2_DMS(float_Longitude)
    return DMS_Latitude, DMS_Longitude

def main():
    # My Location
    L1 = DMS(34,15,37)
    L2 = DMS(108,38,57)
    print(LL_DMS_2_Maidenhead(L1,L2))
    
main()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值