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:  
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).  
coordinates.dms2dec = makeInvokeFunc('_dms2dec')
function coordinates._dms2dec(args)
local direction = args[1]
local degrees = args[2]
local minutes = args[3]
local seconds = args[4]  
return convert_dms2dec(direction, degrees, minutes, seconds)
end  
---
coord  
Main entry point for Lua function to replace  
Usage:  
...  
Refer to documentation page for many additional parameters and
configuration options.  
Note: This function provides the visual display elements of . In
order to load coordinates into the database, the parser
function must also be called, this is done automatically in the Lua
version of .  
coordinates.coord = makeInvokeFunc('_coord')
function coordinates._coord(args)
if not tonumber(args[1]) and not args[2] then
args[3] = args[1]; args[1] = nil
local entity = mw.wikibase.getEntityObject(args.qid)
if entity
and entity.claims
and entity.claims.P625
and entity.claims.P625[1].mainsnak.snaktype == 'value'
then
local precision = entity.claims.P625[1].mainsnak.datavalue.value.precision
args[1] = entity.claims.P625[1].mainsnak.datavalue.value.latitude
args[2] = entity.claims.P625[1].mainsnak.datavalue.value.longitude
if precision then
precision = -math_mod._round(math.log(precision)/math.log(10),0)
args[1] = math_mod._round(args[1],precision)
args[2] = math_mod._round(args[2],precision)
end
end
end  
local contents, backward = formatTest(args)
local Notes = args.notes or ''
local Display = args.display and args.display:lower or 'inline'  
-- it and ti are short for inline,title and title,inline
local function isInline(s)
-- Finds whether coordinates are displayed inline.
return s:find('inline') ~= nil or s == 'i' or s == 'it' or s == 'ti'
end
local function isInTitle(s)
-- Finds whether coordinates are displayed in the title.
return s:find('title') ~= nil or s == 't' or s == 'it' or s == 'ti'
end  
local function coord_wrapper(in_args)
-- Calls the parser function .
return mw.getCurrentFrame:callParserFunction('#coordinates', in_args) or ''
end  
local text = ''
if isInline(Display) then
text = text .. '<span>' .. contents .. Notes .. '</span>'
end
if isInTitle(Display) then
-- Add to output since indicator content is invisible to Lua later on
if not isInline(Display) then
text = text .. '<span>' .. contents .. Notes .. '</span>'
end
text = text .. displaytitle(contents .. Notes) .. makeWikidataCategories(args.qid)
end
if not args.nosave then
local page_title, count = mw.title.getCurrentTitle, 1
if backward then
local tmp = {}
while not string.find((args[count-1] or *), '[EW]') do tmp[count] = (args[count] or *); count = count+1 end
tmp.count = count; count = 2*(count-1)
while count >= tmp.count do table.insert(tmp, 1, (args[count] or '')); count = count-1 end
for i, v in ipairs(tmp) do args[i] = v end
else
while count <= 9 do args[count] = (args[count] or ''); count = count+1 end
end
if isInTitle(Display) and not page_title.isTalkPage and page_title.subpageText ~= 'doc' and page_title.subpageText ~= 'testcases' then args[10] = 'primary' end
args.notes, args.format, args.display = nil
text = text .. coord_wrapper(args)
end
return text
end  
---
coord2text  
Extracts a single value from a transclusion of .
IF THE GEOHACK LINK SYNTAX CHANGES THIS FUNCTION MUST BE MODIFIED.  
Usage:  
Valid values for the second parameter are: lat (signed integer), long (signed integer), type, scale, dim, region, globe, source  
function coordinates._coord2text(coord,type)
if coord == * or type == * or not type then return nil end
type = mw.text.trim(type)
if type == 'lat' or type == 'long' then
local result, negative = mw.text.split((mw.ustring.match(coord,'[%.%d]+°[NS] [%.%d]+°[EW]') or ''), ' ')
if type == 'lat' then
result, negative = result[1], 'S'
else
result, negative = result[2], 'W'
end
result = mw.text.split(result, '°')
if result[2] == negative then result[1] = '-'..result[1] end
return result[1]
else
return mw.ustring.match(coord, 'params=.-_' .. type .. ':(.-)[ _]')
end
end  
function coordinates.coord2text
return coordinates._coord2text(.args[1],.args[2])
end  
---
coordinsert  
Injects some text into the Geohack link of a transclusion of (if that text isn't already in the transclusion). Outputs the modified transclusion of .
IF THE GEOHACK LINK SYNTAX CHANGES THIS FUNCTION MUST BE MODIFIED.  
Usage:  
Do not make Geohack unhappy by inserting something which isn't mentioned in the documentation.  
function coordinates.coordinsert
-- for the 2nd or later integer parameter (the first is the coord template, as above)
for i, v in ipairs(.args) do
if i ~= 1 then
-- if we cannot find in the coord_template the i_th coordinsert parameter e.g. region
if not mw.ustring.find(.args[1], (mw.ustring.match(.args[i], '^(.-:)') or '')) then
-- find from the params= up to the first possibly-present underscore
-- and append the i_th coordinsert parameter and a space
-- IDK why we're adding a space but it does seem somewhat convenient
.args[1] = mw.ustring.gsub(.args[1], '(params=.-)_? ', '%1_'...args[i]..' ')
end
end
end
if .args.name then
-- if we can't find the vcard class
if not mw.ustring.find(.args[1], '<span>') then
-- take something that looks like a coord template and add the vcard span with class and fn org class
local namestr = .args.name
.args[1] = mw.ustring.gsub(
.args[1],
'(<span>)(<span[^<>]*>[^<>]*</span><span[^<>]*>[^<>]*<span[^<>]*>[^<>]*</span></span>)(</span>)',
'%1<span>%2<span>&#xfeff; (<span>' .. namestr .. '</span>)</span></span>%3'
-- then find anything from coordinates parameters to the 'end' and attach the title parameter
.args[1] = mw.ustring.gsub(
.args[1],
'(&params=[^&"<>%[%] ]*) ',
'%1&title=' .. mw.uri.encode(namestr) .. ' '
end
end  
-- replace the existing indicator with a new indicator using the modified content
.args[1] = mw.ustring.gsub(
.args[1],
'(<span]*">(.+)</span>)\127[^\127]*UNIQ%-%-indicator%-%x+%-%-?QINU[^\127]*\127',
function (inline, coord) return inline .. displaytitle(coord) end  
return .args[1]
end  
return coordinates