Module:Coordinates: Difference between revisions

Jump to navigation Jump to search
Content added Content deleted
(more formatting)
(more formatting / comments)
Line 1: Line 1:
--[[
--[[
This module is intended to replace the functionality of {{Coord}} and related
This module is intended to replace the functionality of {{Coord}} and related
tmeplates. It provides several methods, including
templates. It provides several methods, including


{{#Invoke:Coordinates | coord }}
{{#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: Line 138:
else
else
coordinateSpec.default = "dms"
coordinateSpec.default = "dms"
end
end


return coordinateSpec, errors
return coordinateSpec, errors
Line 238: Line 241:
end
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)
function formatTest(args)
local result, errors;
local result, errors;
Line 297: Line 303:
return specPrinter( args, result ) .. " " .. errorPrinter(errors) .. '[[Category:Pages with malformed coordinate tags]]';
return specPrinter( args, result ) .. " " .. errorPrinter(errors) .. '[[Category:Pages with malformed coordinate tags]]';
end
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
end


Line 327: Line 356:
end
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)
function convert_dms2dec(direction, degrees_str, minutes_str, seconds_str)
local degrees = tonumber(degrees_str) or 0
local degrees = tonumber(degrees_str) or 0
Line 379: Line 380:
end
end
-- nil -> 0
local decimal = factor * (degrees+(minutes+seconds/60)/60)
local decimal = factor * (degrees+(minutes+seconds/60)/60)
return string.format( "%." .. precision .. "f", decimal ) -- not tonumber since this whole thing is string based.
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
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 )
function validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, source, strong )
local errors = {};
local errors = {};
Line 455: Line 455:
end
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)
function coordinates.dec2dms(frame)
globalFrame = frame
globalFrame = frame
Line 473: Line 479:
end
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 )
function coordinates.determineMode( value1, value2 )
local precision = math.max( math_mod._precision( value1 ), math_mod._precision( value2 ) );
local precision = math.max( math_mod._precision( value1 ), math_mod._precision( value2 ) );
Line 484: Line 494:
end
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)
function coordinates.dms2dec(frame)
globalFrame = frame
globalFrame = frame
Line 495: Line 517:
end
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)
function coordinates.coord(frame)
globalFrame = frame
globalFrame = frame