5633969d98
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>
80 lines
2.7 KiB
PHP
80 lines
2.7 KiB
PHP
<?php
|
|
/**
|
|
* Population des zones IRIS (recensement INSEE 2022).
|
|
*
|
|
* GET ?code=<code_iris>&com=<code_commune>
|
|
* → { iris_pop, com_pop, com_name } pour un IRIS et sa commune
|
|
* POST ?action=batch body {codes:[...]}
|
|
* → { "<code_iris>": pop, ... } pour colorer la couche
|
|
*
|
|
* Population communale = somme des IRIS de la commune. Pour Paris, Lyon et
|
|
* Marseille, les données sont à l'arrondissement : on agrège à l'échelle de
|
|
* la VILLE entière (com_name renvoie alors « Paris » / « Lyon » / « Marseille »).
|
|
*/
|
|
declare(strict_types=1);
|
|
require __DIR__ . '/db.php';
|
|
|
|
/** Ville PLM correspondant à un code commune-arrondissement, ou null. */
|
|
function plm_city(string $com): ?array
|
|
{
|
|
if ($com >= '75101' && $com <= '75120') return ['Paris', '75101', '75120'];
|
|
if ($com >= '69381' && $com <= '69389') return ['Lyon', '69381', '69389'];
|
|
if ($com >= '13201' && $com <= '13216') return ['Marseille', '13201', '13216'];
|
|
return null;
|
|
}
|
|
|
|
try {
|
|
// --- Batch : populations d'une liste d'IRIS (pour la coloration) ---
|
|
if (($_GET['action'] ?? '') === 'batch') {
|
|
$codes = body_json()['codes'] ?? [];
|
|
if (!is_array($codes) || !$codes) {
|
|
json_out([]);
|
|
}
|
|
$codes = array_slice(array_values(array_unique(array_map('strval', $codes))), 0, 5000);
|
|
$ph = implode(',', array_fill(0, count($codes), '?'));
|
|
$st = db()->prepare("SELECT code, pop FROM iris_pop WHERE code IN ($ph)");
|
|
$st->execute($codes);
|
|
$out = [];
|
|
foreach ($st as $r) {
|
|
$out[$r['code']] = (float) $r['pop'];
|
|
}
|
|
json_out($out);
|
|
}
|
|
|
|
// --- Détail d'un IRIS + sa commune ---
|
|
$code = trim((string) ($_GET['code'] ?? ''));
|
|
$com = trim((string) ($_GET['com'] ?? ''));
|
|
|
|
$irisPop = null;
|
|
if ($code !== '') {
|
|
$st = db()->prepare('SELECT pop FROM iris_pop WHERE code = ?');
|
|
$st->execute([$code]);
|
|
$v = $st->fetchColumn();
|
|
if ($v !== false) {
|
|
$irisPop = (int) round((float) $v);
|
|
}
|
|
}
|
|
|
|
$comPop = null;
|
|
$comName = null;
|
|
if ($com !== '') {
|
|
$plm = plm_city($com);
|
|
if ($plm) {
|
|
$comName = $plm[0];
|
|
$st = db()->prepare('SELECT SUM(pop) FROM iris_pop WHERE com BETWEEN ? AND ?');
|
|
$st->execute([$plm[1], $plm[2]]);
|
|
} else {
|
|
$st = db()->prepare('SELECT SUM(pop) FROM iris_pop WHERE com = ?');
|
|
$st->execute([$com]);
|
|
}
|
|
$v = $st->fetchColumn();
|
|
if ($v !== null && $v !== false) {
|
|
$comPop = (int) round((float) $v);
|
|
}
|
|
}
|
|
|
|
json_out(['iris_pop' => $irisPop, 'com_pop' => $comPop, 'com_name' => $comName]);
|
|
} catch (Throwable $e) {
|
|
json_error('Erreur : ' . $e->getMessage(), 500);
|
|
}
|