Modulo:Mapframe: differenze tra le versioni

Contenuto cancellato Contenuto aggiunto
Nuova pagina: require('Module:No globals') local getArgs = require('Module:Arguments').getArgs local p = {} local function agg(t,...) local args = {...} for _, s in ipairs(...
 
m modulo no globals obsoleto
 
(20 versioni intermedie di 2 utenti non mostrate)
Riga 1:
require('Module:No globalsstrict')
 
local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Module:Wikidata')
local errorCategory = '[[Categoria:Voci con errori del modulo Mapframe]]'
 
local p = {}
 
-- Error handler per xpcall, formatta l'errore.
local function agg(t,...)
local args =function {...}errhandler(msg)
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
for _, s in ipairs(args) do
return string.format('<span class="error">%s</span>%s', msg, cat)
table.insert(t, s)
end
end
 
local function p.mappagetLegenda(framepoints, ncols)
local tdivStyle = getArgs(frame){
['margin-top'] = '5px',
['column-count'] = ncols,
['-moz-column-count'] = ncols,
['-webkit-column-count'] = ncols
}
local divNode = mw.html.create('div'):css(divStyle)
local currNum = 1
local currChar = 'A'
 
for _, point in ipairs(points) do
local err = '-'
local pNode = mw.html.create('p')
:css('font-size', '90%')
:css('margin-top', '0')
local codeNode = mw.html.create('code')
:css('font-weight', 'bold')
:css('color', 'white')
:css('border-radius', '6px')
:css('border', '2px solid ' .. point.col)
:css('background', point.col)
if not point.simb then
codeNode:wikitext('&nbsp;')
elseif point.simb:find('-number') == 1 then
codeNode:wikitext(currNum)
currNum = currNum + 1
elseif point.simb:find('-letter') == 1 then
codeNode:wikitext(currChar)
currChar = ('ABCDEFGHIJKLMNOPQRSTUVWXYZA'):match(currChar .. '(.)')
elseif point.simb == '' then
else
codeNode
:css('padding-left', '0')
:css('padding-right', '0')
:css('background', 'white')
codeNode:wikitext(string.format('[[File:Maki2-%s-18.svg|18px]]', point.simb))
end
pNode:node(codeNode):wikitext('&nbsp;' .. point.nome)
divNode:node(pNode)
end
 
return tostring(divNode)
end
 
function p._mappa(args)
local pp = {}
local geopoints = t['dati'] or -1{}
local colore sinottico = t['colore']tonumber(args.sinottico) or '#b80000'0
local gruppocolore = t['gruppo']args.colore or '#b80000'
local autozoomsimbolo = t['autozoom']args.simbolo or 's-number'
local cornicegruppo = args.gruppo or ''
local autozoom = args.autozoom or 's'
if (t['cornice']) then cornice = tonumber(t['cornice']) or 1 else cornice = 1 end
local elenco = (args.elenco == '1' or args.elenco == '2') and args.elenco or nil
local dsimbolo = 'medium'
local cornice = tonumber(args.cornice) or 1
local lat1, lat2, lon1, lon2 = 400, -400, 400, -400
local numm_args = -1{
width = args.larghezza or (sinottico == 1 and 260 or 350),
height = args.altezza or (sinottico == 1 and 260 or 300),
align = args.allinea or (sinottico == 1 and 'center' or 'right'),
-- frameless con cornice = 0 o con sinottico = 1
frameless = (cornice == 0 or sinottico == 1) and 1 or nil
}
 
-- =================================
if (geo == -1) then
-- popola la sequence points
num = 1
-- =================================
while (t['nome'..num]) do
if not args.dati then
pp[num] = {
local num = 1
nome = t['nome'..num], -- nome del punto
if sinottico == 1 and args.lat1 == nil and args.lon1 == nil then
lat = tonumber(t['lat'..num]) or -400, --latitudine
args.lat1 = mWikidata._getProperty({ 'P625', coord = 'latitude', n = 1 })
lon = tonumber(t['lon'..num]) or -400, --longitudine
args.lon1 = mWikidata._getProperty({ 'P625', coord = 'longitude', n = 1 })
col = t['colore'..num] or colore, -- colore del punto
end
simb = (t['simbolo'..num] or '-number'), --simbolo da mostrare / predefinito numero
while args['nome' .. num] do
points[num] = {
nome = args['nome' .. num], -- nome del punto
desc = args['desc' .. num], -- descrizione del punto
lat = tonumber(args['lat' .. num]) or -400, --latitudine
lon = tonumber(args['lon' .. num]) or -400, --longitudine
col = args['colore' .. num] or colore, -- colore del punto
simb = args['simbolo' .. num] or simbolo, --simbolo da mostrare
dsimb = args['dim_simbolo' .. num] or dsimbolo, --dimensione del simbolo da mostrare
}
if ((pppoints[num].simb == '-number') and (gruppo ~= '')) then pp
points[num].simb = "'-number-"' ..gruppo endgruppo
end
if (pp[num].lat > 85 or pp[num].lat<-85) then
if points[num].lat > 85 or points[num].lat < -85 then
err = string.format('latitudine non valida per il punto %d', num)
error(string.format('latitudine non valida per il punto %d', num), 2)
elseif (pp[num].lon > 180 or pp[num].lon<-180) then
elseif points[num].lon > 180 or points[num].lon < -180 then
err = string.format('longitudine non valida per il punto %d', num)
error(string.format('longitudine non valida per il punto %d', num), 2)
else
lat1 = math.min(lat1, pppoints[num].lat)
lat2 = math.max(lat2, pppoints[num].lat)
lon1 = math.min(lon1, pppoints[num].lon)
lon2 = math.max(lon2, pppoints[num].lon)
end
num = num + 1
end
-- non utilizza nessun simbolo con un solo punto e args.simbolo e args.simbolo1 non definiti
if #points == 1 and args.simbolo == nil and args.simbolo1 == nil then
points[1].simb = nil
end
end
 
-- =================================
if (err == '-') then
-- popola la table m_args
local m_args = {}
-- =================================
m_args['width'] = (t['larghezza'] or 350)
-- valore ingrandimento
m_args['height'] = (t['altezza'] or 300)
if args.zoom then
m_args['align'] = (t['allinea'] or "right")
m_args.zoom = tonumber(args.zoom)
elseif not args.dati then
if (cornice ~= 1) then m_args['frameless'] = 1 end
if #points == 1 then
 
m_args.zoom = 10 -- valore predefinito per singolo punto
if (t['zoom']) then
if sinottico == 1 then
m_args['zoom'] = tonumber(t['zoom'])
m_args.zoom = 13 -- valore predefinito per sinottico con singolo punto
elseif (geo == -1 and autozoom == 's') then
end
local dx = 1.1*(lon2-lon1)/360
elseif autozoom == 's' then -- calcola zoom automaticamente
local dy = 1.1*(math.log(math.tan( math.pi*(1 + lat2/90)/4)) - math.log(math.tan( math.pi*(1 + lat1/90)/4)))/(2*math.pi)
local dx = 1.1 * (lon2 - lon1) / 360
local dy = 1.1 * (math.log(math.tan(math.pi * (1 + lat2 / 90) / 4)) - math.log(math.tan( math.pi * (1 + lat1 / 90) / 4))) / (2 * math.pi)
 
local scalax, scalay
if (dx == 0) then scalax = 18 else scalax = math.floor(-math.log(dx) / math.log(2)) end
if (dy == 0) then scalay = 18 else scalay = math.floor(-math.log(dy) / math.log(2)) end
if ((dx == 0) and (dy == 0)) then
m_args['.zoom'] = 10 -- valore default per singolo punto
else
m_args['.zoom'] = math.max(0, math.min(18, scalax, scalay))
end
end
end
-- esclude didascalia per cornice = 0 e per sinottico = 1
if cornice == 1 and sinottico ~= 1 then
m_args.text = (args.didascalia or '') .. (elenco and getLegenda(points, elenco) or '')
end
 
if args.centro_lon then
if ((cornice == 1) and (t['didascalia'])) then m_args['text'] = t['didascalia'] end
m_args.longitude = tonumber(args.centro_lon)
 
elseif not args.dati then
if (t['centro_lon']) then
if #points == 1 then
m_args['longitude'] = tonumber(t['centro_lon'])
m_args.longitude = points[1].lon
elseif (geo == -1) then
else
m_args['longitude'] = (lon1+lon2)/2
m_args.longitude = (lon1 + lon2) / 2
end
end
 
if (t['args.centro_lat']) then
m_args['.latitude'] = tonumber(t['args.centro_lat'])
elseif (geonot == -1)args.dati then
if #points == 1 then
local l1 = 1-math.log(math.tan( math.pi*(1 + lat1/90)/4))/math.pi
m_args.latitude = points[1].lat
local l2 = 1-math.log(math.tan( math.pi*(1 + lat2/90)/4))/math.pi
else
local centroy = (l1+l2)/2
centroylocal l1 = (1 - math.atanlog(math.exptan(math.pi * (1-centroy) + lat1 / 90))-math.pi / 4)*360) / math.pi
local l2 = 1 - math.log(math.tan(math.pi * (1 + lat2 / 90) / 4)) / math.pi
m_args['latitude'] = centroy
local centroy = (l1 + l2) / 2
centroy = (math.atan(math.exp(math.pi * (1 - centroy))) - math.pi / 4) * 360 / math.pi
m_args.latitude = centroy
end
end
 
-- =================================
local m_dati
-- popola la table m_dati
if (geo == -1) then
-- =================================
m_dati = { type = "FeatureCollection", features = {} }
local m_dati
for i=1,#pp do
if args.dati then
m_dati["features"][i] = {
typem_dati = "Feature",args.dati
else
properties = {},
m_dati = { type = 'FeatureCollection', features = {} }
geometry = {
for i, point in ipairs(points) do
type = "Point",
m_dati.features[i] = {
coordinates = { pp[i].lon, pp[i].lat }
type = 'Feature',
}
properties = {
['marker-symbol'] = point.simb,
['marker-color'] = point.col,
['marker-size'] = point.dsimb,
title = point.nome,
description = point.desc
},
geometry = {
type = 'Point',
coordinates = { point.lon, point.lat }
}
}
m_dati["features"][i]['properties']['marker-symbol'] = pp[i].simb
m_dati["features"][i]['properties']['marker-color'] = pp[i].col
m_dati["features"][i]['properties']['title'] = pp[i].nome
end
m_dati = mw.text.jsonEncode(m_dati)
else
m_dati = frame:preprocess(geo)
end
if (t['debug']) then
local mdebug = mw.text.jsonEncode(m_args)
mdebug = '<pre>' .. mdebug .. '\n\n' .. m_dati .. '</pre>'
return mdebug
else
return mw.getCurrentFrame():extensionTag ("mapframe", m_dati, m_args )
end
m_dati = mw.text.jsonEncode(m_dati)
end
 
if args.debug then
local mdebug = mw.text.jsonEncode(m_args)
return string.format('<pre>%s\n\n%s</pre>', mdebug, m_dati)
else
return mw.getCurrentFrame():extensionTag('mapframe', m_dati, m_args)
return err
end
end
 
-- Funzione per il template {{Mappa OSM}}.
function p.mappa(frame)
return select(2, xpcall(function()
return p._mappa(getArgs(frame, { parentOnly = true }))
end, errhandler))
end