Modulo:Coord: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
aggiorno dms2dec/dec2dms alle classi
non generare indicatore quando non va mostrato, fix per Special:Permalink/140491317#Anomalia_coordinate
 
(71 versioni intermedie di 9 utenti non mostrate)
Riga 1:
--[[
* Modulo perche implementareimplementa leil funzionalità ditemplate Coord.
*
* Traduce in lua:
* Template:Coord
* Template:Coord/input/dec
* Template:Coord/input/d
* Template:Coord/input/dm
* Template:Coord/input/dms
* Template:Coord/input/ERROR
* Template:Coord/input/error2
* Template:Coord/link
* Template:Coord/prec dec
* Template:Coord/dms2dec
* Template:Coord/dec2dms
* Template:Coord/dec2dms/d
* Template:Coord/dec2dms/d1
* Template:Coord/dec2dms/dm
* Template:Coord/dec2dms/dm1
* Template:Coord/dec2dms/dms
* Template:Coord/dec2dms/dms1
* Template:Precision1
]]
 
require('strict')
-- Variabili globali
local p = {} -- per l'esportazione delle funzioni del modulo
local args = {} -- argomenti passati al template
local errorTable = {} -- table per contenere gli errori da ritornare
 
local mWikidata = require('Modulo:Wikidata')
-- Import
local htmlBuildercfg = requiremw.loadData("'Modulo:HtmlBuilder"Coord/Configurazione')
local errorCategory = '[[Categoria:Errori di compilazione del template Coord]]'
 
-- =============================================================================
-- Configurazione
-- Funzioni di utilità
local cfg = mw.loadData("Modulo:Coord/Configurazione")
-- =============================================================================
 
-- Error handler per xpcall, formatta l'errore.
-------------------------------------------------------------------------------
--
-- Funzioni di utilità
-- @param {string} msg
-------------------------------------------------------------------------------
-- @return {string}
local function errhandler(msg)
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
return string.format('<div style="color:red">Il template {{Coord}} ha riscontrato degli errori ' ..
'([[Template:Coord|istruzioni]]):\n%s</div>%s', msg, cat)
end
 
-- RitornaRestituisce il numero arrotondato al numero di cifre decimali richiesto.
-- http://lua-users.org/wiki/SimpleRound
--
-- @param {number} num
-- @param {number} idp
-- @return {number}
local function round(num, idp)
local mult = 10^(idp or 0)
return math.floor(num * mult + 0.5) / mult
end
 
-- Restituisce la stringa "0 + numero" quando il numero è di una sola cifra, altrimenti lo stesso numero.
return math.floor(num * mult + 0.5) / mult
--
-- @param {number} num
-- @return {string}
local function padleft0(num)
return (num < 10 and '0' or '') .. num
end
 
-- Converte un numero in stringa senza usare la notazione scientifica, esempio tostring(0.00001).
-- Ritorna il numero di cifre decimali di un numero
--
local function getDecimalPlaces(num)
-- @param {number} num
local dec = tostring(num):match("%d+%.(%d+)")
-- @return {string}
local function numberToString(num)
-- la parentesi () extra serve per non restituire anche il gsub.count
return (string.format('%f', num):gsub('%.?0+$', ''))
end
 
-- Parsifica il parametro display e restituisce una table con chiavi inline, title e debug.
return dec and dec:len() or 0
--
-- @param {table} args
-- @return {table}
local function getDisplay(args)
return {
inline = not args.display or args.display == 'inline' or args.display == 'inline,title',
title = args.display == 'title' or args.display == 'inline,title',
debug = args.display == 'debug'
}
end
 
local function getZoom( extraparams )
-- Ritorna la stringa "0 + numero" quando il numero è di una sola cifra, altrimenti lo stesso numero
local scale = extraparams:match( '%f[%w]scale: ?(%d+)' )
local function padleft0(num)
if scale then
return num < 10 and ("0" .. num) or num
return math.floor(math.log10( 5 / tonumber( scale ) ) * 3 + 25)
end
 
local extraType = extraparams:match( '%f[%w]type: ?(%w+)' )
if extraType then
local zoomType = {
country = 5,
state = 6,
adm1st = 7,
adm2nd = 8,
city = 9,
isle = 10,
mountain = 10,
waterbody = 10,
airport = 12,
landmark = 13,
}
return zoomType[ extraType ]
end
return 13
end
 
-- Legge i parametri passati al modulo.
-------------------------------------------------------------------------------
--
-- Validazione parametri
-- @param {table} frame
-------------------------------------------------------------------------------
-- @return {table}
local function getArgs(frame)
local args = {}
 
-- copia i parametri ricevuti, eccetto quelli con nome valorizzati a stringa vuota
-- Aggiunge un messaggio di errore alla risposta come elenco puntato
for k, v in pairs(frame:getParent().args) do
local function dumpError(...)
if v ~= '' or tonumber(k) then
local arg = {...}
args[k] = string.gsub(v, '^%s*(.-)%s*$', '%1')
end
end
-- retrocompatibilità con una funzionalità nascosta del precedente template:
-- ignorava qualunque parametro posizionale vuoto dopo longitudine e parametri geohack
for i = #args, 1, -1 do
if args[i] == '' then
table.remove(args, i)
else
break
end
end
-- rimuove i parametri posizionali vuoti front to back fermandosi al primo non vuoto
while args[1] == '' do
table.remove(args, 1)
end
-- se l'utente non ha fornito lat e long con i posizionali ma con latdec e longdec
if (#args == 0 or (#args == 1 and not tonumber(args[1]))) and
tonumber(args.latdec) and tonumber(args.longdec) then
table.insert(args, 1, numberToString(args.latdec))
table.insert(args, 2, numberToString(args.longdec))
end
 
return args
table.insert(errorTable, "* ")
for _, val in ipairs(arg) do
table.insert(errorTable, val)
end
table.insert(errorTable, "\n")
end
 
-- =============================================================================
-- Ritorna una stringa contenente la lista degli errori,
-- Classe DecCoord
-- nel namespace principale aggiunge una categoria di warning
-- =============================================================================
local function getErrors()
local text = ""
 
-- La classe DecCoord rappresenta una coordinata (latitudine o longitudine) in gradi decimali.
if mw.title.getCurrentTitle().namespace == 0 then
text = "[[Categoria:" .. cfg.categorie["warning"] .. "]]\n"
end
 
local DecCoord = {}
text = text .. "<span style=\"color:red;\">Il template {{Coord}} ha riscontrato degli errori ([[Template:Coord|istruzioni]]):\n" ..
local DmsCoord = {} -- dichiarata qui per le conversioni
table.concat(errorTable) .. "</span>"
 
-- Costruttore della classe DecCoord.
return text
--
-- @param {string} deg - i gradi decimali, positivi o negativi, se negativi viene
-- cambiato il segno e la direzione cardinale eventualmente invertita
-- @param {string} card - la direzione cardinale (N|S|E|W)
-- @return {table} un nuovo oggetto DecCoord
function DecCoord:new(deg, card)
local self = {}
 
setmetatable(self, { __index = DecCoord,
__tostring = function(t) return self:__tostring() end,
__concat = function(t, t2) return tostring(t) .. tostring(t2) end })
 
self.deg = tonumber(deg)
if self.deg < 0 then
self.card = card == 'N' and 'S' or (card == 'E' and 'W' or card)
self.deg = -self.deg
else
self.card = card
end
 
return self
end
 
-- Richiamata automaticamente ogni volta che è richiesto un tostring o un concatenamento.
-- Riconosce il tipo di richiesta ("dec", "d", "dm" o "dms") e valida i parametri posizionali
--
-- Ritorna il tipo di richiesta o nil in caso di errore.
-- @return {string}
local function paramsParse()
function DecCoord:__tostring()
local paramType, paramName, paramMin, paramMax, reqFormat, prefix, num, str
return numberToString(self.deg) .. '°' .. self.card
local posArgs = 0
end
 
-- Restituisce i gradi con segno.
-- riconoscimento tipo di richiesta
--
for k, v in ipairs(args) do
-- @return {string}
posArgs = posArgs + 1
function DecCoord:getDeg()
end
local deg = self.deg if* posArgs((self.card == 2'N' or posArgsself.card =='E') 3and 1 or then-1)
return numberToString(deg)
reqFormat = "dec"
end
elseif posArgs == 4 or posArgs == 5 then
reqFormat = "d"
elseif posArgs == 6 or posArgs == 7 then
reqFormat = "dm"
elseif posArgs == 8 or posArgs == 9 then
reqFormat = "dms"
else
dumpError("Errato numero di parametri")
return nil
end
 
-- Restituisce un nuovo oggetto DmsCoord, convertendo in gradi/minuti/secondi.
-- validazione parametri posizionali
--
currFormat = cfg.params[reqFormat]
-- @return {table} un nuovo oggetto DmsCoord
for k, v in ipairs(args) do
function DecCoord:toDms()
if currFormat[k] then
local deg, min, sec
paramType = currFormat[k][1]
 
paramName = currFormat[k][2]
deg = round(self.deg * 3600, 2)
paramMin = currFormat[k][3]
sec = round(math.floor(deg) % 60 + deg - math.floor(deg), 2)
paramMax = currFormat[k][4]
deg = math.floor((deg - sec) / 60)
prefix = reqFormat .. " format: " .. paramName
min = deg % 60
-- valida un parametro di tipo numero
deg = math.floor((deg - min) / 60) % 360
if paramType == "number" then
 
num = tonumber(v)
return DmsCoord:new(deg, min, sec, self.card)
if num then
if num < paramMin then
dumpError(prefix, " < ", paramMin)
elseif num > paramMax then
dumpError(prefix, " > ", paramMax)
end
else
dumpError(prefix, " non è un numero")
end
-- valida un parametro di tipo stringa
elseif paramType == "string" then
if v ~= paramMin and v ~= paramMax then
dumpError(prefix, " diverso da ", paramMin, " e da ", paramMax)
end
end
end
end
return #errorTable == 0 and reqFormat or nil
end
 
-- =============================================================================
-------------------------------------------------------------------------------
-- classi DecCoord e Classe DmsCoord
-- =============================================================================
-------------------------------------------------------------------------------
 
-- RappresentaLa classe DmsCoord rappresenta una coordinata (latlatitudine o longlongitudine) in gradi decimali /minuti/secondi.
local DecCoord = {
deg = nil, card = nil
}
 
-- Costruttore della classe DmsCoord.
-- Rappresenta una coordinata (lat o long) in gradi/minuti/secondi
--
local DmsCoord = {
-- @param {string} deg - i gradi
deg = nil, min = nil, sec = nil, card = nil
-- @param {string} min - i minuti, può essere nil
}
-- @param {string} sec - i secondi, può essere nil
-- @param {string} card - la direzione cardinale (N|S|E|W)
-- @return {table} un nuovo oggetto DmsCoord
function DmsCoord:new(deg, min, sec, card)
local self = {}
 
setmetatable (self, { __index = DmsCoord,
-- Costruttore di DecCoord
__tostring = function(t) return self:__tostring() end,
-- Se deg è negativo viene cambiato di segno e la direzione cardinale eventualmente invertita
__concat = function(t, t2) return tostring(t) .. tostring(t2) end })
function DecCoord:new(deg, card)
local self = {}
setmetatable(self, { __index = DecCoord,
__tostring = function(t) return self:__tostring() end,
__concat = function(t, t2) return tostring(t) .. tostring(t2) end })
 
self.deg = tonumber(deg)
if self.degmin = <min 0and thentonumber(min)
self.cardsec = card == "N"sec and "S" or tonumber(card == "E" and "W" or cardsec)
self.degcard = -self.degcard
else
self.card = card
end
 
return self
end
 
-- Richiamata automaticamente ogni volta che è richiesto un tostring o un concatenamento.
--
function DecCoord:__tostring()
-- @return {string}
return self.deg .. "°" .. self.card
function DmsCoord:__tostring()
return self.deg .. '°' ..
(self.min and (padleft0(self.min) .. '′') or '') ..
(self.sec and (padleft0(self.sec) .. '″') or '') ..
self.card
end
 
-- RitornaRestituisce un nuovo oggetto DmsCoordDecCoord, convertendo in gradi/minuti/secondi decimali.
--
function DecCoord:toDms(dmsFormat)
-- @return {table} un nuovo oggetto DecCoord
local deg, min, sec
function DmsCoord:toDec()
local deg = round((self.deg + ((self.min or 0) + (self.sec or 0) / 60) / 60), 6)
return DecCoord:new(deg, self.card)
end
 
-- =============================================================================
-- Classe Coord
-- =============================================================================
 
-- La classe Coord è la classe principale del modulo.
-- Al suo interno ha un riferimento alla latitudine e longitudine in ogni formato.
 
local Coord = {}
 
-- Costruttore della classe Coord.
--
-- @param {table} args
-- @return {table} un nuovo oggetto Coord
function Coord:new(args)
local decLat, decLong, dmsLat, dmsLong
local display = getDisplay(args)
local self = { args = args }
 
setmetatable(self, { __index = Coord })
 
if args.from and display.title then
error('il parametro "from" è valido solo con display=inline', 3)
end
-- con display=title o con i parametri "prop" o "from"
-- legge le coordinate da P625 per utilizzarle o per confrontarle con quelle inserite
if getDisplay(self.args).title or self.args.prop or args.from then
self:_checkWikidata()
-- con "from", senza coordinate utente e su Wikidata non esegue i controlli successivi
if self.args.from and #self.args < 2 and not tonumber(args[1]) then
return self
end
end
 
-- identifica il tipo di chiamata
self:_checkRequestFormat()
 
-- in base al tipo di chiamata crea gli oggetti DecCoord o DmsCoord
if self.reqFormat == 'dec' then
-- {{coord|1.111|2.222}}
decLat = DecCoord:new(args[1], 'N')
decLong = DecCoord:new(args[2], 'E')
elseif self.reqFormat == 'd' then
-- {{coord|1.111|N|3.333|W}}
decLat = DecCoord:new(args[1], args[2])
decLong = DecCoord:new(args[3], args[4])
elseif self.reqFormat == 'dm' then
-- {{coord|1|2|N|4|5|W}}
dmsLat = DmsCoord:new(args[1], args[2], nil, args[3])
dmsLong = DmsCoord:new(args[4], args[5], nil, args[6])
elseif self.reqFormat == 'dms' then
-- {{coord|1|2|3|N|5|6|7|W}}
dmsLat = DmsCoord:new(args[1], args[2], args[3], args[4])
dmsLong = DmsCoord:new(args[5], args[6], args[7], args[8])
end
 
-- effettua le conversioni dec <=> dms
if self.reqFormat == 'dec' or self.reqFormat == 'd' then
dmsLat = decLat:toDms()
dmsLong = decLong:toDms()
-- rimuove secondi e minuti se zero e presenti in lat e long
if dmsLat.sec == 0 and dmsLong.sec == 0 then
dmsLat.sec, dmsLong.sec = nil, nil
if dmsLat.min == 0 and dmsLong.min == 0 then
dmsLat.min, dmsLong.min = nil, nil
end
end
elseif self.reqFormat == 'dm' or self.reqFormat == 'dms' then
decLat = dmsLat:toDec()
decLong = dmsLong:toDec()
end
-- se presente args.catuguali e non è stato usato Wikidata verifica se uguali
if args.catuguali and self.wdLat and self.wdLong and
self.wdCat == nil and
self.wdLat == round(decLat:getDeg(), 6) and
self.wdLong == round(decLong:getDeg(), 6) then
self.wdCat = '[[Categoria:P625 uguale su Wikidata]]'
end
 
self.decLat = decLat
if dmsFormat == "d" then
self.decLong = decLong
deg = round(self.deg, 0)
self.dmsLat = dmsLat
elseif dmsFormat == "dm" then
self.dmsLong = dmsLong
deg = round(self.deg * 60, 0)
min = deg % 60
deg = math.floor((deg - min) / 60)
elseif dmsFormat == "dms" then
deg = round(self.deg * 3600, 0)
sec = deg % 60
deg = math.floor((deg - sec) / 60)
min = deg % 60
deg = math.floor((deg - min) / 60) % 360
end
 
return DmsCoord:new(deg, min, sec, self.card)
end
 
-- Legge la P625 e la utilizza come latitudine e longitudine se non fornite dall'utente.
-- Ritorna i gradi con segno
function DecCoordCoord:getDeg_checkWikidata()
self.wdEntityId = self.args.from or mw.wikibase.getEntityIdForCurrentPage();
return self.deg * ((self.card == "N" or self.card =="E") and 1 or -1)
if self.args.prop then
self.wdLat = mWikidata._getQualifier({ self.args.prop, 'P625', coord = 'latitude', n = 1, nq = 1, from = self.wdEntityId })
self.wdLong = mWikidata._getQualifier({ self.args.prop, 'P625', coord = 'longitude', n = 1, nq = 1, from = self.wdEntityId })
else
self.wdLat = mWikidata._getProperty({ 'P625', coord = 'latitude', n = 1, from = self.wdEntityId })
self.wdLong = mWikidata._getProperty({ 'P625', coord = 'longitude', n = 1, from = self.wdEntityId })
end
if self.wdLat and self.wdLong then
self.wdLat = round(self.wdLat, 6)
self.wdLong = round(self.wdLong, 6)
-- se l'utente non ha fornito lat e long usa quelli di Wikidata
if #self.args == 0 or (#self.args == 1 and not tonumber(self.args[1])) then
table.insert(self.args, 1, numberToString(self.wdLat))
table.insert(self.args, 2, numberToString(self.wdLong))
self.wdCat = '[[Categoria:P625 letta da Wikidata]]'
end
else
self.wdCat = '[[Categoria:P625 assente su Wikidata]]'
end
end
 
-- Riconosce il tipo di richiesta: dec, d, dm o dms.
-- Costruttore di DmsCoord
function DmsCoordCoord:new_checkRequestFormat(deg, min, sec, card)
local selferrorTable = {}
setmetatable (self, { __index = DmsCoord,
__tostring = function(t) return self:__tostring() end,
__concat = function(t, t2) return tostring(t) .. tostring(t2) end })
 
-- riconoscimento tipo di richiesta
self.deg = tonumber(deg)
if #self.min =args min< and2 tonumber(min)then
error('* coordinate non specificate', 4)
self.sec = sec and tonumber(sec)
elseif #self.cardargs < =4 cardthen
self.reqFormat = 'dec'
elseif #self.args < 6 then
self.reqFormat = 'd'
elseif #self.args < 8 then
self.reqFormat = 'dm'
elseif #self.args < 10 then
self.reqFormat = 'dms'
else
error('* errato numero di parametri', 4)
end
 
-- con le richieste dm e dms verifica se ci sono parametri lasciati vuoti in modo valido.
return self
if self.reqFormat == 'dms' then
-- {{coord|1|2||N|5|6||E}} valido
if self.args[3] == '' and self.args[7] == '' then
table.remove(self.args, 7)
table.remove(self.args, 3)
self.reqFormat = 'dm'
-- {{coord|1|2|3|N|5|6||E}} non valido
elseif self.args[3] == '' or self.args[7] == '' then
error('* lat e long hanno diversa precisione', 4)
-- {{coord|1||3|N|5||7|E}} valido
elseif self.args[2] == '' and self.args[6] == '' then
self.args[2], self.args[6] = 0, 0
-- {{coord|1|2|3|N|5||7|E}} non valido
elseif self.args[2] == '' or self.args[6] == '' then
error('* lat e long hanno diversa precisione', 4)
end
end
if self.reqFormat == 'dm' then
-- {{coord|1||N|4||E}} valido
if self.args[2] == '' and self.args[5] == '' then
table.remove(self.args, 5)
table.remove(self.args, 2)
self.reqFormat = 'd'
-- {{coord|1|2|N|4||E}} non valido
elseif self.args[2] == '' or self.args[5] == '' then
error('* lat e long hanno diversa precisione', 4)
end
end
 
-- validazione parametri posizionali
local currFormat = cfg.params[self.reqFormat]
local globe = self.args[#self.args]:match('globe:(%w+)')
self.isEarth = not globe or globe == 'earth'
for k, v in ipairs(self.args) do
if currFormat[k] then
local err
local parType = currFormat[k][1]
local parName = currFormat[k][2]
local parMin = currFormat[k][3]
local parMax = currFormat[k][4]
-- valida un parametro di tipo numero
if parType == 'number' then
local num = tonumber(v)
if num then
if self.isEarth and num < parMin then
err = string.format('* %s format: %s < %s', self.reqFormat, parName, parMin)
elseif self.isEarth and math.floor(num) > parMax then
err = string.format('* %s format: %s > %s', self.reqFormat, parName, parMax)
end
else
err = string.format('* %s format: %s non è un numero', self.reqFormat, parName)
end
-- valida un parametro di tipo stringa
elseif parType == 'string' then
if v ~= parMin and v ~= parMax then
err = string.format('* %s format: %s diverso da %s e da %s',
self.reqFormat, parName, parMin, parMax)
end
end
if err then
table.insert(errorTable, err)
end
end
end
 
if #errorTable > 0 then
error(table.concat(errorTable, '\n'), 4)
end
end
 
-- Utilizza l'estensione [[mw:Extension:GeoData]].
-- Richiamata automaticamente ogni volta che è richiesto un tostring o un concatenamento
--
function DmsCoord:__tostring()
-- @param {table} display
return self.deg .. "°" ..
-- @return {string}
(self.min and (padleft0(self.min) .. "′") or "") ..
function Coord:_setGeoData(display)
(self.sec and (padleft0(self.sec) .. "″") or "") ..
local gdStr = string.format('{{#coordinates:%s|%s|name=%s}}',
(self.card and self.card or "")
table.concat(self.args, '|'),
(display.title and mw.title.getCurrentTitle().namespace == 0) and 'primary' or '',
self.args.name or '')
return mw.getCurrentFrame():preprocess(gdStr)
end
 
-- Funzione di debug, restituisce latitudine e longitudine in entrambi i formati.
-- Ritorna un nuovo oggetto DecCoord, convertendo in gradi decimali
--
function DmsCoord:toDec()
-- @return {string}
local roundval, deg
function Coord:getDebugCoords()
-- con args.from restitusce una stringa vuota se non c'è nessun dato
roundval = (self.sec and 5 or (self.min and 3 or 0)) + getDecimalPlaces(self.sec or self.min or self.deg)
if deg = round((self.degargs.from +and ((#self.minargs or< 0)2 +and not tonumber(self.sec or 0args[1]) / 60) / 60), roundval)then
return ''
end
return DecCoord:new(deg, self.card)
return self.decLat .. ' ' .. self.decLong .. ' ' .. self.dmsLat .. ' ' .. self.dmsLong
end
 
-- Restituisce l'HTML di un elemento <maplink> di Kartographer per le coordinate
-------------------------------------------------------------------------------
--
-- Coord
-- @return {string}
-------------------------------------------------------------------------------
function Coord:_buildMaplinkHTML()
local defaultFormat
if self.args.format then
defaultFormat = self.args.format
elseif self.reqFormat == 'dec' then
defaultFormat = 'dec'
else
defaultFormat = 'dms'
end
 
local linkText;
-- Usato nelle conversioni da gradi decimali a gradi/minuti/secondi,
if defaultFormat == 'dec' then
-- dato il numero di decimali maggiore tra latdec e longdec ritorna il formato adatto:
linkText = self.decLat .. ' ' .. self.decLong
-- "d" per 0 decimali
else
-- "dm" per 1-2 decimali
linkText = tostring(self.dmsLat) .. ' ' .. tostring(self.dmsLong)
-- "dms" per > 3 decimali
end
local function getDmsFormat(latdec, longdec)
local d1, d2, max
d1 = getDecimalPlaces(latdec)
d2 = getDecimalPlaces(longdec)
max = d1 > d2 and d1 or d2
 
local jsonParams = {
return max == 0 and "d" or
type = 'Feature',
((max == 1 or max == 2) and "dm" or "dms")
geometry = {
end
type ='Point',
coordinates = {
round( self.decLong:getDeg(), 6 ), -- max precision in GeoJSON format
round( self.decLat:getDeg(), 6 )
}
},
properties = {
['marker-color'] = "228b22",
}
}
 
if self.wdEntityId then
-- Crea l'HTML ritornato da Coord
-- geoshape da Wikidata
local function buildHTML(decLat, decLong, dmsLat, dmsLong, param, defaultFormat)
jsonParams = {
local root, text, url, displayInline, displayTitle, htmlTitle
jsonParams,
{
type = 'ExternalData',
service = 'geoshape',
ids = self.wdEntityId,
properties = {
['fill-opacity'] = 0.2
}
}
}
end
 
return mw.getCurrentFrame():extensionTag{
-- geohack url e parametri
name = 'maplink',
url = cfg.geohackUrl ..
content = mw.text.jsonEncode( jsonParams ),
"&pagename=" .. mw.uri.encode(mw.title.getCurrentTitle().prefixedText, "WIKI") ..
args = {
"&params=" .. param ..
text = linkText,
(args["name"] and ("&title=" .. mw.uri.encode(args["name"])) or "")
zoom = getZoom( self.args[#self.args] or '' ),
latitude = self.decLat:getDeg(),
longitude = self.decLong:getDeg(),
}
}
end
 
root = htmlBuilder.create()
root
.tag("span")
.addClass("plainlinks nourlexpansion")
.wikitext("[" .. url)
.tag("span")
.addClass(defaultFormat == "dec" and "geo-nondefault" or "geo-default")
.tag("span")
.addClass("geo-dms")
.attr("title", "Mappe, foto aeree e altri dati per questa posizione")
.tag("span")
.addClass("latitude")
.wikitext(tostring(dmsLat))
.done()
.wikitext(" ")
.tag("span")
.addClass("longitude")
.wikitext(tostring(dmsLong))
.done()
.done()
.done()
.tag("span")
.addClass("geo-multi-punct")
.wikitext("&#xfeff; / &#xfeff;")
.done()
.tag("span")
.addClass(defaultFormat == "dec" and "geo-default" or "geo-nondefault")
.wikitext(args["name"] and "<span class=\"vcard\">" or "")
.tag("span")
.addClass("geo-dec")
.attr("title", "Mappe, foto aeree e altri dati per questa posizione")
.wikitext(decLat .. " " .. decLong)
.done()
.tag("span")
.attr("style", "display:none")
.tag("span")
.addClass("geo")
.wikitext(decLat:getDeg() .. "; " .. decLong:getDeg())
.done()
.done()
.wikitext(args["name"] and ("<span style=\"display:none\"> (<span class=\"fn org\">" ..
args["name"] .. "</span>)</span></span>") or "")
.done()
.wikitext("]")
.done()
 
-- Restituisce l'HTML di un link a geohack con le coordinate (usato solo quando
-- formatta il risultato a seconda di args["display"] (nil, "inline", "title", "inline,title")
-- non è possibile usare maplink).
text = tostring(root) .. (args["notes"] or "")
--
displayInline = not args["display"] or args["display"] == "inline" or args["display"] == "inline,title"
-- @return {string}
displayTitle = args["display"] == "title" or args["display"] == "inline,title"
function Coord:_buildGeohackLink()
htmlTitle = "<span style=\"font-size: small;\"><span id=\"coordinates\">[[Coordinate geografiche|Coordinate]]: "
-- crea la stringa per il parametro params di geohack.php
local geohackParams
 
if self.reqFormat == 'dec' then
return (displayInline and text or "") ..
geohackParams = string.format('%s_N_%s_E', self.args[1], self.args[2])
(displayTitle and (htmlTitle .. text .. "</span></span>") or "")
if self.args[3] then
geohackParams = geohackParams .. '_' .. self.args[3]
end
else
-- concatena solo i posizionali
geohackParams = table.concat(self.args, '_')
end
-- geohack url e parametri
local url = string.format('%s&pagename=%s&params=%s', cfg.geohackUrl,
mw.uri.encode(mw.title.getCurrentTitle().prefixedText, 'WIKI'), geohackParams)
if self.args.name then
url = url .. '&title=' .. mw.uri.encode(self.args.name)
end
local linkNode = mw.html.create('span')
:addClass('plainlinks nourlexpansion')
:wikitext('[' .. url .. ' ' .. tostring(self.dmsLat) .. ' ' .. tostring(self.dmsLong) .. ']')
:done()
return tostring(linkNode)
end
 
-- Restituisce l'HTML per il microformat Geo.
-- Dato un input fino a 9 parametri posizionali e 4 con nome, ritorna un html
-- FIXME Serve ancora nel 2024? La documentazione in proposito è scarsissima, e tutti
-- contenente le coordinate in formato dec e dms come collegamento esterno a geohack.php.
-- i link a strumenti che lo usano non vanno più. Inoltre, qui usiamo un elemento nascosto;
local function coord()
-- va bene lo stesso? Come verificarlo? Le specifiche non ne parlano.
local param, defaultFormat, dmsFormat, reqFormat
--
-- @return {string}
function Coord:_buildGeoMarkup()
return mw.html.create('')
:wikitext(self.args.name and '<span class="vcard">' or '')
:tag('span')
:attr('style', 'display:none')
:addClass('geo')
:tag('span')
:addClass('latitude')
:wikitext(tostring(self.dmsLat))
:done()
:wikitext( ', ' )
:tag('span')
:addClass('longitude')
:wikitext(tostring(self.dmsLong))
:done()
:wikitext(self.args.name and ('<span style="display:none"> (<span class="fn org">' ..
self.args.name .. '</span>)</span></span>') or '')
:done()
end
 
-- Restituisce l'HTML contenente le coordinate con link che utilizza <maplink> (Kartographer)
-- se possibile, e geohack altrimenti.
--
-- @return {string}
function Coord:getHTML()
-- con args.from restitusce una stringa vuota se non c'è nessun dato
if self.args.from and #self.args < 2 and not tonumber(self.args[1]) then
return ''
elseif self.args.display == 'debug' then
return self:getDebugCoords()
end
 
local display = getDisplay(self.args)
 
local coordLink
if self.isEarth then
-- Usa maplink se possibile. A maggio 2024 supporta solo coordinate terrestri,
-- vedi T151138.
coordLink = self:_buildMaplinkHTML()
else
coordLink = self:_buildGeohackLink()
end
 
local geo = self:_buildGeoMarkup();
 
local html = coordLink .. tostring(geo) .. (self.args.notes or '')
reqFormat = paramsParse()
if not reqFormat then
return getErrors()
end
if reqFormat == "dec" then
-- {{coord|43.6500|-79.3800}}
decLat = DecCoord:new(args[1], "N")
decLong = DecCoord:new(args[2], "E")
elseif reqFormat == "d" then
-- {{coord|43.651234|N|79.383333|W}}
decLat = DecCoord:new(args[1], args[2])
decLong = DecCoord:new(args[3], args[4])
elseif reqFormat == "dm" then
-- {{coord|43|29|N|79|23|W}}
dmsLat = DmsCoord:new(args[1], args[2], nil, args[3])
dmsLong = DmsCoord:new(args[4], args[5], nil, args[6])
elseif reqFormat == "dms" then
-- {{coord|43|29|4|N|79|23|0|W}}
dmsLat = DmsCoord:new(args[1], args[2], args[3], args[4])
dmsLong = DmsCoord:new(args[5], args[6], args[7], args[8])
end
-- conversioni dec <=> dms
if reqFormat == "dec" or reqFormat == "d" then
dmsFormat = getDmsFormat(decLat.deg, decLong.deg)
dmsLat = decLat:toDms(dmsFormat)
dmsLong = decLong:toDms(dmsFormat)
elseif reqFormat == "dm" or reqFormat == "dms" then
decLat = dmsLat:toDec()
decLong = dmsLong:toDec()
end
 
local frame = mw.getCurrentFrame()
if args["format"] then
defaultFormat = args["format"]
local ret = frame:extensionTag('templatestyles', '', {src = 'Modulo:Coord/styles.css'}) ..
elseif reqFormat == "dec" then
(display.inline and html or '');
defaultFormat = "dec"
elseif reqFormat == "d" then
defaultFormat = (dmsFormat == "d" and "dms" or "dec")
else
defaultFormat = "dms"
end
 
if display.title then
-- crea la stringa param per geohack.php
-- formatta il risultato a seconda di args.display (nil, 'inline', 'title', 'inline,title')
if reqFormat == "dec" then
-- se inline e title, in stampa visualizza solo il primo
param = args[1] .. "_N_" .. args[2] .. "_E" .. (args[3] and ("_" .. args[3]) or "")
local htmlTitle = string.format(
else
'<div style="font-size: small"><span %s id="coordinates">[[Coordinate geografiche|Coordinate]]: %s</span></div>',
-- concatena solo i posizionali
display.inline and 'class="noprint"' or '',
param = table.concat(args, "_")
html
end
)
ret = ret .. frame:extensionTag( 'indicator', htmlTitle, { name = 'coordinates' } )
end
 
return ret ..
return args["display"] == "debug" and
self:_setGeoData(display) ..
(decLat .. " " .. decLong .. " " .. dmsLat .. " " .. dmsLong) or
(mw.title.getCurrentTitle().namespace == 0 and self.wdCat or '')
buildHTML(decLat, decLong, dmsLat, dmsLong, param, defaultFormat)
end
 
-- =============================================================================
-- Entry-point per eventuale {{dms2dec}}
-- Funzioni esportate
function p.dms2dec(frame)
-- =============================================================================
local args = frame.args
 
local p = {}
-- {{dms2dec|N|10|20|35}}
 
return DmsCoord:new(args[2], args[3], args[4], args[1]):toDec():getDeg()
-- Funzione importata da https://en.wikipedia.org/w/index.php?title=Module:Coordinates&oldid=789126031
-- per estrarre lat, long, type, scale, dim, region, globe, source, dal link a geohack.php generato da coord.
function p.coord2text(frame)
if frame.args[1] == '' or frame.args[2] == '' or not frame.args[2] then return nil end
frame.args[2] = mw.text.trim(frame.args[2])
if frame.args[2] == 'lat' or frame.args[2] == 'long' then
local result, negative = mw.text.split((mw.ustring.match(frame.args[1],'[%.%d]+°[NS] [%.%d]+°[EW]') or ''), ' ')
if frame.args[2] == '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(frame.args[1], 'params=.-_'..frame.args[2]..':(.-)[ _]')
end
end
 
-- Entry-pointFunzione per eventuale template {{dec2dmsdms2dec}}.
function p.dms2dec(frame)
local args = frame.args
-- {{dms2dec|N|2|3|4}}
return DmsCoord:new(args[2], args[3], args[4], args[1]):toDec():getDeg()
end
 
-- Funzione per eventuale template {{dec2dms}}.
function p.dec2dms(frame)
local args = frame.args
-- {{dec2dms|1.111|N|S}}
return DecCoord:new(args[1], tonumber(args[1]) >= 0 and args[2] or args[3]):toDms()
-- {{dec2dms|10.391944|N|S|d}}
return DecCoord:new(args[1], tonumber(args[1]) >= 0 and args[2] or args[3]):toDms(args[4])
end
 
-- Funzione per l'utilizzo da un altro modulo.
-- Entry-point per {{coord}}
function p.coord_main(frameargs)
return Coord:new(args):getHTML()
for k, v in pairs(frame:getParent().args) do
if v ~= "" then
args[k] = v
end
end
return coord()
end
 
-- Funzione per il template {{Coord}}.
function p.main(frame)
return select(2, xpcall(function()
return p._main(getArgs(frame))
end, errhandler))
end
 
return p