如何将GPS坐标转换为十进制的Lua? [英] How to convert GPS coordinates to decimal in Lua?
问题描述
我需要使用Lua将GPS坐标从WGS84转换为十进制。
我确信之前已完成此操作,所以我正在寻找代码片段的提示。
更正问题:在Lua中将DMS(Degress Minutes Seconds)转换为DEG((十进制)Degrees)的代码?
例子:
维也纳:dms:48°12'30N 16°22'28E
或
苏黎世:dms:47°21'7N 8° 30'37E
我发现的困难是从这些字符串中获得数字。
特别是如何处理程度(°)分钟(')和秒()的符号。
这样我就可以为每个坐标处理一个表格坐标{}。 >
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'30N 16°22'28E'解析为DMS +标题组件:
-
这是您的字符串(请注意转义的单引号):
latlon = '48°12 \'30N 16°22 \'28E'
-
将其分解为两个步骤:lat / lon,然后是每个元素的组成部分,需要捕获(),忽略标题周围的空格(N和E)和%s *:
lat,ns,lon,ew = string.match(latlon,'(。*)%s *(%a) % s *(。*)%s *(%a)')
-
现在是48°12'30,ns是'N',lon是16°22'28,ew是'E'。对于拉特的组件,一步一步:
- string.match(lat,'48°12'30') - 哎呀'需要转义或
- string.match(lat,'48°12 \'30'')
- 准备好捕获:
- string .match(lat,'(48)°(12)\\''(30)'') - 准备好通用数字
d1,m1,s1 = string.match(lat,'(%d +)° (%d +)\\''(%d +)'')
d2,m2,s2 = string.match(lon,'(%d +)°(%d +)\'(%d +)'')
-
现在你已经知道(d1,m1,s1,ns)和(d2,m2 ,s2,ew),你有:
pre $ code $ 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值,您可以得到decDeg1 = 48.208333,这是根据在线计算器的正确值(如 http://www.satsig.net/degrees-minutes-seconds-calculator.htm )。
I need to convert GPS coordinates from WGS84 to decimal using Lua.
I am sure it's been done before, so I am looking for a hint to a code snippet.
corrected question: Code to convert DMS (Degress Minutes Seconds) to DEG ((decimal) Degrees) in Lua?
examples: Vienna: dms: 48°12'30" N 16°22'28" E or Zurich: dms: 47°21'7" N 8°30'37" E
The difficulty I find is to get the numbers out of these strings. Especially how to handle the signs for degree (°) minutes (') and seconds ("). So that I would have for example a table coord{} per coordinate to deal with.
coord {1} [48]
coord {2} [12]
coord {3} [30]
coord {4} [N]
coord {5} [16]
coord {6} [22]
coord {7} [28]
coord {8} [E]
Suggestions are appreciated, thanks.
Parse the string latlon = '48°12'30" N 16°22'28" E' into DMS+heading components:
This is your string (note the escaped single-quote):
latlon = '48°12\'30" N 16°22\'28" E'
Break it down into two steps: the lat/lon, then components of each. You need captures "()", ignore spaces around the heading (N and E) with "%s*":
lat, ns, lon, ew = string.match(latlon, '(.*)%s*(%a)%s*(.*)%s*(%a)')
The lat is now 48°12'30", ns is 'N', lon is 16°22'28", ew is 'E'. For components of lat, step by step:
-- 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+)"')
Now that you know (d1, m1, s1, ns) and (d2, m2, s2, ew), you have:
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)
For your values of lat, you get decDeg1 = 48.208333 which is the correct value according to online calculators (like http://www.satsig.net/degrees-minutes-seconds-calculator.htm).
这篇关于如何将GPS坐标转换为十进制的Lua?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!