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,77 @@
|
||||
<?php
|
||||
/**
|
||||
* Import des populations du recensement INSEE 2022 (base infracommunale
|
||||
* « évolution et structure de la population »).
|
||||
*
|
||||
* Importe le fichier IRIS dans la table iris_pop (code IRIS → population P22_POP).
|
||||
* La population communale est déduite par somme des IRIS d'une même commune
|
||||
* (les 5 premiers caractères du code IRIS = code commune).
|
||||
*
|
||||
* Pré-requis : télécharger et DÉZIPPER la base dans data/insee/ :
|
||||
* https://www.insee.fr/fr/statistiques/fichier/8647014/base-ic-evol-struct-pop-2022_csv.zip
|
||||
* → data/insee/base-ic-evol-struct-pop-2022.CSV
|
||||
*
|
||||
* Usage : php import_pop.php
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
require __DIR__ . '/api/db.php';
|
||||
|
||||
function say(string $m): void { echo $m . "\n"; flush(); }
|
||||
|
||||
try {
|
||||
$pdo = db();
|
||||
$pdo->exec(
|
||||
'CREATE TABLE IF NOT EXISTS iris_pop (
|
||||
code VARCHAR(12) NOT NULL PRIMARY KEY,
|
||||
com VARCHAR(5) NOT NULL,
|
||||
pop DOUBLE NOT NULL DEFAULT 0,
|
||||
INDEX idx_com (com)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4'
|
||||
);
|
||||
|
||||
$csv = __DIR__ . '/data/insee/base-ic-evol-struct-pop-2022.CSV';
|
||||
if (!is_file($csv)) {
|
||||
throw new RuntimeException(
|
||||
"Fichier absent : $csv\n" .
|
||||
" Téléchargez et dézippez la base IRIS dans data/insee/ (voir l'en-tête de ce script)."
|
||||
);
|
||||
}
|
||||
|
||||
$fh = fopen($csv, 'r');
|
||||
$header = fgetcsv($fh, 0, ';');
|
||||
$iIris = array_search('IRIS', $header, true);
|
||||
$iCom = array_search('COM', $header, true);
|
||||
$iPop = array_search('P22_POP', $header, true);
|
||||
if ($iIris === false || $iCom === false || $iPop === false) {
|
||||
throw new RuntimeException('Colonnes IRIS / COM / P22_POP introuvables.');
|
||||
}
|
||||
|
||||
$ins = $pdo->prepare('REPLACE INTO iris_pop (code, com, pop) VALUES (?, ?, ?)');
|
||||
$pdo->beginTransaction();
|
||||
$n = 0;
|
||||
while (($row = fgetcsv($fh, 0, ';')) !== false) {
|
||||
$code = $row[$iIris] ?? '';
|
||||
if ($code === '') {
|
||||
continue;
|
||||
}
|
||||
$com = $row[$iCom] ?? substr($code, 0, 5);
|
||||
$pop = (float) str_replace(',', '.', (string) ($row[$iPop] ?? '0'));
|
||||
$ins->execute([$code, $com, $pop]);
|
||||
if (++$n % 5000 === 0) {
|
||||
$pdo->commit();
|
||||
$pdo->beginTransaction();
|
||||
}
|
||||
}
|
||||
$pdo->commit();
|
||||
fclose($fh);
|
||||
|
||||
say("✔ iris_pop : $n IRIS importés (population 2022).");
|
||||
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