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,78 @@
|
||||
<?php
|
||||
/**
|
||||
* Ajoute / met à jour le code INSEE commune (comc) des établissements,
|
||||
* depuis l'opendata annuaire. Nécessaire pour relier les écoles à la
|
||||
* population INSEE (indexée par code commune) — cf. taux de scolarisation.
|
||||
*
|
||||
* Usage : php import_comc.php
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
require __DIR__ . '/api/db.php';
|
||||
|
||||
const ANNUAIRE_COMC =
|
||||
'https://data.education.gouv.fr/api/explore/v2.1/catalog/datasets/fr-en-annuaire-education/'
|
||||
. 'exports/csv?select=identifiant_de_l_etablissement,code_commune&delimiter=%3B';
|
||||
|
||||
function say(string $m): void { echo $m . "\n"; flush(); }
|
||||
|
||||
try {
|
||||
$pdo = db();
|
||||
try {
|
||||
$pdo->exec('ALTER TABLE etablissements ADD COLUMN comc VARCHAR(5) NULL');
|
||||
$pdo->exec('ALTER TABLE etablissements ADD INDEX idx_comc (comc)');
|
||||
say('✔ Colonne comc ajoutée.');
|
||||
} catch (Throwable $e) {
|
||||
say('ℹ Colonne comc déjà présente.');
|
||||
}
|
||||
|
||||
$tmp = __DIR__ . '/data/insee/annuaire_comc.csv';
|
||||
@mkdir(dirname($tmp), 0777, true);
|
||||
say('… Téléchargement (identifiant + code_commune) …');
|
||||
$fp = fopen($tmp, 'w');
|
||||
$ch = curl_init(ANNUAIRE_COMC);
|
||||
curl_setopt_array($ch, [
|
||||
CURLOPT_FILE => $fp, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 600,
|
||||
CURLOPT_USERAGENT => 'ecoles-import/1.0',
|
||||
]);
|
||||
if (!curl_exec($ch)) {
|
||||
throw new RuntimeException('Téléchargement échoué : ' . curl_error($ch));
|
||||
}
|
||||
curl_close($ch);
|
||||
fclose($fp);
|
||||
|
||||
$fh = fopen($tmp, 'r');
|
||||
$header = fgetcsv($fh, 0, ';');
|
||||
$header[0] = preg_replace('/^\xEF\xBB\xBF/', '', (string) $header[0]);
|
||||
$iId = array_search('identifiant_de_l_etablissement', $header, true);
|
||||
$iCom = array_search('code_commune', $header, true);
|
||||
if ($iId === false || $iCom === false) {
|
||||
throw new RuntimeException('Colonnes attendues introuvables.');
|
||||
}
|
||||
|
||||
$upd = $pdo->prepare('UPDATE etablissements SET comc = ? WHERE id = ?');
|
||||
$pdo->beginTransaction();
|
||||
$n = 0;
|
||||
while (($r = fgetcsv($fh, 0, ';')) !== false) {
|
||||
$id = $r[$iId] ?? '';
|
||||
$com = $r[$iCom] ?? '';
|
||||
if ($id === '' || $com === '') {
|
||||
continue;
|
||||
}
|
||||
$upd->execute([$com, $id]);
|
||||
if (++$n % 5000 === 0) { $pdo->commit(); $pdo->beginTransaction(); }
|
||||
}
|
||||
$pdo->commit();
|
||||
fclose($fh);
|
||||
@unlink($tmp);
|
||||
|
||||
$filled = (int) $pdo->query("SELECT COUNT(*) FROM etablissements WHERE comc IS NOT NULL AND comc <> ''")->fetchColumn();
|
||||
say("✔ comc renseigné pour $filled établissements (sur $n lignes).");
|
||||
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