diff --git a/public/sternenhimmel.php b/public/sternenhimmel.php index 2709870..03c4954 100644 --- a/public/sternenhimmel.php +++ b/public/sternenhimmel.php @@ -6,12 +6,12 @@ ini_set('memory_limit', '512M'); session_start(); $loggedIn = isset($_SESSION['user_id']); +$starsApiRequested = isset($_GET['stars_api']) && (string)$_GET['stars_api'] === '1'; $pageTitle = 'Sternenhimmel - AstroTools'; $bodyClass = 'sternenhimmel-page'; $constellationData = ['constellations' => []]; $constellationDataError = null; -$starData = []; $starDataError = null; $messierData = []; $messierDataError = null; @@ -105,31 +105,41 @@ try { static fn(array $a, array $b): int => strcmp($a['id'], $b['id']) ); - $constellationData['constellations'] = array_values($constellations); + if ($starsApiRequested) { + $starData = []; + $starStmt = $planetariumPdo->query( + "SELECT hip, proper, bayer, con, spect, mag, ci, rarad, decrad + FROM star_hipparcos + WHERE mag IS NOT NULL + AND rarad IS NOT NULL + AND decrad IS NOT NULL + AND mag <= " . (float)$starDbMagLimit . " + ORDER BY mag ASC" + ); + while ($row = $starStmt->fetch()) { + $starData[] = [ + 'hip' => $row['hip'] !== null ? (int)$row['hip'] : null, + 'proper' => trim((string)($row['proper'] ?? '')), + 'bayer' => trim((string)($row['bayer'] ?? '')), + 'con' => trim((string)($row['con'] ?? '')), + 'spect' => trim((string)($row['spect'] ?? '')), + 'ra' => (float)$row['rarad'], + 'dec' => (float)$row['decrad'], + 'mag' => (float)$row['mag'], + 'ci' => $row['ci'] !== null ? (float)$row['ci'] : null, + ]; + } - $starStmt = $planetariumPdo->query( - "SELECT hip, proper, bayer, con, spect, mag, ci, rarad, decrad - FROM star_hipparcos - WHERE mag IS NOT NULL - AND rarad IS NOT NULL - AND decrad IS NOT NULL - AND mag <= " . (float)$starDbMagLimit . " - ORDER BY mag ASC" - ); - while ($row = $starStmt->fetch()) { - $starData[] = [ - 'hip' => $row['hip'] !== null ? (int)$row['hip'] : null, - 'proper' => trim((string)($row['proper'] ?? '')), - 'bayer' => trim((string)($row['bayer'] ?? '')), - 'con' => trim((string)($row['con'] ?? '')), - 'spect' => trim((string)($row['spect'] ?? '')), - 'ra' => (float)$row['rarad'], - 'dec' => (float)$row['decrad'], - 'mag' => (float)$row['mag'], - 'ci' => $row['ci'] !== null ? (float)$row['ci'] : null, - ]; + header('Content-Type: application/json; charset=utf-8'); + echo json_encode([ + 'ok' => true, + 'stars' => $starData, + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + exit; } + $constellationData['constellations'] = array_values($constellations); + $messierStmt = $planetariumPdo->query( "SELECT messier, name, object_type_abrev, ra_deg, dec_deg, v_mag, major_axis FROM dso_objects @@ -177,6 +187,15 @@ try { } } } catch (Throwable $e) { + if ($starsApiRequested) { + header('Content-Type: application/json; charset=utf-8'); + http_response_code(500); + echo json_encode([ + 'ok' => false, + 'error' => $e->getMessage(), + ], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); + exit; + } $constellationDataError = $e->getMessage(); $starDataError = $e->getMessage(); $messierDataError = $e->getMessage(); @@ -190,10 +209,6 @@ $constellationDataErrorJson = json_encode( $constellationDataError, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); -$starDataJson = json_encode( - $starData, - JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES -); $starDataErrorJson = json_encode( $starDataError, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES @@ -549,7 +564,7 @@ let showStars = true; // ═══════════════════════════════════════════════════════════════════════════════ const EMBEDDED_CONSTELLATION_DATA = ; const EMBEDDED_CONSTELLATION_ERROR = ; -const EMBEDDED_STAR_DATA = ; +const STAR_DATA_URL = 'sternenhimmel.php?stars_api=1'; const EMBEDDED_STAR_ERROR = ; const EMBEDDED_MESSIER_DATA = ; const EMBEDDED_MESSIER_ERROR = ; @@ -1805,8 +1820,7 @@ function finalizeStarLoad() { if (AUTO_IMG) autoExportPng(); } -function loadEmbeddedStarsAsync() { - const source = Array.isArray(EMBEDDED_STAR_DATA) ? EMBEDDED_STAR_DATA : []; +function processStarBatch(source) { const total = source.length; const chunkSize = total > 60000 ? 1800 : 1200; let index = 0; @@ -1862,6 +1876,27 @@ function loadEmbeddedStarsAsync() { window.setTimeout(processChunk, 0); } +async function loadEmbeddedStarsAsync() { + updateStarLoadingUi(0, 1); + if (starLoadingNote) starLoadingNote.textContent = 'Sternkatalog wird vom Server geladen ...'; + + const response = await fetch(STAR_DATA_URL, { + headers: { 'Accept': 'application/json' }, + cache: 'no-store' + }); + + if (!response.ok) { + throw new Error('Sternkatalog konnte nicht geladen werden (HTTP ' + response.status + ')'); + } + + const payload = await response.json(); + if (!payload || payload.ok !== true || !Array.isArray(payload.stars)) { + throw new Error(payload && payload.error ? payload.error : 'Ungültige Sternkatalog-Antwort'); + } + + processStarBatch(payload.stars); +} + // Manueller Fallback über Datei-Dialog document.getElementById('fileInput').addEventListener('change', function(e) { const file = e.target.files[0]; @@ -2017,7 +2052,13 @@ syncMessierMagLimitUi(); setLocation(LAT_DEG, LON_DEG, ELEV_M, HEADER_LOCATION.name || 'Standard-Standort', HEADER_LOCATION.id ?? ''); resize(); window.requestAnimationFrame(() => { - window.setTimeout(loadEmbeddedStarsAsync, 40); + window.setTimeout(() => { + loadEmbeddedStarsAsync().catch(err => { + console.warn('Sterndaten aus der Datenbank nicht geladen:', err); + if (starLoadingStatus) starLoadingStatus.textContent = 'Fehler'; + if (starLoadingNote) starLoadingNote.textContent = err && err.message ? err.message : 'Sternkatalog konnte nicht geladen werden'; + }); + }, 40); }); }); // end window.onload