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>
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
<?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 l’export 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);
|
||||
}
|
||||
Reference in New Issue
Block a user