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
table.insert(errors, {source, "latitude minutes >= 60"})
end
if lat_m < 0 then
table.insert(errors, {source, "latitude minutes < 0"})
end
if lat_s >= 60 then
table.insert(errors, {source, "latitude seconds >= 60"})
end
if lat_s < 0 then
table.insert(errors, {source, "latitude seconds < 0"})
end
if long_d >= 360 then
table.insert(errors, {source, "longitude degrees >= 360"})
end
if long_d <= -360 then
table.insert(errors, {source, "longitude degrees <= -360"})
end
if long_m >= 60 then
table.insert(errors, {source, "longitude minutes >= 60"})
end
if long_m < 0 then
table.insert(errors, {source, "longitude minutes < 0"})
end
if long_s >= 60 then
table.insert(errors, {source, "longitude seconds >= 60"})
end
if long_s < 0 then
table.insert(errors, {source, "longitude seconds < 0"})
end  
return errors;
end  
---
parseDec  
Transforms decimal format latitude and longitude into the
structure to be used in displaying coordinates  
local function parseDec( lat, long, format )
local coordinateSpec = {}
local errors = {}  
if not long then
return nil,
elseif not tonumber(long) then
return nil,
end  
errors = validate( lat, nil, nil, long, nil, nil, 'parseDec', false );
coordinateSpec["dec-lat"] = lat;
coordinateSpec["dec-long"] = long;  
local mode = coordinates.determineMode( lat, long );
coordinateSpec["dms-lat"] = convert_dec2dms( lat, "N", "S", mode) -- }
coordinateSpec["dms-long"] = convert_dec2dms( long, "E", "W", mode) -- }  
if format then
coordinateSpec.default = format
else
coordinateSpec.default = "dec"
end  
return coordinateSpec, errors
end  
---
parseDMS  
Transforms degrees, minutes, seconds format latitude and longitude
into the a structure to be used in displaying coordinates  
local function parseDMS( lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format )
local coordinateSpec, errors, backward = {}, {}  
lat_f = lat_f:upper;
long_f = long_f:upper;  
-- Check if specified backward
if lat_f == 'E' or lat_f == 'W' then
lat_d, long_d, lat_m, long_m, lat_s, long_s, lat_f, long_f, backward = long_d, lat_d, long_m, lat_m, long_s, lat_s, long_f, lat_f, true;
end  
errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', true );
if not long_d then
return nil,
elseif not tonumber(long_d) then
return nil,
end  
if not lat_m and not lat_s and not long_m and not long_s and #errors == 0 then
if math_mod._precision( lat_d ) > 0 or math_mod._precision( long_d ) > 0 then
if lat_f:upper == 'S' then
lat_d = '-' .. lat_d;
end
if long_f:upper == 'W' then
long_d = '-' .. long_d;
end  
return parseDec( lat_d, long_d, format );
end
end  
coordinateSpec["dms-lat"] = lat_d.."°"..optionalArg(lat_m,"′") .. optionalArg(lat_s,"″") .. lat_f
coordinateSpec["dms-long"] = long_d.."°"..optionalArg(long_m,"′") .. optionalArg(long_s,"″") .. long_f
coordinateSpec["dec-lat"] = convert_dms2dec(lat_f, lat_d, lat_m, lat_s) -- }
coordinateSpec["dec-long"] = convert_dms2dec(long_f, long_d, long_m, long_s) -- }  
if format then
coordinateSpec.default = format
else
coordinateSpec.default = "dms"
end  
return coordinateSpec, errors, backward
end  
---
Check the input arguments for coord to determine the kind of data being provided
and then make the necessary processing.  
local function formatTest(args)
local result, errors
local backward, primary = false, false  
local function getParam(args, lim)
local ret = {}
for i = 1, lim do
ret[i] = args[i] or ''
end
return table.concat(ret, '_')
end  
if not args[1] then
-- no lat logic
return errorPrinter
elseif not tonumber(args[1]) then
-- bad lat logic
return errorPrinter
elseif not args[4] and not args[5] and not args[6] then
-- dec logic
result, errors = parseDec(args[1], args[2], args.format)
if not result then
return errorPrinter(errors);
end
-- formatting for geohack: geohack expects D_N_D_E notation or D;D notation
-- wikiminiatlas doesn't support D;D notation
-- #coordinates parserfunction doesn't support negative decimals with NSWE
result.param = table.concat({
math.abs(tonumber(args[1])),
((tonumber(args[1]) or 0) < 0) and 'S' or 'N',
math.abs(tonumber(args[2])),
((tonumber(args[2]) or 0) < 0) and 'W' or 'E',
args[3] or ''}, '_')
elseif dmsTest(args[4], args[8]) then
-- dms logic
result, errors, backward = parseDMS(args[1], args[2], args[3], args[4],
args[5], args[6], args[7], args[8], args.format)
if args[10] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
end
if not result then
return errorPrinter(errors)
end
result.param = getParam(args, 9)
elseif dmsTest(args[3], args[6]) then
-- dm logic
result, errors, backward = parseDMS(args[1], args[2], nil, args[3],
args[4], args[5], nil, args[6], args['format'])
if args[8] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
end
if not result then
return errorPrinter(errors)
end
result.param = getParam(args, 7)
elseif dmsTest(args[2], args[4]) then
-- d logic
result, errors, backward = parseDMS(args[1], nil, nil, args[2],
args[3], nil, nil, args[4], args.format)
if args[6] then
table.insert(errors, {'formatTest', 'Extra unexpected parameters'})
end
if not result then
return errorPrinter(errors)
end
result.param = getParam(args, 5)
else
-- Error
return errorPrinter .. ''
end
result.name = args.name  
local extra_param = {'dim', 'globe', 'scale', 'region', 'source', 'type'}
for _, v in ipairs(extra_param) do
if args[v] then
table.insert(errors, {'formatTest', 'Parameter: "' .. v .. '=" should be "' .. v .. ':"' })
end
end  
local ret = specPrinter(args, result)
if #errors > 0 then
ret = ret .. ' ' .. errorPrinter(errors) .. ''
end
return ret, backward
end  
---
Generate Wikidata tracking categories.  
local function makeWikidataCategories(qid)
local ret
local qid = qid or mw.wikibase.getEntityIdForCurrentPage
if mw.wikibase and current_page.namespace == 0 then
if qid and mw.wikibase.entityExists(qid) and mw.wikibase.getBestStatements(qid, "P625") and mw.wikibase.getBestStatements(qid, "P625")[1] then
local snaktype = mw.wikibase.getBestStatements(qid, "P625")[1].mainsnak.snaktype
if snaktype == 'value' then
-- coordinates exist both here and on Wikidata, and can be compared.
ret = 'Coordinates on Wikidata'
elseif snaktype == 'somevalue' then
ret = 'Coordinates on Wikidata set to unknown value'
elseif snaktype == 'novalue' then
ret = 'Coordinates on Wikidata set to no value'
end
else
-- We have to either import the coordinates to Wikidata or remove them here.
ret = 'Coordinates not on Wikidata'
end
end
if ret then
return string.format('', ret)
else
return ''
end
end  
---
link  
Simple function to export the coordinates link for other uses.  
Usage:  
function coordinates.link
return coord_link;
end  
---
dec2dms  
Wrapper to allow templates to call dec2dms directly.  
Usage:  
decimal_coordinate is converted to DMS format. If positive, the positive_suffix
is appended (typical N or E), if negative, the negative suffix is appended. The
specified precision is one of 'D', 'DM', or 'DMS' to specify the level of detail
to use.  
coordinates.dec2dms = makeInvokeFunc('_dec2dms')
function coordinates._dec2dms(args)
local coordinate = args[1]
local firstPostfix = args[2] or ''
local secondPostfix = args[3] or ''
local precision = args[4] or ''  
return convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
end  
---
Helper function to determine whether to use D, DM, or DMS
format depending on the precision of the decimal input.  
function coordinates.determineMode( value1, value2 )
local precision = math.max( math_mod._precision( value1 ), math_mod._precision( value2 ) );
if precision <= 0 then
return 'd'
elseif precision <= 2 then
return 'dm';
else
return 'dms';
end
end  
---
dms2dec  
Wrapper to allow templates to call dms2dec directly.  
Usage: