Files
ecoles/import_pop_hist.php
Arnaud 5633969d98 Version initiale — carte des établissements scolaires
Carte Leaflet (annuaire Éducation nationale) + API PHP/MySQL :
fiche établissement (effectifs, IPS, population INSEE, DPE ADEME),
GED documents/photos, commentaires par groupe, gestion utilisateurs
et politique de mots de passe. Imports opendata (effectifs, IPS,
population, DPE, IRIS).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 21:35:41 +02:00

95 lines
3.4 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Import de l'historique des populations légales communales (INSEE).
* Source : public.opendatasoft.com, dataset « historique-des-populations-legales »
* (population municipale par commune et par recensement, ~1876 → 2022).
*
* Remplit la table pop_commune (comc = code INSEE commune, annee → population).
* Sert à tracer la courbe d'évolution de la population de la commune dans la fiche.
*
* Usage : php import_pop_hist.php
*/
declare(strict_types=1);
require __DIR__ . '/api/db.php';
const POP_HIST_CSV =
'https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets/'
. 'historique-des-populations-legales/exports/csv'
. '?select=code_insee,annee,population_municipale&delimiter=%3B';
function say(string $m): void { echo $m . "\n"; flush(); }
try {
$pdo = db();
$pdo->exec(
'CREATE TABLE IF NOT EXISTS pop_commune (
comc VARCHAR(5) NOT NULL,
annee SMALLINT NOT NULL,
pop INT NOT NULL DEFAULT 0,
PRIMARY KEY (comc, annee),
INDEX idx_comc (comc)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
);
$csv = __DIR__ . '/data/insee/pop_hist_communes.csv';
if (!is_file($csv)) {
@mkdir(dirname($csv), 0777, true);
say('… Téléchargement de lexport CSV …');
$fp = fopen($csv, 'w');
$ch = curl_init(POP_HIST_CSV);
curl_setopt_array($ch, [
CURLOPT_FILE => $fp, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 600,
CURLOPT_USERAGENT => 'ecoles-import/1.0',
]);
$ok = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
fclose($fp);
if (!$ok) {
@unlink($csv);
throw new RuntimeException('Téléchargement échoué : ' . $err);
}
}
$fh = fopen($csv, 'r');
$header = fgetcsv($fh, 0, ';');
$header[0] = preg_replace('/^\xEF\xBB\xBF/', '', (string) $header[0]); // retire un éventuel BOM
$iC = array_search('code_insee', $header, true);
$iY = array_search('annee', $header, true);
$iP = array_search('population_municipale', $header, true);
if ($iC === false || $iY === false || $iP === false) {
throw new RuntimeException('Colonnes code_insee / annee / population_municipale introuvables.');
}
$ins = $pdo->prepare('REPLACE INTO pop_commune (comc, annee, pop) VALUES (?, ?, ?)');
$pdo->beginTransaction();
$n = 0;
while (($row = fgetcsv($fh, 0, ';')) !== false) {
$comc = trim((string) ($row[$iC] ?? ''));
$annee = (int) ($row[$iY] ?? 0);
$pop = (int) round((float) str_replace([' ', ','], ['', '.'], (string) ($row[$iP] ?? '0')));
if ($comc === '' || $annee === 0) {
continue;
}
$ins->execute([$comc, $annee, $pop]);
if (++$n % 10000 === 0) {
$pdo->commit();
$pdo->beginTransaction();
if ($n % 100000 === 0) say("$n lignes");
}
}
$pdo->commit();
fclose($fh);
$communes = (int) $pdo->query('SELECT COUNT(DISTINCT comc) FROM pop_commune')->fetchColumn();
say("✔ pop_commune : $n lignes importées ($communes communes).");
say('🎉 Terminé.');
} catch (Throwable $e) {
if (isset($pdo) && $pdo->inTransaction()) {
$pdo->rollBack();
}
http_response_code(500);
say('!! ERREUR : ' . $e->getMessage());
exit(1);
}