---
This module is intended to replace the functionality of and related
templates. It provides several methods, including  
: General function formatting and displaying
coordinate values.  
: Simple function for converting decimal
degree values to DMS format.  
: Simple function for converting DMS format
to decimal degree format.  
: Export the link used to reach the tools  
require('strict')  
local math_mod = require("Module:Math")
local coordinates = {};
local isSandbox = mw.getCurrentFrame:getTitle:find('sandbox', 1, true);  
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 function, replacement for
local function displaytitle(coords)
return mw.getCurrentFrame:extensionTag{
name = 'indicator',
args = { name = 'coordinates' },
content = '<span id="coordinates">ஆள்கூறுகள்: ' .. coords .. '</span>'
}
end  
-- Helper function, used in detecting DMS formatting
local function dmsTest(first, second)
if type(first) ~= 'string' or type(second) ~= 'string' then
return nil
end
local s = (first .. second):upper
return s:find('^[NS][EW]$') or s:find('^[EW][NS]$')
end  
-- Wrapper function to grab args, see Module:Arguments for this function's documentation.
local function makeInvokeFunc(funcName)
return function
local args = require('Module:Arguments').getArgs(, {
wrappers = 'Template:Coord'
})
return coordinates[funcName](args, )
end
end  
-- Helper function, handle optional args.
local function optionalArg(arg, supplement)
return arg and arg .. supplement or ''
end  
---
Formats any error messages generated for display  
local function errorPrinter(errors)
local result = ""
for i,v in ipairs(errors) do
result = result .. '<strong>Coordinates: ' .. v[2] .. '</strong>
'
end
return result
end  
---
Determine the required CSS class to display coordinates  
Usually geo-nondefault is hidden by CSS, unless a user has overridden this for himself
default is the mode as specificied by the user when calling the template
mode is the display mode (dec or dms) that we will need to determine the css class for  
local function displayDefault(default, mode)
if default == "" then
default = "dec"
end  
if default == mode then
return "geo-default"
else
return "geo-nondefault"
end
end  
---
specPrinter  
Output formatter. Takes the structure generated by either parseDec
or parseDMS and formats it for inclusion on Wikipedia.  
local function specPrinter(args, coordinateSpec)
local uriComponents = coordinateSpec["param"]
if uriComponents == "" then
-- RETURN error, should never be empty or nil
return "ERROR param was empty"
end
if args["name"] then
uriComponents = uriComponents .. "&title=" .. mw.uri.encode(coordinateSpec["name"])
end  
local geodmshtml = '<span title="Maps, aerial photos, and other data for this location">'
.. '<span>' .. coordinateSpec["dms-lat"] .. '</span> '
.. '<span>' ..coordinateSpec["dms-long"] .. '</span>'
.. '</span>'  
local lat = tonumber( coordinateSpec["dec-lat"] ) or 0
local geodeclat
if lat < 0 then
-- FIXME this breaks the pre-existing precision
geodeclat = tostring(coordinateSpec["dec-lat"]):sub(2) .. "°S"
else
geodeclat = (coordinateSpec["dec-lat"] or 0) .. "°N"
end  
local long = tonumber( coordinateSpec["dec-long"] ) or 0
local geodeclong
if long < 0 then
-- FIXME does not handle unicode minus
geodeclong = tostring(coordinateSpec["dec-long"]):sub(2) .. "°W"
else
geodeclong = (coordinateSpec["dec-long"] or 0) .. "°E"
end  
local geodechtml = '<span title="Maps, aerial photos, and other data for this location">'
.. geodeclat .. ' '
.. geodeclong
.. '</span>'  
local geonumhtml = '<span>'
.. coordinateSpec["dec-lat"] .. '; '
.. coordinateSpec["dec-long"]
.. '</span>'  
local inner = '<spandefault"], "dms" ) .. '">' .. geodmshtml .. '</span>'
.. '<span>&#xfeff; / &#xfeff;</span>'
.. '<spandefault"], "dec" ) .. '">';  
if not args["name"] then
inner = inner .. geodechtml
.. '<span>&#xfeff; / ' .. geonumhtml .. '</span></span>'
else
inner = inner .. '<span>' .. geodechtml
.. '<span>&#xfeff; / ' .. geonumhtml .. '</span>'
.. '<span>&#xfeff; (<span>'
.. args["name"] .. '</span>)</span></span></span>'
end  
local stylesheetLink = 'Module:Coordinates' .. ( isSandbox and '/sandbox' or '' ) .. '/styles.css'
return mw.getCurrentFrame:extensionTag{
name = 'templatestyles', args = { src = stylesheetLink }
} .. '<span>[' .. coord_link .. uriComponents ..
' ' .. inner .. ']</span>' .. ''
end  
-- Helper function, convert decimal to degrees
local function convert_dec2dms_d(coordinate)
local d = math_mod._round( coordinate, 0 ) .. "°"
return d .. ""
end  
-- Helper function, convert decimal to degrees and minutes
local function convert_dec2dms_dm(coordinate)
coordinate = math_mod._round( coordinate * 60, 0 );
local m = coordinate % 60;
coordinate = math.floor( (coordinate - m) / 60 );
local d = coordinate % 360 .."°"  
return d .. string.format( "%02d′", m )
end  
-- Helper function, convert decimal to degrees, minutes, and seconds
local function convert_dec2dms_dms(coordinate)
coordinate = math_mod._round( coordinate * 60 * 60, 0 );
local s = coordinate % 60
coordinate = math.floor( (coordinate - s) / 60 );
local m = coordinate % 60
coordinate = math.floor( (coordinate - m) / 60 );
local d = coordinate % 360 .."°"  
return d .. string.format( "%02d′", m ) .. string.format( "%02d″", s )
end  
---
Helper function, convert decimal latitude or longitude to
degrees, minutes, and seconds format based on the specified precision.  
local function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
local coord = tonumber(coordinate)
local postfix
if coord >= 0 then
postfix = firstPostfix
else
postfix = secondPostfix
end  
precision = precision:lower;
if precision == "dms" then
return convert_dec2dms_dms( math.abs( coord ) ) .. postfix;
elseif precision == "dm" then
return convert_dec2dms_dm( math.abs( coord ) ) .. postfix;
elseif precision == "d" then
return convert_dec2dms_d( math.abs( coord ) ) .. postfix;
end
end  
---
Convert DMS format into a N or E decimal coordinate  
local function convert_dms2dec(direction, degrees_str, minutes_str, seconds_str)
local degrees = tonumber(degrees_str)
local minutes = tonumber(minutes_str) or 0
local seconds = tonumber(seconds_str) or 0  
local factor = 1
if direction == "S" or direction == "W" then
factor = -1
end  
local precision = 0
if seconds_str then
precision = 5 + math.max( math_mod._precision(seconds_str), 0 );
elseif minutes_str and minutes_str ~= '' then
precision = 3 + math.max( math_mod._precision(minutes_str), 0 );
else
precision = math.max( math_mod._precision(degrees_str), 0 );
end  
local decimal = factor * (degrees+(minutes+seconds/60)/60)
return string.format( "%." .. precision .. "f", decimal ) -- not tonumber since this whole thing is string based.
end  
---
Checks input values to for out of range errors.  
local function validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, source, strong )
local errors = {};
lat_d = tonumber( lat_d ) or 0;
lat_m = tonumber( lat_m ) or 0;
lat_s = tonumber( lat_s ) or 0;
long_d = tonumber( long_d ) or 0;
long_m = tonumber( long_m ) or 0;
long_s = tonumber( long_s ) or 0;  
if strong then
if lat_d < 0 then
table.insert(errors, {source, "latitude degrees < 0 with hemisphere flag"})
end
if long_d < 0 then
table.insert(errors, {source, "longitude degrees < 0 with hemisphere flag"})
end
---
1. coordinates is inconsistent about whether this is an error. If globe: is
specified, it won't error on this condition, but otherwise it will.  
For not simply disable this check.  
if long_d > 180 then
table.insert(errors, {source, "longitude degrees > 180 with hemisphere flag"})
end  
end  
if lat_d > 90 then
table.insert(errors, {source, "latitude degrees > 90"})
end
if lat_d < -90 then
table.insert(errors, {source, "latitude degrees < -90"})
end
if lat_m >= 60 then