Module:WikidataIB/sandbox1: Difference between revisions

Content deleted Content added
that allows getCoords to be much simpler
decimalToDMS function to reduce duplicated code
Line 105:
if x == math.floor(x) then x = math.floor(x) end
return x
end
 
 
-------------------------------------------------------------------------------
-- decimalToDMS takes a decimal degrees (x) with precision (p)
-- and returns degrees/minutes/seconds according to the precision
local function decimalToDMS(x, p)
local latmind = math.floor(latmsx)
local latsecms = (latmsx - latmind) * 60
if p > 0.5 then -- precision is > 1/2 a degree
if latmsms > 30 then latdegd = latdegd + 1 end
latmsms = 0
end
local longdegm = math.floor(longms)
local longmss = (longms - longdegm) * 60
if p > 0.008 then -- precision is > 1/2 a minute
if latsecs > 30 then latminm = latminm +1 end
s = 0
elseif p > 0.00014 then -- precision is > 1/2 a second
latsecs = math.floor(100 * latsecs + 0.5) / 100
elseif p > 0.000014 then -- precision is > 1/20 second
latsecs = math.floor(100010 * latsecs + 0.5) / 100010
elseif p > 0.0000014 then -- precision is > 1/200 second
longsecs = math.floor(100 * longsecs + 0.5) / 100
else -- cap it at 3 dec places for now
longsecs = math.floor(1000 * longsecs + 0.5) / 1000
end
return d, m, s
end
 
Line 555 ⟶ 583:
elseif datatype == "globe-coordinate" then
-- 'display' parameter defaults to "inline, title" *** unused for now ***
-- local disp = args.display or ""
-- if disp == "" then disp = "inline, title" end
end--
 
-- format parameter switches from deg/min/sec to decimal degrees
-- default is deg/min/sec -- decimal degrees needs |format = dec
local form = (args.format or ""):lower():sub(1,3)
if form ~= "dec" then form = "dms" end
 
local lat, long, prec = datavalue.latitude, datavalue.longitude, datavalue.precision
 
local ns = "N"
local ew = "W"
Line 575 ⟶ 601:
long = - long
end
 
if form == "dec" then
out[#out+1] = lat .. "°" .. ns .. " " .. long .. "°" .. ew
else
local latdeg, latmin, latsec = math.floordecimalToDMS(lat, prec)
local latmslongdeg, longmin, longsec = decimalToDMS(latlong, - latdegprec) * 60
if prec > 0.5 then
if latms > 30 then latdeg = latdeg + 1 end
latms = 0
end
local latmin = math.floor(latms)
local latsec = (latms - latmin) * 60
if prec > 0.008 then
if latsec > 30 then latmin = latmin +1 end
latsec = 0
end
if prec > 0.00014 then latsec = math.floor(latsec + 0.5) end
if prec > 0.000014 then latsec = math.floor(10 * latsec + 0.5) / 10 end
if prec > 0.0000014 then
latsec = math.floor(100 * latsec + 0.5) / 100
else -- cap it at 3 dec places for now
latsec = math.floor(1000 * latsec + 0.5) / 1000
end
local longdeg = math.floor(long)
local longms = (long - longdeg) * 60
if prec > 0.5 then
if longms > 30 then longdeg = longdeg + 1 end
longms = 0
end
local longmin = math.floor(longms)
local longsec = (longms - longmin) * 60
if prec > 0.008 then
if longsec > 30 then longmin = longmin +1 end
longsec = 0
end
if prec > 0.00014 then longsec = math.floor(longsec + 0.5) end
if prec > 0.000014 then longsec = math.floor(10 * longsec + 0.5) / 10 end
if prec > 0.0000014 then
longsec = math.floor(100 * longsec + 0.5) / 100
else -- cap it at 3 dec places for now
longsec = math.floor(1000 * longsec + 0.5) / 1000
end
 
if latsec == 0 and longsec == 0 then