Přeskočit na obsah

Modul:Souřadnice

Z Infopedia

Dokumentaci tohoto modulu lze vytvořit na stránce Modul:Souřadnice/Dokumentace

local p = {}

local function trim(s)
    if not s then return '' end
    return (s:gsub('^%s*(.-)%s*$', '%1'))
end

local function isMetaToken(s)
    if not s or s == '' then return true end
    if s:find(':', 1, true) or s:find('=', 1, true) then
        return true
    end
    return false
end

local function isDirLat(s)
    local l = mw.ustring.lower(s)
    return l == 'n' or l == 's' or l == 'j' or l == 'sš' or l == 'jš' or l == 'š'
end

local function isDirLon(s)
    local l = mw.ustring.lower(s)
    return l == 'e' or l == 'w' or l == 'v' or l == 'z' or l == 'vd' or l == 'zd'
end

local function latSign(s)
    -- Uppercase 'S' in DMS with E/W or Z (like Chile 30|14|40|S|70|44|57|Z) means South;
    -- Lowercase 's' (or 'sš') means Czech 'sever' (North).
    if s == 'S' then return -1 end
    local l = mw.ustring.lower(s)
    if l == 'j' or l == 'jš' then return -1 end
    return 1
end

local function lonSign(s)
    local l = mw.ustring.lower(s)
    if l == 'w' or l == 'z' or l == 'zd' then return -1 end
    return 1
end

local function toNum(s)
    if not s then return nil end
    s = s:gsub(',', '.')
    return tonumber(s)
end

local function toDMS(degDecimal)
    local absVal = math.abs(degDecimal)
    local d = math.floor(absVal + 1e-9)
    local remMin = (absVal - d) * 60
    local m = math.floor(remMin + 1e-7)
    local s = math.floor((remMin - m) * 60 + 0.5)
    if s >= 60 then
        s = s - 60
        m = m + 1
    end
    if m >= 60 then
        m = m - 60
        d = d + 1
    end
    return d, m, s
end

local function detectZoom(metaList)
    local allMeta = table.concat(metaList, ' '):lower()
    if allMeta:find('continent') then return 3 end
    if allMeta:find('country') or allMeta:find('stát') then return 5 end
    if allMeta:find('region') and not allMeta:find('city') and not allMeta:find('landmark') and not allMeta:find('mountain') then
        if allMeta:find('type:') or allMeta:find('typ:') or allMeta:find('typ=') then
            -- has explicit type, continue checking
        else
            return 10
        end
    end
    if allMeta:find('waterbody') or allMeta:find('sea') then return 7 end
    if allMeta:find('city') or allMeta:find('město') then return 12 end
    if allMeta:find('landmark') or allMeta:find('mountain') or allMeta:find('airport') or allMeta:find('observatory') then return 14 end
    return 12
end

function p.main(frame)
    local parent = frame:getParent()
    local args = parent and parent.args or frame.args

    local pos = {}
    local meta = {}
    local i = 1
    while args[i] ~= nil do
        local v = trim(args[i])
        if v ~= '' then
            if isMetaToken(v) then
                table.insert(meta, v)
            else
                table.insert(pos, v)
            end
        end
        i = i + 1
    end

    local namedType = trim(args['typ'] or args['type'] or '')
    if namedType ~= '' then table.insert(meta, 'type:' .. namedType) end
    local namedRegion = trim(args['region'] or '')
    if namedRegion ~= '' then table.insert(meta, 'region:' .. namedRegion) end

    local display = trim(args['display'] or args['zobrazit'] or ''):lower()
    for _, m in ipairs(meta) do
        local ml = m:lower()
        if ml:find('zobrazit') or ml:find('titulek') or ml:find('title') then
            display = display .. ',' .. ml
        end
    end
    local wantTitle = display:find('title') or display:find('titulek')
    local wantInline = display == '' or display:find('inline') or (not wantTitle)

    local fmt = trim(args['format'] or ''):lower()

    local lat, lon
    local latText, lonText

    local n = #pos
    if n == 8 and isDirLat(pos[4]) and isDirLon(pos[8]) then
        local d1, m1, s1 = toNum(pos[1]), toNum(pos[2]), toNum(pos[3])
        local d2, m2, s2 = toNum(pos[5]), toNum(pos[6]), toNum(pos[7])
        if d1 and m1 and s1 and d2 and m2 and s2 then
            local sgn1 = latSign(pos[4])
            local sgn2 = lonSign(pos[8])
            lat = sgn1 * (math.abs(d1) + m1 / 60 + s1 / 3600)
            lon = sgn2 * (math.abs(d2) + m2 / 60 + s2 / 3600)
            latText = string.format('%d° %d′ %g″ %s', math.abs(d1), m1, s1, sgn1 >= 0 and 's. š.' or 'j. š.')
            lonText = string.format('%d° %d′ %g″ %s', math.abs(d2), m2, s2, sgn2 >= 0 and 'v. d.' or 'z. d.')
        end
    elseif n == 6 and isDirLat(pos[3]) and isDirLon(pos[6]) then
        local d1, m1 = toNum(pos[1]), toNum(pos[2])
        local d2, m2 = toNum(pos[4]), toNum(pos[5])
        if d1 and m1 and d2 and m2 then
            local sgn1 = latSign(pos[3])
            local sgn2 = lonSign(pos[6])
            lat = sgn1 * (math.abs(d1) + m1 / 60)
            lon = sgn2 * (math.abs(d2) + m2 / 60)
            latText = string.format('%d° %g′ %s', math.abs(d1), m1, sgn1 >= 0 and 's. š.' or 'j. š.')
            lonText = string.format('%d° %g′ %s', math.abs(d2), m2, sgn2 >= 0 and 'v. d.' or 'z. d.')
        end
    elseif n == 4 and isDirLat(pos[2]) and isDirLon(pos[4]) then
        local d1, d2 = toNum(pos[1]), toNum(pos[3])
        if d1 and d2 then
            local sgn1 = latSign(pos[2])
            local sgn2 = lonSign(pos[4])
            lat = sgn1 * math.abs(d1)
            lon = sgn2 * math.abs(d2)
            if fmt == 'dms' or (math.floor(d1) ~= d1) then
                local ld, lm, ls = toDMS(lat)
                local nd, nm, ns = toDMS(lon)
                latText = string.format('%d° %d′ %d″ %s', ld, lm, ls, sgn1 >= 0 and 's. š.' or 'j. š.')
                lonText = string.format('%d° %d′ %d″ %s', nd, nm, ns, sgn2 >= 0 and 'v. d.' or 'z. d.')
            else
                latText = string.format('%d° %s', math.abs(d1), sgn1 >= 0 and 's. š.' or 'j. š.')
                lonText = string.format('%d° %s', math.abs(d2), sgn2 >= 0 and 'v. d.' or 'z. d.')
            end
        end
    elseif n == 2 then
        local d1, d2 = toNum(pos[1]), toNum(pos[2])
        if d1 and d2 then
            lat, lon = d1, d2
            local ld, lm, ls = toDMS(lat)
            local nd, nm, ns = toDMS(lon)
            latText = string.format('%d° %d′ %d″ %s', ld, lm, ls, lat >= 0 and 's. š.' or 'j. š.')
            lonText = string.format('%d° %d′ %d″ %s', nd, nm, ns, lon >= 0 and 'v. d.' or 'z. d.')
        end
    end

    if not lat or not lon then
        return '<span class="infopedia-coord-err">' .. table.concat(pos, ' ') .. '</span>'
    end

    local zoom = detectZoom(meta)
    local latStr = string.format('%.6f', lat)
    local lonStr = string.format('%.6f', lon)
    local mapyUrl = string.format('https://mapy.cz/zakladni?x=%s&y=%s&z=%d', lonStr, latStr, zoom)
    local osmUrl = string.format('https://www.openstreetmap.org/?mlat=%s&mlon=%s#map=%d/%s/%s', latStr, lonStr, zoom, latStr, lonStr)

    local pill = string.format(
        '<span class="infopedia-coord"><span class="infopedia-coord-pin">📍</span> <span class="infopedia-coord-val">%s, %s</span> <span class="infopedia-coord-links">([%s Mapy.cz] · [%s OSM])</span></span>',
        latText, lonText, mapyUrl, osmUrl
    )

    local styles = frame:extensionTag('templatestyles', '', { src = 'Souřadnice/styles.css' })
    local out = styles

    if wantTitle then
        out = out .. frame:extensionTag('indicator', pill, { name = 'coordinates' })
    end
    if wantInline then
        out = out .. pill
    end

    return out
end

return p