Module:Coordinates: Difference between revisions

more formatting / comments
(more formatting)
(more formatting / comments)
Line 1:
--[[
This module is intended to replace the functionality of {{Coord}} and related
tmeplatestemplates. It provides several methods, including
 
{{#Invoke:Coordinates | coord }} : General function formatting and displaying
coordinate values.
 
{{#Invoke:Coordinates | dec2dms }} : Simple function for converting decimal
Which provides the functionality to generate a coordinate link equivalent to
degree values to DMS format.
{{Coord}}
 
{{#Invoke:Coordinates | dms2dec }} : Simple function for converting DMS format
to decimal degree format.
 
]]
Line 134 ⟶ 138:
else
coordinateSpec.default = "dms"
end
 
return coordinateSpec, errors
Line 238 ⟶ 241:
end
 
--[[
-- Check the arguments to determine what type of coordinates has been input
Check the input arguments for coord to determine the kind of data being provided
and then make the necessary processing.
]]
function formatTest(args)
local result, errors;
Line 297 ⟶ 303:
return specPrinter( args, result ) .. " " .. errorPrinter(errors) .. '[[Category:Pages with malformed coordinate tags]]';
end
end
 
--[[
Helper function, convert decimal latitude or longitude to
degrees, minutes, and seconds format based on the specified precision.
]]
function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
local coord = tonumber(coordinate) or 0
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
 
Line 327 ⟶ 356:
end
 
--[[
Convert DMS format into a N or E decimal coordinate
Helper function, convert decimal latitude or longitude to
degrees, minutes, and seconds format based on the specified precision.
]]
function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
-- {{Coord/dec2dms/{{{4}}}|{{#ifexpr:{{{1}}} >= 0||-}}{{{1}}}}}{{#ifexpr:{{{1}}} >= 0|{{{2}}}|{{{3}}}}}
local coord = tonumber(coordinate) or 0
local postfix
if coord >= 0 then
postfix = firstPostfix
else
postfix = secondPostfix
end
 
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
-- return "" .. globalFrame:expandTemplate{ title = 'coord/dec2dms', args = {coordinate, firstPostfix, secondPostfix, precision}}
end
 
--- Convert DMS into a N or E decimal coordinate
-- @param coordinate direction. either "N" "S" "E" or "W"
-- @param degrees: string or number
-- @param minutes: string or number
-- @param seconds: string or number
-- @return a number with the N or E normalized decimal coordinate of the input
function convert_dms2dec(direction, degrees_str, minutes_str, seconds_str)
local degrees = tonumber(degrees_str) or 0
Line 379 ⟶ 380:
end
-- nil -> 0
local decimal = factor * (degrees+(minutes+seconds/60)/60)
return string.format( "%." .. precision .. "f", decimal ) -- not tonumber since this whole thing is string based.
--return "" .. globalFrame:expandTemplate{ title = 'coord/dms2dec', args = {direction, degrees, minutes, seconds}}
end
 
--[[
-- Check the input for out of range errors.
Checks input values to for out of range errors.
]]
function validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, source, strong )
local errors = {};
Line 455:
end
 
--[[
dec2dms
 
Wrapper to allow templates to call dec2dms directly.
--- The display function we exposed to Module:Coordinates
function coordinates.input(frame)
globalFrame = frame;
return formatTest(frame.args)
end
 
Usage:
--- The dec2dms function exposed to Module:Coordinates
{{ Invoke:Coordinates | dec2dms | decimal_coordinate | positive_suffix |
negative_suffix | precision }}
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.
]]
function coordinates.dec2dms(frame)
globalFrame = frame
Line 473 ⟶ 479:
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 ) );
Line 484 ⟶ 494:
end
 
--[[
--- The dec2dms function exposed to Module:Coordinates
dms2dec
 
Wrapper to allow templates to call dms2dec directly.
 
Usage:
{{ Invoke:Coordinates | dms2dec | direction_flag | degrees |
minutes | seconds }}
Converts DMS values specified as degrees, minutes, seconds too decimal format.
direction_flag is one of N, S, E, W, and determines whether the output is
positive (i.e. N and E) or negative (i.e. S and W).
]]
function coordinates.dms2dec(frame)
globalFrame = frame
Line 495 ⟶ 517:
end
 
--[[
--- This is used by {{coord}}.
coord
 
Main entry point for Lua function to replace {{coord}}
 
Usage:
{{ Invoke:Coordinates | coord }}
{{ Invoke:Coordinates | coord | lat | long }}
{{ Invoke:Coordinates | coord | lat | lat_flag | long | long_flag }}
...
Refer to {{coord}} documentation page for many additional parameters and
configuration options.
Note: This function provides the visual display elements of {{coord}}. In
order to load coordinates into the database, the {{#coordinates:}} parser
function must also be called, this is done automatically in the Lua
version of {{coord}}.
]]
function coordinates.coord(frame)
globalFrame = frame
Anonymous user