&com= * → { iris_pop, com_pop, com_name } pour un IRIS et sa commune * POST ?action=batch body {codes:[...]} * → { "": 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); }