Vés al contingut

Mòdul:Mapa cos celeste

De la Viquipèdia, l'enciclopèdia lliure
Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul Mapa cos celeste (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


local getArgs = require('Module:Arguments').getArgs
local mWikidata = require('Module:Wikidades')
local cfg = mw.loadData('Module:Mapa cos celeste/dades')
local errorCategory = '[[Categoria:Articles amb errors del mòdul Mapa cos celeste]]'
local p = {}

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 getX(map, long, marksize)
	local dim = 250
	return math.floor(dim * (math.fmod(long + 360 + map.spost_mer0, 360) / 360) - marksize / 2)
end

local function getY(map, lat, marksize)
	local dim = 250
	local ret = 0
	if map.projection == 'equirectangular' then
		-- projecció cilíndrica equidistant o equirectangular
		ret = (dim * map.height / map.width) / 2 *
			  (-lat / map.max_lat) +
			  (dim * map.height / map.width) / 2 - marksize / 2
	elseif map.projection == 'Mercator' then
		-- pProjecció cilíndrica de Mercator
		ret = (dim * map.height / map.width) / 2 *
			  (-math.log(math.tan(math.pi / 4 + lat * math.pi / 360))) /
			  math.log(math.tan(math.pi / 4 + map.max_lat * math.pi / 360)) +
			  (dim * map.height / map.width) / 2 - marksize / 2
	elseif map.projection == 'Lambert' then
		-- projecció cilíndrica equivalent de Lambert
		ret = (dim * map.height / map.width) / 2 *
			  (-math.sin(lat * math.pi / 180) / math.sin(map.max_lat * math.pi / 180)) +
			  (dim * map.height / map.width) / 2 - marksize / 2
	end
	return math.floor(ret)
end

-- Afegeix la preposició davant del nom del cos celeste
local function prep(cos)
	if cos == 'lluna' then
		return 'de la Lluna'
	elseif mw.ustring.find(cos, "^[aàeèéiíoòóuú]") then
		return 'd’' .. mw.language.getContentLanguage():ucfirst(cos)
	end
	return 'de ' .. mw.language.getContentLanguage():ucfirst(cos)
end

-- Per utilitzar des d'altres mòduls
function p._main(args)
	-- paràmetres requerits: mapa, lat, long
	if args.mapa or args[1] then
		args.mapa = mw.ustring.lower(args.mapa or args[1])
	else
		error('mapa no especificat', 2)
	end
	args.mapa = cfg.aliases[args.mapa] or args.mapa -- àlies eventual
	if not cfg.maps[args.mapa] then
		error('mapa no disponible: ' .. args.mapa, 2)
	end
	
	-- recuperació de coordenades de Wikidata
	args.lat = args.lat or mWikidata.claim({property = 'P625', formatting = 'latitude', editicon='false'}) or mWikidata.claim({property = 'P8981', formatting = 'latitude', editicon='false'})
	args.long = args.long or mWikidata.claim({property = 'P625', formatting = 'longitude', editicon='false'}) or mWikidata.claim({property = 'P8981', formatting = 'longitude', editicon='false'})
	if not args.lat then
		error('latitud no especificada', 2)
	elseif not tonumber(args.lat) then
		error('la latitud no és numèrica', 2)
	elseif not args.long then
		error('longitud no especificada', 2)
	elseif not tonumber(args.long) then
		error('la longitud no és numèrica', 2)
	end
	
	local map = cfg.maps[args.mapa]
	local caption = string.format('Mapa topogràfic %s. Projecció %s. Àrea representada: %s.',
						prep(args.mapa),
						(map.projection == 'equirectangular' and '' or 'de ') .. map.projection,
						map.range)
	
	return mw.getCurrentFrame():expandTemplate {
		title = 'Sobreposat',
		args = {
			base = map.image,
			base_width = '250px',
			base_caption = caption,
			float = args.mark or 'DeepPink pog.svg',
			float_width = (args.marksize or 15) .. 'px',
			float_caption = args.nom or '',
			x = getX(map, args.long, args.marksize or 15),
			y = getY(map, args.lat, args.marksize or 15)
		}
	}
end

-- Punt d'entrada des de plantilla
function p.main(frame)
	return select(2, xpcall(function()
		return p._main(getArgs(frame, {parentOnly = true}))
	end, errhandler))
end

return p