Python 時刻形式経緯度十進変換関数クラス

航空や気象で扱われてる時刻形式の経緯度を地図で扱うために、十進値に変換する関数を作ってクラスにしてみました。

< コード例 >

クラス


'''
時刻形式経緯度十進変換クラス
'''
class DigitLL:

  '''
  コンストラクタ
  txt : 時刻経緯度
  endchr : N or S or E or W
  '''
  def __init__(self, txt, endchr):
    self.txt = txt
    self.endchr = endchr

  '''
  十進変換
  '''
  def digitLLFromTimeLL(self):

      numInt = int(self.txt)
      # 整数部
      hourPartVal = int(numInt / 10000)
      # 分
      minutePartVal = int(numInt / 100) % 100
      # 秒
      secondPartVal = numInt % 100

      minuteVal = minutePartVal / 60
      secondVal = secondPartVal / 3600
      resval = hourPartVal + minuteVal + secondVa
      
      # 南半球、西半球の場合 #
      if (self.endchr == "S" or self.endchr == "W
          resval = resval * -1

      # debug
      # print(str(hourPartVal) + ":" + str(minute

      return resval

呼び出し
ダウンロードしたExcelのファイルのセルの値を読み込んで変換する場面で使ってます。


'''
Waypointエクセルファイル読込
使用
wp_euro_procedure.pyから使う

filename : エクセルファイル名
effdate_end : effective expired date
'''
def readExcel(filename, effdate_end):

    wb = openpyxl.load_workbook(filename)
    sheet = wb["FRA Points"]

    # 書き込み用タプルリスト追加 #

    tpllist = []

    # 行数を調べる #
    rowmax = getDataRowMax(sheet)

    # identcol = "C" + str(i + 2)

    ## ++ 辞書値リスト追加走査 ++ ##
    for i in range(rowmax):

        ident = sheet["C" + str(i + 2)].value
        type = sheet["A" + str(i + 2)].value
        latcval = sheet["D" + str(i + 2)].value
        loncval = sheet["E" + str(i + 2)].value
        name = sheet["F" + str(i + 2)].value
        #if type == None:
        #    type = "FIX"
            
        type = "FIX" if type == None else type     

        latendchr = latcval[0:1]
        lonendchr = loncval[0:1]
        latvalpart  = latcval[1:]
        lonvalpart = loncval[1:]

        #lat = digitLLFromTimeLL(latvalpart, latendchr)
        #lon = digitLLFromTimeLL(lonvalpart, lonendchr)
        
        # クラスを使う #
        dll_lat = dll.DigitLL(latvalpart, latendchr)
        dll_lon = dll.DigitLL(lonvalpart, lonendchr)
        
        lat = dll_lat.digitLLFromTimeLL()
        lon = dll_lon.digitLLFromTimeLL()
        
        
        # debug
        print(ident)
        print(latcval + " => " + str(lat))
        print(loncval + " => " + str(lon))
        print("-------------------------------------------")

        tpl = {"type": "", "ident": "", "name": "", "lat": -500, "lon": -500}

        if lat != 0 and lon != 0:
            tpl["type"] = type
            tpl["ident"] = ident
            tpl["name"] = name
            tpl["lat"] = lat
            tpl["lon"] = lon
            tpllist.append(tpl)

< エクセルファイル例 >

[商品価格に関しましては、リンクが作成された時点と現時点で情報が変更されている場合がございます。]

航空情報 2023年 6月号 [雑誌]
価格:1,425円(税込、送料無料) (2023/5/21時点)

コメントを残す