@ganja:
My lua version of the PHP code:
function GPS_D2DM(data)
-- due to lacking 'int' datatype (MathFloor() doesn't work for negative 'float's)
local raw=ToNumber(data)
local dAbs=MathAbs(raw)
local d = MathFloor(dAbs)
local m = ToNumber(StringFormat("%.03f", (dAbs-d)*60))
if raw ~= 0 then d=dAbs/raw*d end
return d, m
end
function DD_DD2Coords(lat, lon)
lat, lon = DD_DD2Coords2(lat, lon)
return lat.." "..lon
end
function DD_DD2Coords2(lat, lon, strict)
local ns, ew
local latD, latM = GPS_D2DM(lat)
local lonD, lonM = GPS_D2DM(lon)
if ToNumber(lat) > 0 then
ns = 'N'
else
ns = 'S'
end
if ToNumber(lon) > 0 then
ew = 'E'
else
ew = 'W'
end
latD = MathAbs(latD)
lonD = MathAbs(lonD)
if strict then
lat = StringFormat("%s %02d° %02d.%03d", ns, latD, MathFloor(latM), MathFloor((latM - MathFloor(latM))*1000 + 0.5));
lon = StringFormat("%s %02d° %02d.%03d", ew, lonD, MathFloor(lonM), MathFloor((lonM - MathFloor(lonM))*1000 + 0.5));
else
lat = StringFormat("%s %d° %d.%03d", ns, latD, MathFloor(latM), MathFloor((latM - MathFloor(latM))*1000 + 0.5));
lon = StringFormat("%s %d° %d.%03d", ew, lonD, MathFloor(lonM), MathFloor((lonM - MathFloor(lonM))*1000 + 0.5));
end
return lat, lon
end