mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-16 16:22:20 +00:00
# Conflicts: # scripts/package-boundaries.json # src/i18n/locales/ar.json # src/i18n/locales/de.json # src/i18n/locales/es.json # src/i18n/locales/fr.json # src/i18n/locales/it.json # src/i18n/locales/ja.json # src/i18n/locales/pt.json # src/i18n/locales/ru.json # src/i18n/locales/uk.json # src/i18n/locales/zh-CN.json # src/i18n/locales/zh-Hant.json # src/layers/satellites/ingestion.test.mjs # src/layers/satellites/lifecycle.js # src/layers/satellites/state.js
125 lines
4.0 KiB
JavaScript
125 lines
4.0 KiB
JavaScript
import * as Cesium from 'cesium';
|
|
import { satelliteClassText } from '../../data/satelliteClass.js';
|
|
import { ISS_NORAD } from './policy.js';
|
|
|
|
/**
|
|
* Map one CelesTrak catalog satellite to a JSON-safe analyst record
|
|
* (analyst query engine seam). Pure — no Cesium types. Missing/unknown
|
|
* fields are null, never NaN/undefined. `id` is the display name (same
|
|
* convention as vessels); `noradId` is the track_entity key.
|
|
* @param {Object|null|undefined} raw - {noradId, name, group, lat, lon, altitudeM, speedMps}.
|
|
* @returns {{id: string, noradId: string|null, name: string|null,
|
|
* lat: number|null, lon: number|null, altitudeM: number|null,
|
|
* speedMps: number|null, satelliteClass: string|null, group: string|null}}
|
|
*/
|
|
export function mapAnalystRecord(raw) {
|
|
const num = (v) => (Number.isFinite(v) ? v : null);
|
|
const text = (v) => {
|
|
const t = String(v ?? '').trim();
|
|
return t || null;
|
|
};
|
|
const noradNum =
|
|
raw?.noradId == null || raw.noradId === '' ? NaN : Number(raw.noradId);
|
|
const noradId = Number.isFinite(noradNum)
|
|
? String(Math.trunc(noradNum))
|
|
: text(raw?.noradId);
|
|
const name = text(raw?.name);
|
|
const group = text(raw?.group);
|
|
const isIss = noradNum === ISS_NORAD;
|
|
return {
|
|
id: name || (noradId ? `SAT-${noradId}` : 'SAT-00000'),
|
|
noradId,
|
|
name,
|
|
lat: num(raw?.lat),
|
|
lon: num(raw?.lon),
|
|
altitudeM: num(raw?.altitudeM),
|
|
speedMps: num(raw?.speedMps),
|
|
// Empty records stay class-less; the class table's fallback is VISUAL and
|
|
// must not leak onto a row that has no satellite identity.
|
|
satelliteClass:
|
|
noradId || group
|
|
? satelliteClassText(text(raw?.satelliteClass), group, {
|
|
isIss,
|
|
localize: false,
|
|
})
|
|
: null,
|
|
group,
|
|
};
|
|
}
|
|
|
|
/** Create on-demand analyst snapshots owned by one layer instance. */
|
|
export function createRecords({ state, parts }) {
|
|
function analystGeo(noradId, sat, now) {
|
|
const pos = sat?.satrec
|
|
? parts.orbits.propagatePosition(sat.satrec, now)
|
|
: null;
|
|
if (
|
|
pos &&
|
|
Number.isFinite(pos.latitude) &&
|
|
Number.isFinite(pos.longitude)
|
|
) {
|
|
return {
|
|
lat: pos.latitude,
|
|
lon: pos.longitude,
|
|
altitudeM: pos.altitude,
|
|
speedMps: Number.isFinite(pos.speedMps) ? pos.speedMps : null,
|
|
};
|
|
}
|
|
const point = state._points.get(noradId);
|
|
if (!point?.position) return null;
|
|
const carto = Cesium.Cartographic.fromCartesian(point.position);
|
|
if (!carto) return null;
|
|
return {
|
|
lat: Cesium.Math.toDegrees(carto.latitude),
|
|
lon: Cesium.Math.toDegrees(carto.longitude),
|
|
altitudeM: carto.height,
|
|
speedMps: null,
|
|
};
|
|
}
|
|
|
|
function getAnalystRecords(maxCount = 2000) {
|
|
if (!state._enabled || !state._catalog.size) return [];
|
|
const limit = Number.isFinite(maxCount)
|
|
? Math.max(1, Math.floor(maxCount))
|
|
: 2000;
|
|
const now = new Date(state.now());
|
|
const result = [];
|
|
const emit = (noradId, sat) => {
|
|
if (result.length >= limit) return false;
|
|
// A class the user hid stays out of answers too; the tracked one stays.
|
|
if (
|
|
noradId !== state._trackedNorad &&
|
|
parts.controls?._classVisible?.(noradId) === false
|
|
)
|
|
return true;
|
|
const pos = analystGeo(noradId, sat, now);
|
|
if (!pos) return true;
|
|
result.push(
|
|
mapAnalystRecord({
|
|
noradId,
|
|
name: sat?.name,
|
|
group: sat?.group,
|
|
satelliteClass: sat?.satelliteClass,
|
|
lat: pos.lat,
|
|
lon: pos.lon,
|
|
altitudeM: pos.altitudeM,
|
|
speedMps: pos.speedMps,
|
|
}),
|
|
);
|
|
return result.length < limit;
|
|
};
|
|
for (const [noradId, sat] of state._catalog) {
|
|
if (sat?.group === 'dense') continue;
|
|
if (!emit(noradId, sat)) break;
|
|
}
|
|
if (result.length < limit) {
|
|
for (const [noradId, sat] of state._catalog) {
|
|
if (sat?.group !== 'dense') continue;
|
|
if (!emit(noradId, sat)) break;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
return { getAnalystRecords };
|
|
}
|