How to convert "42°33'N, 1°33'E" to "42.55|1.55" in Scribunto (MediaWiki-hosted Lua)

StackOverflow https://stackoverflow.com/questions/18481858

  •  26-06-2022
  •  | 
  •  

Вопрос

Scribunto is a MediaWiki-hosted version of Lua.
I believe it is fairly standard Lua.

I want to convert geographic coordinates from this format:

42°33'N, 1°33'E

... to this format:

42.55|1.55

How to do this in Scribunto?

Это было полезно?

Решение

This assumes the input string is strictly of the form in the post. If there's any variability e.g. it can omit the minutes, include seconds, latitude and longitude can be separated differently, or whatever, the pattern will need to change.

function translate_coords(str)
    assert(type(str)=="string")
    local patt = "(%d+)°(%d+)'([NS]), (%d+)°(%d+)'([WE])"
    local latd,latm,latdir,lngd,lngm,lngdir = string.match(str,patt)
    assert(latd and latm and latdir)
    assert(lngd and lngm and lngdir)
    latd = latdir=="S" and -latd or latd
    lngd = lngdir=="W" and -lngd or lngd
    return ""..(latd+latm/60).."|"..(lngd+lngm/60)

end
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top