Module:Titled coords

This module generates titled coordinates from raw coordinates extracted from Wikidata. Currently, {{Wikidata}} does not support adding a tit

Module:Titled coords

-- *** Version 1.2 (Beta) ***
local p = {}

-- Module entry point
function p.build(rawCoords, title, options)
    local has_seconds = true
    local output = ""

    if not title then title = "" end
    if not options then options = "" end

    -- ** Display the raw coordinates obtained from {{wikidata}} **
    if string.find(options,"raw") ~= nil then
        output = output .. "rawCoords:" .. rawCoords
    end

    -- ** Extract the individual fields from the raw coordinates **
    -- ** See https://www.lua.org/manual/5.3/manual.html#6.4.1   **
    local sidx, _, lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE =
        string.find(rawCoords, "(%d+)/(%d+)/(%d+%.?%d*)/(%a)/(%d+)/(%d+)/(%d+%.?%d*)/(%a)")

    if sidx == nil then -- extract without seconds
        local eidx
        sidx, eidx, lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE =
            string.find(rawCoords, "(%d+)/(%d+)/(%a)/(%d+)/(%d+)/(%a)")
        if sidx == nil then
            return output .. "<br/>Error: Failed to process rawCoords: " .. rawCoords
        end
        if string.find(options,"idx") ~= nil then
            output = output .. "<br/>start: " .. sidx .. " end: " .. eidx .. " lat_d: " .. lat_d
            return output
        end
        has_seconds = false
    end

    if string.find(options,"debug") then
        if has_seconds then
            output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dms = " .. lat_d .. "|" .. lat_m .. "|" .. lat_s .. "|" .. lat_NS ..
                                      "<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_s .. "|" .. lon_WE .. "\n"
         else
            output = output .. "<br/>rawCoords:" .. rawCoords .. "<br/>lat dm = " .. lat_d .. "|" .. lat_m ..  "|" .. lat_NS ..
                                      "<br/>lon dms = " .. lon_d .. "|" .. lon_m .. "|" .. lon_WE .. "\n"
         end
    end

    -- ** URL encode spaces in the title with + **
    local enc_title = title:gsub(" ", "+")

    -- ** Call Coordinates passing individual fields and setting the title **
    local coord = require('Module:Coordinates')._coord
    local type_title = "type:mountain&title=" .. enc_title
    local coordsTxt   -- declared here or lua strict complains
    if has_seconds then
        coordsTxt = coord{lat_d, lat_m, lat_s, lat_NS, lon_d, lon_m, lon_s, lon_WE, type_title}
    else
        coordsTxt = coord{lat_d, lat_m, lat_NS, lon_d, lon_m, lon_WE, type_title}
    -- ** Doesn't like me adding these args **
    --"type:mountain","format=dms","name=" .. name}}
    end

    if output ~= "" then
        return output .. "<br/>" .. coordsTxt
    end

    return coordsTxt
end

-- Template entry point
function p.main(frame)
    local raw_coords = frame.args[1]
    local title      = frame.args[2] or ""
    local options    = frame.args[3] or ""

    return p.build(raw_coords, title, options)
end

return p

-- LUA debug console tests
-- print(p.build("50/27/40/N/122/17/42/W","test title",""))
-- print(p.build("50/27/N/122/17/W","title no seconds",""))

--[[ Tried to invoke {{wikidata}} directly but not working:
    local raw_coords = frame:expandTemplate{title='wikidata', args={'property','page=' .. page,'raw','coord'}}
]]

Content Disclaimer

Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.

  1. The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
  2. There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
  3. It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
  4. Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.