Module:Carte

De Lagny-sur-Marne Wiki
Révision datée du 16 décembre 2014 à 20:48 par Zolo (discussion)
Aller à : navigation, rechercher

La documentation pour ce module peut être créée à Module:Carte/doc

local p = {}
local mapdatamod = require('Module:Données cartes')
local pointmod = require('Module:Carte/Points')
local linguistic = require('Module:Linguistique')

local function buildmap(map, maptype, width, latitude, longitude, pointtype, caption) -- fonction d'aige pour buildmap
	if map == '-' then
		return
	end
	local mapdata =  mapdatamod.main(map)
	if not mapdata or not mapdata.images then
		addmaintenancecat('Page avec des données de géolocalisation non supportées')
		return "carte non supportée: " .. map
	end
	local name = mapdata.name or '?'
	
	if (not pointtype) then
		pointtype = default
	end
	pointimage = pointmod[pointtype]
	if not pointimage then pointimage = pointmod.default end -- + message d'erreur ?
	
	-- analyse linguistique pour le texte de la carte
	local datagender = mapdata.genre or ''
	local gender = string.sub(datagender, 1, 1) -- les données cartes sont du format ms pour masucline singulier
	local number = string.sub(datagender, 2, 2)
	local determiner = mapdata.determiner
	local ofstring = linguistic.of(name, gender, number, determiner) -- restitue "de France" ou "du Japon"

	local pointsize = tostring(8)
	local pointheight, pointwidth
	if mapdata.projection ==  'Projection équirectangulaire' then
		pointheight = 100 *  (latitude - mapdata.top) / (mapdata.bottom - mapdata.top) 
		pointwidth = 100 *  (longitude - mapdata.left) / (mapdata.right - mapdata.left) 
	else
		return "système cartographique non supporté"
	end
	if (pointheight > 100) or (pointwidth) > 100 or (pointheight < 0) or (pointwidth < 0) then 
		return '[[Category:Article avec une géolocalisation hors-carte]]'
	end
	local htmlheight = tostring(pointheight) .. '%'
	local htmlwidth = tostring(pointwidth) .. '%'

	local pointdiv = mw.html.create('div')
		:css{position = 'absolute', border = 'none', top = htmlheight, left = htmlwidth}
		:tag('div')
			:css{position = 'absolute', top = '-4px', left = '-4px', ['line-height'] = '0'}
			:wikitext('[[Image:' .. pointimage .. '|8px]]')
		:done()
	local mapname = mapdata.name
	local file
	if type(mapdata.images) == 'string' then
		file = mapdata.images
	else 
		file = mapdata.images[maptype] or mapdata.images[1] or mapdata.images['default']
	end
	local alt = 'voir sur la carte ' .. ofstring
	local map = mw.html.create('div')
		:css{['text-align'] = 'center'}
		:addClass("geobox")
		:wikitext('<small>Géolocalisation sur la carte ' .. ofstring .. '</small>')
		:tag('table')
			:addClass('DebutCarte')
			:attr({border="0", cellspacing="0", cellpadding="0"})
			:css({margin = '0', border = 'none', padding = '0'})
				:tag('tr')
					:tag('td')
						:tag('div')
							:css({position= 'relative', margin = "auto",})
							:wikitext('[[Fichier:' .. file .. '|' .. width .. 'px' .. '|' .. alt .. ']]' )
							:node(pointdiv)
							:done()
						:done()
					:done()
				:done()
	return map
end

function p.multimap(params)
	local maplist = params.maplist
	local width = params.width
	local latitude = params.latitude
	local longitude = params.longitude
	
	-- les cartes s'affichent en ordre inversé, remettre bien
	local newlist = {} 
	for i, j in pairs(maplist) do
		table.insert(newlist, 1, j)
	end
	maplist = newlist

	-- traitement de la largeur
	if width and tonumber(width) then
		width = tonumber(width)
	else
		width = 280
	end-- si pas un nombre, erreur ?
	local div =  mw.html.create('div'):addClass('img_toogle')
	
	if not pointimage then
		pointimage = 'Point carte.svg'
	end
	
	--transition: appel aux [[Modèle:Géolocalisation/]] n en l'absence de données dans le module
 	local mapliststring = table.concat(maplist, '/')
	for i, j in pairs(maplist) do
		if not require('Module:Données cartes').main(j) then 
			return
			mw.getCurrentFrame():expandTemplate{ title = 'Infobox/Géolocalisation multiple/transition', args = {['géolocalisation'] = mapliststring, type = maptype, latitude = latitude, longitude = longitude }}
	.. '[[Catégorie:Page avec des données de géolocalisation non supportées]]'
		end
	end
	for i, map in pairs(maplist) do 
		local newmap = buildmap( map, maptype, width, latitude, longitude, pointimage, caption)
		div:node(newmap)
	end
	return div
end

function p.map(frame)
	local args = frame.args
	-- utilisation du franaçs
	args.maplist =  mw.text.split( args.carte, '/', true)
	return p.multimap(args)
end
return p