Modulo:Mapframe

Questo è un modulo scritto in Lua. Le istruzioni che seguono sono contenute nella sottopagina Modulo:Mapframe/man (modifica · cronologia)
Sandbox: Modulo:Mapframe/sandbox (modifica · cronologia) · Sottopagine: lista · Test: Modulo:Mapframe/test (modifica · cronologia · esegui)
Modulo che implementa il template {{Mappa OSM}}.
require('Module:No globals')
local getArgs = require('Module:Arguments').getArgs
local errorCategory = '[[Categoria:Voci con errori del modulo Mapframe]]'
local p = {}
-- Error handler per xpcall, formatta l'errore.
local function errhandler(msg)
local cat = mw.title.getCurrentTitle().namespace == 0 and errorCategory or ''
return string.format('<span class="error">%s</span>%s', msg, cat)
end
local function agg(t, ...)
local args = {...}
for _, s in ipairs(args) do
table.insert(t, s)
end
end
function p._mappa(args)
local points = {}
local geo = args.dati or -1
local colore = args.colore or '#b80000' --colore
local simbolo = args.simbolo or '-number' --simbolo
local gruppo = args.gruppo or ''
local autozoom = args.autozoom or 's'
local cornice
if args.cornice then cornice = tonumber(args.cornice) or 1 else cornice = 1 end
local lat1, lat2, lon1, lon2 = 400, -400, 400, -400
if geo == -1 then
local num = 1
while args['nome' .. num] do
points[num] = {
nome = args['nome' .. num], -- nome 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
}
if (points[num].simb == '-number' and gruppo ~= '') then points[num].simb = '-number-' .. gruppo end
if (points[num].lat > 85 or points[num].lat < -85) then
error(string.format('latitudine non valida per il punto %d', num), 2)
elseif (points[num].lon > 180 or points[num].lon<-180) then
error(string.format('longitudine non valida per il punto %d', num), 2)
else
lat1 = math.min(lat1, points[num].lat)
lat2 = math.max(lat2, points[num].lat)
lon1 = math.min(lon1, points[num].lon)
lon2 = math.max(lon2, points[num].lon)
end
num = num + 1
end
end
local m_args = {}
m_args.width = args.larghezza or 350
m_args.height = args.altezza or 300
m_args.align = args.allinea or 'right'
if cornice ~= 1 then m_args.frameless = 1 end
if args.zoom then
m_args.zoom = tonumber(args.zoom)
elseif (geo == -1 and autozoom == 's') then
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
if cornice == 1 and args.didascalia then m_args.text = args.didascalia end
if args.centro_lon then
m_args.longitude = tonumber(args.centro_lon)
elseif geo == -1 then
m_args.longitude = (lon1 + lon2) / 2
end
if args.centro_lat then
m_args.latitude = tonumber(args.centro_lat)
elseif (geo == -1) then
local l1 = 1 - math.log(math.tan(math.pi * (1 + lat1 / 90) / 4)) / math.pi
local l2 = 1 - math.log(math.tan(math.pi * (1 + lat2 / 90) / 4)) / math.pi
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
local m_dati
if geo == -1 then
m_dati = { type = 'FeatureCollection', features = {} }
for i = 1, #points do
m_dati.features[i] = {
type = 'Feature',
properties = {},
geometry = {
type = 'Point',
coordinates = { points[i].lon, points[i].lat }
}
}
m_dati.features[i].properties['marker-symbol'] = points[i].simb
m_dati.features[i].properties['marker-color'] = points[i].col
m_dati.features[i].properties.title = points[i].nome
end
m_dati = mw.text.jsonEncode(m_dati)
else
m_dati = frame:preprocess(geo)
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)
end
end
-- Funzione per il template {{Mapframe}}.
function p.mappa(frame)
return select(2, xpcall(function()
return p._mappa(getArgs(frame, { parentOnly = true }))
end, errhandler))
end
return p