Module:Coordinates: Wersiýalaryň arasyndaky tapawut

Content deleted Content added
Gysgaça mazmuny ýok
Gysgaça mazmuny ýok
Setir 11:
{{#Invoke:Coordinates | dms2dec }} : Simple function for converting DMS format
to decimal degree format.
 
{{#Invoke:Coordinates | link }} : Export the link used to reach the tools
 
]]
 
local math_mod = require( "Module:Math" );
globalFrame = nil
local coordinates = {};
 
coordinates = {};
local current_page = mw.title.getCurrentTitle()
local page_name = mw.uri.encode( current_page.prefixedText, 'WIKI' );
local coord_link = '//tools.wmflabs.org/geohack/geohack.php?pagename=' .. page_name .. '&params='
 
--[[ Helper function, replacement for {{coord/display/title}} ]]
local function displaytitle (s, notes)
local l = "[[GeografikГеографические koordinatlarкоординаты|KoordinatyКоординаты]]: " .. s
local co = '<span id="coordinates">' .. l .. notes .. '</span>';
return '<span style="font-size: small;">' .. co .. '</span>';
end
 
--[[ Helper function, Replacement for {{coord/display/inline}} ]]
local function displayinline (s, notes)
return s .. notes
end
 
--[[ Helper function, used in detecting DMS formatting ]]
local function dmsTest = function(first, second)
local concatenated = first:upper() .. second:upper();
if type(first) ~= 'string' or type(second) ~= 'string' then
return nil
if concatenated == "NE" or concatenated == "NW" or concatenated == "SE" or concatenated == "SW" or
end
concatenated == "EN" or concatenated == "WN" or concatenated == "ES" or concatenated == "WS" then
local s = (first .. second):upper()
return true;
return s:find('^[NS][EW]$') or s:find('^[EW][NS]$')
end
return false;
end
 
--[[
parseDec
 
Transforms decimal format latitude and longitude into the a
--[[ Wrapper function to grab args, see Module:Arguments for this function's documentation. ]]
structure to be used in displaying coordinates
local function makeInvokeFunc(funcName)
]]
return function (frame)
function parseDec( lat, long, format )
local args = require('Module:Arguments').getArgs(frame, {
local coordinateSpec = {}
wrappers = 'Template:Coord'
local errors = {}
})
return coordinates[funcName](args)
if long == "" or long == nil then
end
return nil, {{"parseDec", "Пропущена долгота"}}
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, "&nbsp;с.&nbsp;ш.", "&nbsp;ю.&nbsp;ш.", mode) -- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-long"] = convert_dec2dms( long, "&nbsp;в.&nbsp;д.", "&nbsp;з.&nbsp;д.", mode) -- {{coord/dec2dms|{{{2}}}|E|W|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
if format ~= "" then
coordinateSpec.default = format
else
coordinateSpec.default = "dec"
end
 
return coordinateSpec, errors
end
 
--[[ Helper function, handle optional args. ]]
local function optionalArg(arg, supplementsuplement)
return if arg ~= nil and arg .. supplement~= or"" ''then
return string.format( "%02d", tonumber( arg ) ) .. suplement
end
return ""
end
 
--[[
parseDMS
Formats any error messages generated for display
 
Transforms degrees, minutes, seconds format latitude and longitude
into the a structure to be used in displaying coordinates
]]
function parseDMS( lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format )
local function errorPrinter(errors)
local resultcoordinateSpec = ""{}
for i,v in ipairs( local errors) do= {}
local errorHTML = '<strong class="error">Coordinates: ' .. v[2] .. '</strong>'
lat_f = lat_f:upper();
result = result .. errorHTML .. "<br />"
long_f = long_f:upper();
end
return result
-- Check if specified backward
end
if lat_f == 'E' or lat_f == 'W' then
local t_d, t_m, t_s, t_f;
t_d = lat_d;
t_m = lat_m;
t_s = lat_s;
t_f = lat_f;
lat_d = long_d;
lat_m = long_m;
lat_s = long_s;
lat_f = long_f;
long_d = t_d;
long_m = t_m;
long_s = t_s;
long_f = t_f;
end
errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', true );
if long_d == nil or long_d == "" then
table.insert(errors, {"parseDMS", "Пропущена долгота" })
end
coordinateSpec["dec-lat"] = convert_dms2dec(lat_f, lat_d, lat_m, lat_s) -- {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}}
coordinateSpec["dec-long"] = convert_dms2dec(long_f, long_d, long_m, long_s) -- {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}}
 
if lat_m == nil and lat_s == nil and long_m == nil and long_s == nil and #errors == 0
--[[
or math_mod._precision( lat_d ) > 0 or math_mod._precision( long_d ) > 0 then
Determine the required CSS class to display coordinates
if lat_f:upper() == 'S' then
lat_d = '-' .. lat_d;
lat_f = "&nbsp;ю.&nbsp;ш.";
else
lat_f = "&nbsp;с.&nbsp;ш.";
end
if long_f:upper() == 'W' then
long_d = '-' .. long_d;
long_f = "&nbsp;з.&nbsp;д.";
else
long_f = "&nbsp;в.&nbsp;д.";
end
return parseDec( lat_d, long_d, format );
end
 
if lat_f:upper() == 'S' then
Usually geo-nondefault is hidden by CSS, unless a user has overridden this for himself
lat_f = "&nbsp;ю.&nbsp;ш.";
default is the mode as specificied by the user when calling the {{coord}} template
else
mode is the display mode (dec or dms) that we will need to determine the css class for
lat_f = "&nbsp;с.&nbsp;ш.";
]]
end
local function displayDefault(default, mode)
 
if default == "" then
if long_f:upper() == 'E' then
default = "dec"
long_f = "&nbsp;в.&nbsp;д.";
end
else
long_f = "&nbsp;з.&nbsp;д.";
if default == mode then
end
return "geo-default"
 
else
coordinateSpec["dms-lat"] = lat_d.."°"..optionalArg(lat_m,"′") .. optionalArg(lat_s,"″") .. lat_f
return "geo-nondefault"
coordinateSpec["dms-long"] = long_d.."°"..optionalArg(long_m,"′") .. optionalArg(long_s,"″") .. long_f
end
 
if format ~= "" then
coordinateSpec.default = format
else
coordinateSpec.default = "dms"
end
 
return coordinateSpec, errors
end
 
Line 97 ⟶ 167:
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задан emptyparam"
end
if args["name"] ~= "" and args["name"] ~= nil then
uriComponents = uriComponents .. "&title=" .. mw.uri.encode(coordinateSpec["name"])
end
local geodmshtml = '<span class="geo-dms" title="Shu joy uchun xarita,Различные fazodanкарты olinganи suratlarсхемы vaдля boshqaэтого maʼlumotlarместа">'
.. '<span class="latitude">' .. coordinateSpec["dms-lat"] .. '</span> '
.. '<span class="longitude">' ..coordinateSpec["dms-long"] .. '</span>'
.. '</span>'
 
local lat = tonumber( coordinateSpec["dec-lat"] ) or 0
if lat < 0 then
local geodeclat
-- FIXME this breaks the pre-existing precision
if lat < 0 then
geodeclat = coordinateSpec["dec-lat"]:sub(2) .. "°&nbsp;ю.&nbsp;ш."
-- FIXME this breaks the pre-existing precision
else
geodeclat = tostring(coordinateSpec["dec-lat"]):sub(2) .. "°S"
geodeclat = (coordinateSpec["dec-lat"] or 0) .. "°&nbsp;с.&nbsp;ш."
else
end
geodeclat = (coordinateSpec["dec-lat"] or 0) .. "°N"
end
 
local long = tonumber( coordinateSpec["dec-long"] ) or 0
if long < 0 then
local geodeclong
-- FIXME does not handle unicode minus
if long < 0 then
geodeclong = coordinateSpec["dec-long"]:sub(2) .. "°&nbsp;з.&nbsp;д."
-- FIXME does not handle unicode minus
else
geodeclong = tostring(coordinateSpec["dec-long"]):sub(2) .. "°W"
geodeclong = (coordinateSpec["dec-long"] or 0) .. "°&nbsp;в.&nbsp;д."
else
end
geodeclong = (coordinateSpec["dec-long"] or 0) .. "°E"
end
local geodechtml = '<span class="geo-dec" title="Различные карты и схемы для этого места">'
.. geodeclat .. ' '
local geodechtml = '<span class="geo-dec" title="Bu yer kartasy">'
.. geodeclong
.. geodeclat .. ' '
.. '</span>'
.. geodeclong
.. '</span>'
 
local geonumhtml = '<span class="geo">'
.. coordinateSpec["dec-lat"] .. '; '
.. coordinateSpec["dec-long"]
.. '</span>'
 
local inner;
local inner = '<span class="' .. displayDefault(coordinateSpec["default"], "dms" ) .. '">' .. geodmshtml .. '</span>'
inner = '<span class="geo-geo-' .. coordinateSpec.default .. '"><span class="geo-multi-punctdms">&#xfeff;' /.. &#xfeff;geodmshtml .. '</span>'
.. '<span class="' .. displayDefault(coordinateSpec[geo-multi-punct"default"],>&#xfeff; "dec"/ ) .. '"&#xfeff;</span>';
.. '<span class="geo-dec">';
 
if notargs["name"] == "" or args["name"] == nil then
inner = inner .. geodechtml
.. '<span style="display:none">&#xfeff; / ' .. geonumhtml .. '</span></span></span>'
else
inner = inner .. '<span class="vcard">' .. geodechtml
.. '<span style="display:none">&#xfeff; / ' .. geonumhtml .. '</span>'
.. '<span style="display:none">&#xfeff; (<span class="fn org">'
.. args["name"] .. '</span>)</span></span></span></span>'
end
 
return local result = '<span class="plainlinks nourlexpansion">' .. globalFrame:preprocess(
'[//tools.wmflabs.org/geohack/geohack.php?language=ru&pagename={{FULLPAGENAMEE}}&params=' ..
'[' .. coord_link .. uriComponents .. ' ' .. inner .. ']' .. '</span>'
uriComponents .. ' ' .. inner .. ']')
end
 
local globe = string.lower(args.globe or '')
--[[ Helper function, convert decimal to degrees ]]
local scale = tonumber(args.scale or '30000')
local function convert_dec2dms_d(coordinate)
local nogoogle = string.lower(args.nogoogle or '')
local d = math_mod._round( coordinate, 0 ) .. "°"
local noosm = string.lower(args.noosm or '')
return d .. ""
local yandex = string.lower(args.yandex or '')
end
 
if globe == '' then
--[[ Helper function, convert decimal to degrees and minutes ]]
if nogoogle == '' then
local function convert_dec2dms_dm(coordinate)
result = result .. '&nbsp;<sup class="geo-google noprint" title="Это место на Картах Google">[//maps.google.com/maps?'
coordinate = math_mod._round( coordinate * 60, 0 );
.. 'll=' .. coordinateSpec["dec-lat"] .. ',' .. coordinateSpec["dec-long"]
local m = coordinate % 60;
.. '&q=' .. coordinateSpec["dec-lat"] .. ',' .. coordinateSpec["dec-long"]
coordinate = math.floor( (coordinate - m) / 60 );
.. '&spn=' .. (scale / 1000000) .. ',' .. (scale / 1000000)
local d = coordinate % 360 .."°"
.. '&t=h&hl=ru (G)]'
.. '</sup>'
return d .. string.format( "%02d′", m )
end
if noosm == '' then
 
result = result .. '&nbsp;<sup class="geo-osm noprint" title="Это место на карте OpenStreetMap">[http://www.openstreetmap.org/?'
--[[ Helper function, convert decimal to degrees, minutes, and seconds ]]
.. 'mlat=' .. coordinateSpec["dec-lat"] .. '&mlon=' .. coordinateSpec["dec-long"]
local function convert_dec2dms_dms(coordinate)
coordinate = math_mod._round( coordinate * 60 * 60, 0 );
if scale < 2000 then result = result .. '&zoom=18'
local s = coordinate % 60
elseif scale < 5000 then result = result .. '&zoom=17'
coordinate = math.floor( (coordinate - s) / 60 );
elseif scale < 10000 then result = result .. '&zoom=16'
local m = coordinate % 60
elseif scale < 20000 then result = result .. '&zoom=15'
coordinate = math.floor( (coordinate - m) / 60 );
elseif scale < 40000 then result = result .. '&zoom=14'
local d = coordinate % 360 .."°"
elseif scale < 80000 then result = result .. '&zoom=13'
elseif scale < 160000 then result = result .. '&zoom=12'
elseif scale < 320000 then result = result .. '&zoom=11'
elseif scale < 640000 then result = result .. '&zoom=10'
else result = result .. '&zoom=9'
end
result = result .. ' (O)]</sup>'
end
 
if yandex ~= '' then
return d .. string.format( "%02d′", m ) .. string.format( "%02d″", s )
result = result .. '&nbsp;<sup class="geo-yandex noprint" title="Это место на Яндекс.Картах">[//maps.yandex.ru/'
.. '?ll=' .. coordinateSpec["dec-long"] .. ',' .. coordinateSpec["dec-lat"]
.. '&pt=' .. coordinateSpec["dec-long"] .. ',' .. coordinateSpec["dec-lat"]
.. '&spn=' .. (scale / 1000000) .. ',' .. (scale / 1000000)
.. '&l=' .. 'sat,skl'
.. ' (Я)]</sup>'
end
end
result = result .. '</span>'
return result
end
 
--[[
Formats any error messages generated for display
Helper function, convert decimal latitude or longitude to
degrees, minutes, and seconds format based on the specified precision.
]]
function errorPrinter(errors)
local function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
local coordresult = tonumber(coordinate)""
for i,v in ipairs(errors) do
local postfix
local errorHTML = '<strong class="error">Координаты: ' .. v[2] .. '</strong>'
if coord >= 0 then
result = result .. errorHTML .. "<br />"
postfix = firstPostfix
end
else
return result
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
 
--[[
Determine the required CSS class to display coordinates
Convert DMS format into a N or E decimal coordinate
 
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 {{coord}} template
mode is the display mode (dec or dms) that we will need to determine the css class for
]]
function displayDefault(default, mode)
local function convert_dms2dec(direction, degrees_str, minutes_str, seconds_str)
if default == "" then
local degrees = tonumber(degrees_str)
default = "dec"
local minutes = tonumber(minutes_str) or 0
end
local seconds = tonumber(seconds_str) or 0
if default == mode then
local factor = 1
return "geo-default"
if direction == "S" or direction == "W" then
else
factor = -1
return "geo-nondefault"
end
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
 
--[[
ChecksCheck the input valuesarguments for coord to fordetermine outthe kind of rangedata being errors.provided
and then make the necessary processing.
]]
function formatTest(args)
local function validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, source, strong )
local result, errors;
local errors = {};
local primary = false;
lat_d = tonumber( lat_d ) or 0;
lat_m = tonumber( lat_m ) or 0;
local param = {}
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 strongargs[1] == "" then
-- no lat logic
if lat_d < 0 then
return errorPrinter( {{"formatTest", "Пропущена широта"}} )
table.insert(errors, {source, "latitude degrees < 0 with hemisphere flag"})
elseif args[4] == "" and args[5] == "" and args[6] == "" then
end
-- dec logic
if long_d < 0 then
result, errors = parseDec( args[1], args[2], args['format'] )
table.insert(errors, {source, "longitude degrees < 0 with hemisphere flag"})
if result == nil then
end
return errorPrinter( errors );
--[[
end
#coordinates is inconsistent about whether this is an error. If globe: is
param = { args[1], "N", args[2], "E", args[3] };
specified, it won't error on this condition, but otherwise it will.
elseif dmsTest(args[4], args[8]) then
-- dms logic
For not simply disable this check.
result, errors = parseDMS( args[1], args[2], args[3], args[4],
args[5], args[6], args[7], args[8], args['format'] )
if long_d > 180 then
param = { args[1], args[2], args[3], args[4], args[5],
table.insert(errors, {source, "longitude degrees > 180 with hemisphere flag"})
args[6], args[7], args[8], args[9] };
end
if args[10] ~= '' then
]]
table.insert( errors, { 'formatTest', 'Неожиданные дополнительные параметры' } );
end
end
elseif dmsTest(args[3], args[6]) then
if lat_d > 90 then
-- dm logic
table.insert(errors, {source, "latitude degrees > 90"})
result, errors = parseDMS( args[1], args[2], nil, args[3],
end
args[4], args[5], nil, args[6], args['format'] )
if lat_d < -90 then
param = { args[1], args[2], args[3], args[4], args[5], args[6], args[7] };
table.insert(errors, {source, "latitude degrees < -90"})
if args[8] ~= '' then
end
table.insert( errors, { 'formatTest', 'Неожиданные дополнительные параметры' } );
if lat_m >= 60 then
end
table.insert(errors, {source, "latitude minutes >= 60"})
elseif dmsTest(args[2], args[4]) then
end
-- d logic
if lat_m < 0 then
result, errors = parseDMS( args[1], nil, nil, args[2],
table.insert(errors, {source, "latitude minutes < 0"})
args[3], nil, nil, args[4], args['format'] )
end
param = { args[1], args[2], args[3], args[4], args[5] };
if lat_s >= 60 then
if args[6] ~= '' then
table.insert(errors, {source, "latitude seconds >= 60"})
table.insert( errors, { 'formatTest', 'Неожиданные дополнительные параметры' } );
end
end
if lat_s < 0 then
else
table.insert(errors, {source, "latitude seconds < 0"})
-- Error
end
return errorPrinter( {{"formatTest", "Неизвестный формат аргумента"}} )
if long_d >= 360 then
end
table.insert(errors, {source, "longitude degrees >= 360"})
result.name = args["name"]
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
 
local last = table.getn (param)
--[[
if param[last] == '' then
parseDec
table.remove(param, last)
end
 
local extra_param = { 'dim', 'globe', 'scale', 'region', 'source', 'type' }
Transforms decimal format latitude and longitude into the a
for _, v in ipairs( extra_param ) do
structure to be used in displaying coordinates
if (args[v] or '') ~= '' then
]]
table.insert( param, v .. ':' .. args[v] );
local function parseDec( lat, long, format )
end
local coordinateSpec = {}
end
local errors = {}
if not long then
return nil, {{"parseDec", "Missing longitude"}}
elseif not tonumber(long) then
return nil, {{"parseDec", "Longitude could not be parsed as a number: " .. long}}
end
errors = validate( lat, nil, nil, long, nil, nil, 'parseDec', false );
coordinateSpec["dec-lat"] = lat;
coordinateSpec["dec-long"] = long;
 
result.param = table.concat( param , '_' );
local mode = coordinates.determineMode( lat, long );
coordinateSpec["dms-lat"] = convert_dec2dms( lat, "N", "S", mode) -- {{coord/dec2dms|{{{1}}}|N|S|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
coordinateSpec["dms-long"] = convert_dec2dms( long, "E", "W", mode) -- {{coord/dec2dms|{{{2}}}|E|W|{{coord/prec dec|{{{1}}}|{{{2}}}}}}}
if format then
coordinateSpec.default = format
else
coordinateSpec.default = "dec"
end
 
if #errors == 0 then
return coordinateSpec, errors
return specPrinter( args, result )
else
return specPrinter( args, result ) .. " " .. errorPrinter(errors) .. '[[Категория:Википедия:Статьи с ошибочными параметрами координат]]';
end
end
 
--[[
Helper function, convert decimal latitude or longitude to
parseDMS
degrees, minutes, and seconds format based on the specified precision.
 
Transforms degrees, minutes, seconds format latitude and longitude
into the a structure to be used in displaying coordinates
]]
function convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
local function parseDMS( lat_d, lat_m, lat_s, lat_f, long_d, long_m, long_s, long_f, format )
local coord = tonumber(coordinate) or 0
local coordinateSpec = {}
local postfix
local errors = {}
if coord >= 0 then
postfix = firstPostfix
lat_f = lat_f:upper();
else
long_f = long_f:upper();
postfix = secondPostfix
end
-- Check if specified backward
if lat_f == 'E' or lat_f == 'W' then
local t_d, t_m, t_s, t_f;
t_d = lat_d;
t_m = lat_m;
t_s = lat_s;
t_f = lat_f;
lat_d = long_d;
lat_m = long_m;
lat_s = long_s;
lat_f = long_f;
long_d = t_d;
long_m = t_m;
long_s = t_s;
long_f = t_f;
end
errors = validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, 'parseDMS', true );
if not long_d then
return nil, {{"parseDMS", "Missing longitude" }}
elseif not tonumber(long_d) then
return nil, {{"parseDMS", "Longitude could not be parsed as a number:" .. long_d }}
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) -- {{coord/dms2dec|{{{4}}}|{{{1}}}|0{{{2}}}|0{{{3}}}}}
coordinateSpec["dec-long"] = convert_dms2dec(long_f, long_d, long_m, long_s) -- {{coord/dms2dec|{{{8}}}|{{{5}}}|0{{{6}}}|0{{{7}}}}}
 
precision = precision:lower();
if format then
if precision == "dms" then
coordinateSpec.default = format
return convert_dec2dms_dms( math.abs( coord ) ) .. postfix;
else
elseif precision == "dm" then
coordinateSpec.default = "dms"
return convert_dec2dms_dm( math.abs( coord ) ) .. postfix;
end
elseif precision == "d" then
return convert_dec2dms_d( math.abs( coord ) ) .. postfix;
end
end
 
--[[ Helper function, convert decimal to degrees ]]
return coordinateSpec, errors
function convert_dec2dms_d(coordinate)
local d = math_mod._round( coordinate, 0 ) .. "°"
return d .. ""
end
 
--[[ Helper function, convert decimal to degrees and minutes ]]
--[[
function convert_dec2dms_dm(coordinate)
Check the input arguments for coord to determine the kind of data being provided
coordinate = math_mod._round( coordinate * 60, 0 );
and then make the necessary processing.
local m = coordinate % 60;
]]
coordinate = math.floor( (coordinate - m) / 60 );
local function formatTest(args)
local d = coordinate % 360 .."°"
local result, errors
local primary = false
return d .. string.format( "%02d′", m )
end
 
--[[ Helper function, convert decimal to degrees, minutes, and seconds ]]
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 )
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( {{"formatTest", "Missing latitude"}} )
elseif not tonumber(args[1]) then
-- bad lat logic
return errorPrinter( {{"formatTest", "Unable to parse latitude as a number:" .. args[1]}} )
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
result.param = table.concat({args[1], 'N', args[2] or '', 'E', args[3] or ''}, '_')
elseif dmsTest(args[4], args[8]) then
-- dms logic
result, errors = 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 = 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 = 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({{"formatTest", "Unknown argument format"}})
end
result.name = args.name
local param = {}
local extra_param = {'dim', 'globe', 'scale', 'region', 'source', 'type'}
for _, v in ipairs(extra_param) do
if (args[v] or '') ~= '' then
table.insert(param, v .. ':' .. args[v] );
end
end
if result.param ~= nil then
result.param = result.param .. table.concat( param , '_' );
end
local ret = specPrinter(args, result)
if #errors > 0 then
ret = ret .. ' ' .. errorPrinter(errors) .. '[[Category:Pages with malformed coordinate tags]]'
end
return ret
end
 
--[[
Convert DMS format into a N or E decimal coordinate
Generate Wikidata tracking categories.
]]
function convert_dms2dec(direction, degrees_str, minutes_str, seconds_str)
local function makeWikidataCategories()
local degrees = tonumber(degrees_str) or 0
local ret
local minutes = tonumber(minutes_str) or 0
if mw.wikibase and current_page.namespace == 0 then
local seconds = tonumber(seconds_str) or 0
local entity = mw.wikibase.getEntityObject()
if entity and entity.claims and entity.claims.P625 and entity.claims.P625[1] then
local factor
local snaktype = entity.claims.P625[1].mainsnak.snaktype
direction = mw.ustring.gsub(direction, '^[ ]*(.-)[ ]*$', '%1');
if snaktype == 'value' then
if direction == "S" or direction == "W" then
-- coordinates exist both here and on Wikidata, and can be compared.
factor = -1
ret = 'Koordinatalar Commonsda'
else
elseif snaktype == 'somevalue' then
factor = 1
ret = 'Coordinates on Wikidata set to unknown value'
end
elseif snaktype == 'novalue' then
ret = 'Coordinates on Wikidata set to no value'
local precision = 0
end
if seconds_str ~= nil and seconds_str ~= '' then
else
precision = 5 + math.max( math_mod._precision(seconds_str), 0 );
-- We have to either import the coordinates to Wikidata or remove them here.
elseif minutes_str ~= nil and minutes_str ~= '' then
ret = 'Koordinatalar Commonsda yok'
precision = 3 + math.max( math_mod._precision(minutes_str), 0 );
end
else
end
precision = math.max( math_mod._precision(degrees_str), 0 );
if ret then
end
return string.format('[[Category:%s]]', ret)
else
local decimal = factor * (degrees+(minutes+seconds/60)/60)
return ''
return string.format( "%." .. precision .. "f", decimal ) -- not tonumber since this whole thing is string based.
end
end
 
--[[
Checks input values to for out of range errors.
link
 
Simple function to export the coordinates link for other uses.
 
Usage:
{{ Invoke:Coordinates | link }}
]]
function validate( lat_d, lat_m, lat_s, long_d, long_m, long_s, source, strong )
function coordinates.link(frame)
local errors = {};
return coord_link;
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, "градусы широты < 0"})
end
if long_d < 0 then
table.insert(errors, {source, "градусы долготы < 0"})
end
--[[
#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, "градусы широты > 90"})
end
if lat_d < -90 then
table.insert(errors, {source, "градусы широты < -90"})
end
if lat_m >= 60 then
table.insert(errors, {source, "минуты широты >= 60"})
end
if lat_m < 0 then
table.insert(errors, {source, "минуты широты < 0"})
end
if lat_s >= 60 then
table.insert(errors, {source, "секунды широты >= 60"})
end
if lat_s < 0 then
table.insert(errors, {source, "секунды широты < 0"})
end
if long_d >= 360 then
table.insert(errors, {source, "градусы долготы >= 360"})
end
if long_d <= -360 then
table.insert(errors, {source, "градусы долготы <= -360"})
end
if long_m >= 60 then
table.insert(errors, {source, "минуты долготы >= 60"})
end
if long_m < 0 then
table.insert(errors, {source, "минуты долготы < 0"})
end
if long_s >= 60 then
table.insert(errors, {source, "секунды долготы >= 60"})
end
if long_s < 0 then
table.insert(errors, {source, "секунды долготы < 0"})
end
return errors;
end
 
Line 537 ⟶ 536:
 
Usage:
{{ 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
Line 545 ⟶ 544:
to use.
]]
function coordinates.dec2dms = makeInvokeFunc('_dec2dms'frame)
globalFrame = frame
function coordinates._dec2dms(args)
local coordinate = frame.args[1]
local firstPostfix = frame.args[2] or ''
local secondPostfix = frame.args[3] or ''
local precision = frame.args[4] or ''
 
return convert_dec2dms(coordinate, firstPostfix, secondPostfix, precision)
end
 
Line 560 ⟶ 559:
]]
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
 
--[[
Line 576 ⟶ 575:
 
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 = makeInvokeFunc('_dms2dec'frame)
globalFrame = frame
function coordinates._dms2dec(args)
local direction = frame.args[1]
local degrees = frame.args[2]
local minutes = frame.args[3]
local seconds = frame.args[4]
 
return convert_dms2dec(direction, degrees, minutes, seconds)
end
 
Line 599 ⟶ 598:
 
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 = makeInvokeFunc('_coord'frame)
globalFrame = frame
function coordinates._coord(args)
local args = frame.args
if not args[1] and not args[2] and mw.wikibase.getEntityObject() then
if args[1] == nil then
local entity = mw.wikibase.getEntityObject()
local pFrame = frame:getParent();
if entity
args = pFrame.args;
and entity.claims
for k,v in pairs( frame.args ) do
and entity.claims.P625
args[k] = v;
and entity.claims.P625[1].mainsnak.snaktype == 'value'
end
then
end
local precision = entity.claims.P625[1].mainsnak.datavalue.value.precision
args[1]=entity.claims.P625[1].mainsnak.datavalue.value.latitude
for i=1,10 do
args[2]=entity.claims.P625[1].mainsnak.datavalue.value.longitude
if precisionargs[i] == nil then
args[i] = ""
precision=-math_mod._round(math.log(precision)/math.log(10),0)
else
args[1]=math_mod._round(args[1],precision)
args[i] = args[i]:match( '^%s*(.-)%s*$' ); --remove whitespace
args[2]=math_mod._round(args[2],precision)
end
end
args['format'] = args['format'] or '';
end
local contents = formatTest(args)
local Notes = args.notes or ''""
local Display = argsstring.display and lower(args.display:lower() or '"inline'")
if Display == '' then
 
Display = 'inline';
local function isInline(s)
end
-- Finds whether coordinates are displayed inline.
return s:find('inline') ~= nil or s == 'i' or s == 'it' or s == 'ti'
local text = ''
end
if string.find( Display, 'inline' ) ~= nil or Display == 'i' or
local function isInTitle(s)
Display == 'it' or Display == 'ti' then
-- Finds whether coordinates are displayed in the title.
text = displayinline(contents, Notes)
return s:find('title') ~= nil or s == 't' or s == 'it' or s == 'ti'
end
if string.find( Display, 'title' ) ~= nil or Display == 't' or
Display == 'it' or Display == 'ti' then
local text = ''
text = text .. displaytitle(contents, Notes)
if isInline(Display) then
end
text = text .. displayinline(contents, Notes)
return text
end
if isInTitle(Display) then
text = text
.. displaytitle(contents, Notes)
.. makeWikidataCategories()
end
return text
end