Module:Composition Division de France
local p = {}
local PopulationFrance = require 'Module:Population de France/Données' local Unite = require 'Module:Unité'
local lang = mw.getContentLanguage() local charte = 'default' local configData = { default = { titreTableau = ' ', selectCapitale = '(siège)', colonnes = { 'intercommunalité' }, couleurEntete = '#f2f2f2', }, ['aire urbaine'] = { titreTableau = 'de l’aire urbaine', selectCapitale = '(ville-centre)', colonnes = { 'statut' }, couleurEntete = '#ece5ca', }, ['arrondissement'] = { titreTableau = 'de l’arrondissement', selectCapitale = '(chef-lieu)', couleurEntete = '#e1e1e1', }, ['canton'] = { titreTableau = 'du canton', selectCapitale = '(bureau centralisateur)', couleurEntete = '#ece5ca', }, ['département'] = { titreTableau = 'du département', selectCapitale = '(préfecture)', colonnes = { 'code postal', 'arrondissement', 'canton', 'intercommunalité' }, couleurEntete = '#f6f3dd', }, ['intercommunalité'] = { titreTableau = 'de l’intercommunalité', colonnes = { 'gentilé' }, couleurEntete = '#ffe2bf', }, ['localité'] = { titreTableau = 'de la localité' }, ['métropole'] = { titreTableau = 'de la métropole', colonnes = { 'gentilé' }, couleurEntete = '#f6f3dd', }, ['région'] = { titreTableau = 'de la région', selectCapitale = '(préfecture)', couleurEntete = '#bbdefd', }, ['unité urbaine'] = { titreTableau= 'de l’unité urbaine', selectCapitale = '(ville-centre)', colonne3 = { 'statut' }, couleurEntete = '#ece5ca', }, }
local function config( param ) return configData[ charte ][ param ] or configData.default[ param ] end
local function abbr( abrev, description ) local wiki = mw.html.create( 'abbr' ) :addClass( 'abbr') :attr{ title = description } :wikitext( abrev ) return wiki end
local function getData( commune ) local data, existe = PopulationFrance.charge_donnees( commune ) if existe == true or existe > 2 then data.page = commune data.population = data.dernier and data[ data.dernier ].pop if data.superficie and data.population then data.densite = data.population / data.superficie local prec = 1 - math.floor( math.log10( data.densite ) ) data.precisionDensite = math.min( 2, math.max( 0, prec ) ) end return data end end
function p.tableau( args )
local testCharte = mw.ustring.lower( args.charte or )
if configData[ testCharte ] then
charte = testCharte
end
-- récupération des données local dataCommunes = {} local i = 1 local fractions = require 'Module:Données/Fractions cantonales/évolution population' local chef = getData( args['chef-lieu'] or args['bureau'] ) if chef then chef.selectCapitale = config( 'selectCapitale' ) chef.canton = fractions.parCommune[ chef.page ] or chef.canton table.insert( dataCommunes, chef ) end for i = 1, 891 do local nomCommune = args[ 'commune ' .. i ] if nomCommune and nomCommune ~= then local commune = getData( nomCommune ) if commune then commune.canton = fractions.parCommune[ nomCommune ] or commune.canton table.insert( dataCommunes, commune ) else table.insert( dataCommunes, nomCommune ) end end end
-- construction du tableau local wiki = mw.html.create() -- entête wiki:newline() :wikitext( '{| class="wikitable sortable titre-en-couleur" style="text-align:center;"' ) :newline() :wikitext( '|+ ', args.titre or 'Liste des communes ' .. config( 'titreTableau' ) .. ( args.autre or ) ) :newline() :wikitext( '|- style="background:', config( 'couleurEntete' ), ';"' ) :newline() :wikitext( '! scope="col" | Nom' ) :newline() :wikitext( '! scope="col" | Code' ) :tag( 'br' ):done() :node( abbr( 'Insee', 'Institut national de la statistique et des études économiques' ) )
for _, colonne in ipairs( config( 'colonnes') ) do local nomColonne = colonne if type( colonne ) == 'table' then nomColonne = colonne.titre end wiki:newline() :wikitext( '! scope="col" | ', lang:ucfirst( nomColonne ) ) end
wiki:newline() :wikitext( '! scope="col" | Superficie' ) :tag( 'br' ):done() :tag( 'small' ) :node( abbr( '(km2)', 'kilomètres carrés' ) ) :done() :newline() :wikitext( '! scope="col" | Population' ) :tag( 'br' ):done() :tag( 'small' ) :addClass( 'nowrap' ) :wikitext( '(dernière ' ) :node( abbr( 'pop. légale', 'population légale' ) ) :wikitext( ')') :done() :newline() :wikitext( '! scope="col" | Densité' ) :tag( 'br' ):done() :tag( 'small' ) :addClass( 'nowrap' ) :node( abbr( '(hab./km2)', 'habitants par kilomètre carré' ) ) :done() :newline() :wikitext( '! scope="col" class="unsortable" | Modifier' )
-- Lignes local frame = mw.getCurrentFrame() for i, commune in ipairs( dataCommunes ) do local nomModele = 'Données/' .. commune.page .. '/informations générales' local utilisationModele = false
wiki:newline() :wikitext( '|-' ) :newline() :wikitext( '| style="text-align:left;" | ' ) if type( commune ) == 'table' then if commune.selectCapitale then wiki:wikitext( "", commune['nom-wp'], "" ) :tag( 'br' ):done() :tag( 'small' ) :wikitext( commune.selectCapitale ) :done() else wiki:wikitext( commune['nom-wp'] ) end
wiki:newline() :wikitext( '| ' ) :wikitext( commune.insee )
for _, colonne in ipairs( config( 'colonnes') ) do
local nomColonne = colonne
if type( colonne ) == 'table' then
nomColonne = colonne.source
end
local dataColonne, test = commune[ nomColonne ]
if not dataColonne then
test, dataColonne = pcall(
frame.expandTemplate,
frame,
{ title = nomModele, args = { nomColonne } }
)
if test then
utilisationModele = true
else
dataColonne =
end
elseif type( dataColonne ) == 'table' then
if type( dataColonne[1] ) == 'table' then
local tempDataColonne = {}
for _, v in ipairs( dataColonne ) do
table.insert( tempDataColonne, v['canton-wp'] )
end
dataColonne = tempDataColonne
end
dataColonne = table.concat( dataColonne, '
' )
end
wiki:newline()
:wikitext( '| ' )
:wikitext( dataColonne )
end
wiki:newline()
:wikitext( '| ' )
:wikitext( Unite._unite{ commune.superficie, ['décimales'] = 2 } )
:newline()
:wikitext( '| ' )
if commune.population then
local fraction
if charte == 'canton' and type( commune.canton ) == 'table' then
for _, c in ipairs( commune.canton ) do
if c.canton == mw.title.getCurrentTitle().text or c.canton == args.canton then
fraction = c
break
end
end
end
if fraction then
wiki:wikitext( 'Fraction : ', Unite._unite{ fraction.pop }, ' ' )
:tag( 'small' )
:wikitext( '(' ..fractions.dateDonnees .. ')' )
:done()
:tag( 'br' ):done()
:tag( 'small' )
:wikitext( 'Commune : ', Unite._unite{ commune.population }, ' ' )
:tag( 'small' )
:wikitext( '(' ..commune.dernier .. ')' )
:done()
:done()
else
wiki:wikitext( Unite._unite{ commune.population }, ' ' )
:tag( 'small' )
:wikitext( '(' ..commune.dernier .. ')' )
:done()
end
end
wiki:newline()
:wikitext( '| ' )
:wikitext( Unite._unite{ commune.densite, ['décimales'] = commune.precisionDensite } )
wiki:newline()
:wikitext( '| ' )
:wikitext( '' )
if utilisationModele then
wiki:wikitext( '
' )
end
else wiki:wikitext( commune ) :newline() :wikitext( '| ' ) :tag( 'span' ) :css{ color = 'red' } :wikitext( '????' ) :wikitext( ) :done() :newline() :wikitext( '| || || || ||' ) end end
-- fin du tableau
wiki:newline()
:wikitext( '|}' )
:wikitext( )
return tostring( wiki ) end
p["modèle"] = function( frame ) local args = frame.getParent and frame:getParent().args if args then return p.tableau( args ) end return "" end
return p