Modulo:Mapframe

Versione del 21 mag 2018 alle 06:55 di M.casanova (discussione | contributi) (Nuova pagina: require('Module:No globals') local getArgs = require('Module:Arguments').getArgs local p = {} local function agg(t,...) local args = {...} for _, s in ipairs(...)
(diff) ← Versione meno recente | Versione attuale (diff) | Versione più recente → (diff)
Info Istruzioni per l'uso
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 p = {}

local function agg(t,...)
    local args = {...}
    for _, s in ipairs(args) do
        table.insert(t, s)
    end
end

function p.mappa(frame)
	local t = getArgs(frame)

	local err = '-'

	local pp = {}
	local geo = t['dati'] or -1
	local colore  = t['colore'] or '#b80000'
	local gruppo = t['gruppo'] or ''
	local autozoom = t['autozoom'] or 's'
	local cornice
	if (t['cornice']) then cornice = tonumber(t['cornice']) or 1 else cornice = 1 end
	local lat1, lat2, lon1, lon2 = 400, -400, 400, -400
	local num = -1

	if (geo == -1) then
		num = 1
		while (t['nome'..num]) do
			pp[num] = {
				nome = t['nome'..num], -- nome del punto
				lat  = tonumber(t['lat'..num]) or -400, --latitudine
				lon  = tonumber(t['lon'..num]) or -400, --longitudine
				col  = t['colore'..num] or colore, -- colore del punto
				simb = (t['simbolo'..num] or '-number'), --simbolo da mostrare / predefinito numero
			}
			if ((pp[num].simb == '-number') and (gruppo ~= '')) then pp[num].simb = "-number-"..gruppo end
			if (pp[num].lat > 85 or pp[num].lat<-85) then
				err = string.format('latitudine non valida per il punto %d', num)
			elseif (pp[num].lon > 180 or pp[num].lon<-180) then
				err = string.format('longitudine non valida per il punto %d', num)
			else
				lat1 = math.min(lat1, pp[num].lat)
				lat2 = math.max(lat2, pp[num].lat)
				lon1 = math.min(lon1, pp[num].lon)
				lon2 = math.max(lon2, pp[num].lon)
			end
			num = num + 1
		end
	end

	if (err == '-') then
		local m_args = {}
		m_args['width'] = (t['larghezza'] or 350)
		m_args['height'] = (t['altezza'] or 300)
		m_args['align'] = (t['allinea'] or "right")
		
		if (cornice ~= 1) then m_args['frameless'] = 1 end

		if (t['zoom']) then
			m_args['zoom'] = tonumber(t['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 (t['didascalia'])) then m_args['text'] = t['didascalia'] end

		if (t['centro_lon']) then
			m_args['longitude'] = tonumber(t['centro_lon'])
		elseif (geo == -1) then
			m_args['longitude'] = (lon1+lon2)/2
		end

		if (t['centro_lat']) then
			m_args['latitude'] = tonumber(t['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,#pp do
				m_dati["features"][i] = {
					type = "Feature",
					properties = {},
					geometry = {
						type = "Point",
						coordinates = { pp[i].lon, pp[i].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
	else
		return err
	end
end

return p