getDatabaseConfig()); $db = Database::getInstance(); // 1. Vérifier la table echo "1. Vérification de la table x_departements_contours:\n"; $sql = "SHOW TABLES LIKE 'x_departements_contours'"; $stmt = $db->query($sql); if ($stmt->rowCount() > 0) { echo " ✓ Table existe\n"; // Compter les enregistrements $sql = "SELECT COUNT(*) as count FROM x_departements_contours"; $stmt = $db->query($sql); $result = $stmt->fetch(); echo " - Nombre d'enregistrements : " . $result['count'] . "\n"; } else { echo " ✗ Table n'existe pas\n"; exit(1); } // 2. Tester l'API geo.api.gouv.fr echo "\n2. Test de l'API geo.api.gouv.fr:\n"; $testDept = '22'; $url = "https://geo.api.gouv.fr/departements/{$testDept}?fields=nom,contour"; $context = stream_context_create([ 'http' => [ 'timeout' => 30, 'header' => "User-Agent: Geosector/1.0\r\n" ] ]); echo " - Récupération du département $testDept...\n"; $response = @file_get_contents($url, false, $context); if ($response === false) { echo " ✗ Erreur lors de la récupération\n"; $error = error_get_last(); echo " - Erreur : " . $error['message'] . "\n"; } else { echo " ✓ Données récupérées\n"; $data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { echo " ✗ Erreur JSON : " . json_last_error_msg() . "\n"; } else { echo " - Nom : " . ($data['nom'] ?? 'Non trouvé') . "\n"; echo " - Contour présent : " . (isset($data['contour']) ? 'Oui' : 'Non') . "\n"; if (isset($data['contour']['coordinates'])) { $coords = $data['contour']['coordinates']; // Analyser la structure echo "\n3. Analyse de la structure des coordonnées:\n"; echo " - Type de contour : " . ($data['contour']['type'] ?? 'Inconnu') . "\n"; echo " - Niveaux d'imbrication : "; $level = 0; $temp = $coords; while (is_array($temp) && !is_numeric($temp[0] ?? null)) { $level++; $temp = $temp[0]; } echo "$level\n"; // Tester la conversion en WKT echo "\n4. Test de conversion en WKT:\n"; // Extraction des coordonnées selon le type $ring = null; if ($data['contour']['type'] === 'Polygon') { $ring = $coords[0]; } elseif ($data['contour']['type'] === 'MultiPolygon') { $ring = $coords[0][0]; } if ($ring) { $points = []; foreach ($ring as $i => $point) { if ($i < 5) { // Afficher les 5 premiers points echo " - Point $i : [" . $point[0] . ", " . $point[1] . "]\n"; } $points[] = $point[0] . ' ' . $point[1]; } echo " - Total points : " . count($ring) . "\n"; // Créer le WKT $wkt = 'POLYGON((' . implode(',', $points) . '))'; echo " - WKT (début) : " . substr($wkt, 0, 100) . "...\n"; // Tester l'insertion echo "\n5. Test d'insertion dans la base:\n"; try { $sql = "INSERT INTO x_departements_contours (code_dept, nom_dept, contour, bbox_min_lat, bbox_max_lat, bbox_min_lng, bbox_max_lng) VALUES (:code, :nom, ST_GeomFromText(:polygon, 4326), :min_lat, :max_lat, :min_lng, :max_lng)"; $stmt = $db->prepare($sql); // Calculer la bounding box $lats = array_map(function($p) { return $p[1]; }, $ring); $lngs = array_map(function($p) { return $p[0]; }, $ring); $params = [ 'code' => $testDept, 'nom' => $data['nom'], 'polygon' => $wkt, 'min_lat' => min($lats), 'max_lat' => max($lats), 'min_lng' => min($lngs), 'max_lng' => max($lngs) ]; echo " - Paramètres :\n"; echo " * Code : " . $params['code'] . "\n"; echo " * Nom : " . $params['nom'] . "\n"; echo " * BBox : [" . $params['min_lat'] . ", " . $params['min_lng'] . "] - [" . $params['max_lat'] . ", " . $params['max_lng'] . "]\n"; // Ne pas vraiment insérer, juste tester echo " - Test de validation du polygone...\n"; $testSql = "SELECT ST_IsValid(ST_GeomFromText(:polygon, 4326)) as is_valid"; $testStmt = $db->prepare($testSql); $testStmt->execute(['polygon' => $wkt]); $testResult = $testStmt->fetch(); if ($testResult['is_valid']) { echo " ✓ Polygone valide\n"; } else { echo " ✗ Polygone invalide\n"; } } catch (Exception $e) { echo " ✗ Erreur : " . $e->getMessage() . "\n"; } } } } } // 6. Vérifier les logs d'erreur PHP echo "\n6. Dernières erreurs PHP:\n"; $errorLog = ini_get('error_log'); if ($errorLog && file_exists($errorLog)) { $lines = array_slice(file($errorLog), -5); foreach ($lines as $line) { if (strpos($line, 'departements_contours') !== false) { echo " " . trim($line) . "\n"; } } } } catch (Exception $e) { echo "✗ Erreur : " . $e->getMessage() . "\n"; echo "Trace:\n" . $e->getTraceAsString() . "\n"; } echo "\n";