Module:Coordinates

From WikiMD's Wellness Encyclopedia

Revision as of 18:58, 18 December 2024 by Prab (talk | contribs) (Replaced content with "--This module handles coordinates without requiring Wikidata integration. require('strict') local coordinates = {} -- GeoHack link base local current_page = mw....")

Lua error in Module:TNT at line 159: Missing JsonConfig extension; Cannot load https://commons.wikimedia.org/wiki/Data:I18n/Uses TemplateStyles.tab. Note: The code which this module's main function (coord) outputs is directly parsed and/or manipulated by Module:Location map and other functions of this module itself (coord2text and coordinsert). If the structure of the output changes (for example, to use the <mapframe> and <maplink> tags), please update the aforementioned scripts as well.

Using the module with coordinsert

When using the {{Coord}} template inside another template, like an infobox, there may be parameters (like type:airport) which should be added automatically. To do so, do something like this:

{{#if:{{{coordinates|}}}|{{#invoke:Coordinates|coordinsert|{{{coordinates|}}}|parameter1:value1|parameter2:value2|parameter3:value3…}}| 

Do not add more vertical bars | than necessary.

Using the module with coord2text to extract latitude or longitude

Developers maintaining legacy code may need to extract latitude or longitude to use a parameters in other code, or a mathematical expression. The module's "coord2text" function can be used to extract data from the {{Coord}} template. To extract the latitude from a Coord template, use:

{{#invoke:coordinates|coord2text|{{Coord|57|18|22|N|4|27|32|E}}|lat}}Script error: The function "coord2text" does not exist.

To extract the longitude, use:

{{#invoke:coordinates|coord2text|{{Coord|57|18|22|N|4|27|32|E}}|long}}Script error: The function "coord2text" does not exist.

Modules using this module directly

Tracking categories


--[[ 
This module handles coordinates without requiring Wikidata integration. 
]]

require('strict')

local coordinates = {}

-- GeoHack link base
local current_page = mw.title.getCurrentTitle()
local page_name = mw.uri.encode(current_page.prefixedText, 'WIKI')
local coord_link = 'https://geohack.toolforge.org/geohack.php?pagename=' .. page_name .. '&params='

-- Helper: Convert decimal to degrees, minutes, and seconds
local function convert_dec2dms(coordinate, positive_suffix, negative_suffix, precision)
    local coord = tonumber(coordinate)
    if not coord then return "Invalid coordinate" end

    local suffix = coord >= 0 and positive_suffix or negative_suffix
    coord = math.abs(coord)

    local degrees = math.floor(coord)
    local minutes = math.floor((coord - degrees) * 60)
    local seconds = math.floor(((coord - degrees) * 60 - minutes) * 60)

    if precision == "d" then
        return string.format("%d°%s", degrees, suffix)
    elseif precision == "dm" then
        return string.format("%d°%d′%s", degrees, minutes, suffix)
    else -- Default to "dms"
        return string.format("%d°%d′%d″%s", degrees, minutes, seconds, suffix)
    end
end

-- Main coord function
function coordinates.coord(frame)
    local args = require('Module:Arguments').getArgs(frame)
    local lat = tonumber(args[1])
    local long = tonumber(args[2])
    local precision = args.format or "dms"
    local display = args.display or "inline"

    if not lat or not long then
        return '<strong class="error">Invalid latitude or longitude</strong>'
    end

    -- Convert coordinates
    local dms_lat = convert_dec2dms(lat, "N", "S", precision)
    local dms_long = convert_dec2dms(long, "E", "W", precision)

    local coords = dms_lat .. " " .. dms_long

    -- Build output
    local result = '<span class="geo">'
        .. '<span class="latitude">' .. dms_lat .. '</span> '
        .. '<span class="longitude">' .. dms_long .. '</span>'
        .. '</span>'

    if display == "title" then
        result = '[[Geographic coordinate system|Coordinates]]: ' .. result
    end

    return result
end

return coordinates