Modul:Wikidata/Formatters/globecoordinate
Vzhled
require 'Modul:No globals'
local p = {}
local lib = require 'Modul:Wikidata/lib'
local function fastConvertDdToDms(ddValue)
local dmsArr = {
degrees = 0,
minutes = 0,
seconds = 0.0
}
if ddValue then
dmsArr.degrees = math.floor(tonumber(ddValue))
dmsArr.minutes = math.floor((tonumber(ddValue) - dmsArr["degrees"]) * 60)
dmsArr.seconds = (tonumber(ddValue) - dmsArr["degrees"] - dmsArr["minutes"]/60) * 3600
end
return dmsArr
end
local function formatCoordinateValue(value, typeOfValue, field)
-- type bude nepovinny - tj. "nil"
-- priklad pouze -- je tam asi X chyb (mimo jine N vs S a podobne)
local result = ""
local latdmsText, londmsText
if typeOfValue == 'dms' then
local latDms = fastConvertDdToDms(value.latitude)
local lonDms = fastConvertDdToDms(value.longitude)
latdmsText = mw.ustring.format('N %s° %s\' %s"', latDms["degrees"], latDms["minutes"], latDms["seconds"])
londmsText = mw.ustring.format('E %s° %s\' %s"', lonDms["degrees"], lonDms["minutes"], lonDms["seconds"])
if field then
if field == 'latitude' then
result = latdmsText
elseif field == 'longitude' then
result = londmsText
end
else
result = latdmsText .. " " .. londmsText
end
elseif typeOfValue == 'dd' then
latdmsText = tonumber(value.latitude)
londmsText = tonumber(value.longitude)
if field then
if field == 'latitude' then
result = latdmsText
elseif field == 'longitude' then
result = londmsText
end
else
result = latdmsText .. " " .. londmsText
end
else
result = value.latitude .. ' / ' .. value.longitude .. ' (přesnost: ' .. value.precision .. ')'
end
return result
end
function p.getRawValue(value, options)
if not options.field or options.field == '' then
return error(lib.formatError('param-not-provided', 'field'))
elseif options.field == 'latitude' or options.field == 'longitude' or options.field == 'precision' or options.field == 'globe' then
return value[options.field]
else
return error(lib.formatError('invalid-field', options.field))
end
end
function p.formatValue(value, options)
if not options.field or options.field == '' then
-- return error(lib.formatError('param-not-provided', 'field'))
return formatCoordinateValue(value, 'dms')
elseif options.field == 'latitude' or options.field == 'longitude' then
return formatCoordinateValue(value, options.typeOfCoordinates, options.field)
elseif options.field == 'precision' or options.field == 'globe' then
return value[options.field]
else
return error(lib.formatError('invalid-field', options.field))
end
end
return p