1

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.

snahl
  • 497
  • 2
  • 10
  • 25
  • 1
    There is a bit of discussion on [lua-users](http://lua-users.org/lists/lua-l/2013-07/msg00859.html); which you can alse read [here](http://comments.gmane.org/gmane.comp.lang.lua.general/101499). I'm thinking "Luiz Henrique de Figueiredo" is @lhf on [so]. :) – hjpotter92 Sep 30 '13 at 02:52
  • 2
    "from WGS84" to decimal is not precise, it is the wrong formulation: usually coordinates are given in "WGS84 decimal degrees". So please reformulate what you mean and give an example if you cannot express it precisely. Maybe you meant DMS (Degress Minutes Seconds) to DEG ((decimal) Degrees) conversion – AlexWien Sep 30 '13 at 14:09
  • @AlexWien Yes, I mean convert DMS (Degress Minutes Seconds) to DEG ((decimal) Degrees). – snahl Oct 08 '13 at 20:17
  • Use any DMS to DEG conversion algorithm, e.g found in wiki, this is not lua specific. – AlexWien Oct 08 '13 at 20:53
  • ok. I rephrased the question. – snahl Oct 17 '13 at 01:31
  • Lua's regexp string matching should make it fairly straightforward to parse these strings into the table you want (see http://lua-users.org/wiki/PatternsTutorial). Once you have those, min/60 + sec/3600 is your decimal degree. – Oliver Oct 17 '13 at 22:00
  • @Schollii Lua's regexp string matching: That is exactly the problem I'd like to find as solution for. – snahl Oct 19 '13 at 09:42

1 Answers1

1

Parse the string latlon = '48°12'30" N 16°22'28" E' into DMS+heading components:

  1. This is your string (note the escaped single-quote):

    latlon = '48°12\'30" N 16°22\'28" E'
    
  2. 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)')
    
  3. 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+)"')
    
  4. 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).

Oliver
  • 27,510
  • 9
  • 72
  • 103
  • ok, this is a start as it runs for the numbers. Yes, the string needs to be escaped with a backslash. How? The headings North, West, South and East (the latter are negative) are also important. Calculating the values is not a problem at all. I extracted them values surgically like this: [splitDMScoords.lua](https://gist.github.com/anonymous/7063176) I was hoping some genius regex or something might do the job in a much more efficient way. Any ideas? – snahl Oct 20 '13 at 00:14
  • @snahl The backslash is what escapes the single quote. It's always better to subdivide regexp if you can; it's like code: avoid complex lengthy expressions, it may show "genius regexp" but shows less than genious maintainability. With this in mind, I have extended my answer, but I left as two separate regexps. – Oliver Oct 21 '13 at 02:28