簡體   English   中英

如何在Lua中將GPS坐標轉換為小數?

[英]How to convert GPS coordinates to decimal in Lua?

我需要使用Lua將GPS坐標從WGS84轉換為十進制。

我確定它已經完成了,所以我正在尋找代碼段的提示。

已解決的問題:在Lua中將DMS(出場時間秒)轉換為DEG((十進制)度)的代碼?

例如:維也納:dms:東48°12'30“ N 16°22'28”或蘇黎世:dms:東47°21'7“ N 8°30'37” E

我發現困難在於從這些字符串中提取數字。 尤其是如何處理度數(°)分鍾(')和秒數(“)。因此,例如,每個坐標都有一個表座標{}來處理。

coord {1} [48]
coord {2} [12]
coord {3} [30]
coord {4} [N]
coord {5} [16]
coord {6} [22]
coord {7} [28]
coord {8} [E]

建議表示贊賞,謝謝。

將字符串latlon = '48°12'30“ N 16°22'28” E'解析為DMS +標題組件:

  1. 這是您的字符串(請注意轉義的單引號):

     latlon = '48°12\\'30" N 16°22\\'28" E' 
  2. 將其分為兩個步驟:緯度/經度,然后是每個成分。 您需要捕獲“()”,並用“%s *”忽略標題(N和E)周圍的空格:

     lat, ns, lon, ew = string.match(latlon, '(.*)%s*(%a)%s*(.*)%s*(%a)') 
  3. 緯度現在為48°12'30“,ns為'N',lon為16°22'28”,ew為'E'。 對於經緯度的組成部分,請分步進行:

     -- string.match(lat, '48°12'30"') -- oops the ' needs escaping or us -- string.match(lat, '48°12\\'30"') -- ready for the captures: -- string.match(lat, '(48)°(12)\\'(30)"') -- ready for generic numbers d1, m1, s1 = string.match(lat, '(%d+)°(%d+)\\'(%d+)"') d2, m2, s2 = string.match(lon, '(%d+)°(%d+)\\'(%d+)"') 
  4. 現在您知道了(d1,m1,s1,ns)和(d2,m2,s2,ew),您具有:

     sign = 1 if ns=='S' then sign = -1 end decDeg1 = sign*(d1 + m1/60 + s1/3600) sign = 1 if ew=='W' then sign = -1 end decDeg2 = sign*(d2 + m2/60 + s2/3600) 

對於lat值,根據在線計算器(例如http://www.satsig.net/degrees-minutes-seconds-calculator.htm ),您將獲得decDeg1 = 48.208333這是正確的值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM