/* GEO EVENTS · 全球极端事件态势 */ const DATA = 'data/'; const $ = (id) => document.getElementById(id); const GROUP = { '大规模暴力': 'conflict', '武装冲突': 'conflict', '武装袭击': 'conflict', '军事部署': 'mil', '胁迫': 'mil', '威胁制裁': 'mil', '抗议骚乱': 'social', '政治施压': 'social', '减少关系': 'social', '外交协商': 'diplo', '外交表态': 'diplo', '调查': 'diplo', '其他': 'diplo', '物质合作': 'coop', '提供援助': 'coop', '让步妥协': 'coop', '地震': 'quake', '自然事件': 'nature', }; const COLOR = { conflict: '#ff6b5a', mil: '#ff8a5a', social: '#56a8ff', diplo: '#7f93ad', coop: '#4ade9f', quake: '#ffb454', nature: '#d9c85c', }; const NAME = { conflict: '武装冲突', mil: '军事胁迫', social: '社会动荡', diplo: '外交政治', coop: '合作援助', quake: '地震', nature: '自然事件', }; const CCOLOR = { 1: '#d9a52b', 2: '#e0553f', 3: '#b98cff' }; /* 国名注记:只在球面放大到一定程度后出现 */ const LABELS = [ ['中国', 104, 34], ['美国', -100, 39], ['俄罗斯', 96, 61], ['印度', 79, 22], ['巴西', -51, -11], ['澳大利亚', 134, -25], ['加拿大', -106, 57], ['阿尔及利亚', 2, 27], ['尼日利亚', 8, 9], ['埃及', 30, 26], ['南非', 24, -29], ['沙特', 45, 24], ['伊朗', 54, 32], ['土耳其', 35, 39], ['乌克兰', 31, 49], ['法国', 2, 46], ['德国', 10, 51], ['日本', 138, 37], ['印尼', 113, -2], ['阿根廷', -64, -35], ['墨西哥', -102, 24], ['哈萨克斯坦', 68, 48], ]; const state = { summary: null, events: [], news: [], cdhe: null, hours: 24, markers: [], cLabels: [], disasters: [], fires: { type: 'FeatureCollection', features: [] }, flights: [], rank: 'epi', feedTop: null, pulses: [], countries: null, feed: 'news', region: '全球' }; const domain = (u) => { try { return new URL(u).hostname.replace(/^www\./, ''); } catch (e) { return ''; } }; const fmt = (n) => (n >= 1e6 ? (n / 1e6).toFixed(1) + 'M' : n >= 1e4 ? (n / 1e3).toFixed(1) + 'k' : String(n)); /* ---------------- 地图 ---------------- */ const map = new maplibregl.Map({ container: 'map', style: { version: 8, projection: { type: 'globe' }, sources: {}, layers: [{ id: 'bg', type: 'background', paint: { 'background-color': '#04070c', 'background-opacity': 0 } }], }, center: [18, 20], zoom: 2.5, minZoom: 1.0, maxZoom: 7, attributionControl: false, dragRotate: false, pitchWithRotate: false, }); window.__map = map; map.on('error', (e) => console.error('MAPERR', e && e.error && e.error.message)); map.addControl(new maplibregl.NavigationControl({ showCompass: false }), 'bottom-right'); function applyGlobe() { try { map.setProjection({ type: 'globe' }); map.setSky({ 'sky-color': '#05090f', 'sky-horizon-blend': 0.36, 'horizon-color': '#134667', 'horizon-fog-blend': 0.14, 'fog-color': '#06111c', 'fog-ground-blend': 0.42, 'atmosphere-blend': 0.45, }); } catch (e) { console.warn('globe 不可用', e); } } function graticule() { const f = []; for (let lon = -180; lon < 180; lon += 30) { const c = []; for (let lat = -85; lat <= 85; lat += 2.5) c.push([lon, lat]); f.push({ type: 'Feature', geometry: { type: 'LineString', coordinates: c } }); } for (let lat = -60; lat <= 60; lat += 30) { const c = []; for (let lon = -180; lon <= 180; lon += 2.5) c.push([lon, lat]); f.push({ type: 'Feature', geometry: { type: 'LineString', coordinates: c } }); } return { type: 'FeatureCollection', features: f }; } const REGIONS = [ ['全球', 18, 20, 2.5], ['中东', 45, 29, 3.6], ['欧洲', 12, 50, 3.7], ['东亚', 116, 32, 3.6], ['南亚', 78, 22, 3.8], ['北美', -100, 40, 3.1], ['非洲', 20, 2, 3.0], ['南美', -60, -15, 3.0], ]; /* Natural Earth 对争议地区给 FIPS=-99,这里按英文名桥接到 GDELT 的 FIPS 代码 */ const BRIDGE = { Israel: ['IS'], Palestine: ['WE', 'GZ'], 'South Sudan': ['OD'], Norway: ['NO'], Mozambique: ['MZ'], 'East Timor': ['TT'], 'Guinea-Bissau': ['PU'], 'Western Sahara': ['WI'], 'Falkland Islands': ['FK'], 'French Southern and Antarctic Lands': ['FS'], }; /** 把国家级 EPI 写回国界要素,供 fill-color 直接读 */ function enrichCountries(gj) { const rows = (state.summary && state.summary.all_countries) || []; const byFips = new Map(rows.map((r) => [r.f, r])); state.nameOf = new Map(); const max = Math.max(...rows.map((r) => r.epi), 1); let hit = 0; gj.features.forEach((f) => { const p = f.properties; let hits = []; const direct = byFips.get(p.FIPS_10) || byFips.get(p.ISO_A2); if (direct) hits = [direct]; else if (BRIDGE[p.NAME_EN]) hits = BRIDGE[p.NAME_EN].map((k) => byFips.get(k)).filter(Boolean); if (hits.length && hits.some((r) => r.epi > 0)) { p.epi = hits.reduce((s, r) => s + r.epi, 0); p.n = hits.reduce((s, r) => s + (r.n || 0), 0); p.p = Math.pow(p.epi / max, 0.55); // 字典里没有的小国只回了两位代码,这时用国界自带的中文名 p.cn = (hits[0].cc && hits[0].cc !== hits[0].f) ? hits[0].cc : (f.properties.NAME_ZH || f.properties.NAME); p.fips = hits[0].f; if (!state.nameOf.has(p.fips)) state.nameOf.set(p.fips, p.cn); hit++; } else { p.epi = 0; p.p = null; p.cn = p.NAME_ZH || p.NAME; } // 粗略中心:外环顶点均值,足够用于飞到该国 const g = f.geometry; let ring = null; if (g.type === 'Polygon') ring = g.coordinates[0]; else if (g.type === 'MultiPolygon') { ring = g.coordinates.reduce((a, b) => (b[0].length > a[0].length ? b : a))[0]; } if (ring) { let sx = 0, sy = 0; ring.forEach(([x, y]) => { sx += x; sy += y; }); p.cx = sx / ring.length; p.cy = sy / ring.length; } }); console.log('国界匹配', hit, '/', gj.features.length); return gj; } function eventFeatures() { return { type: 'FeatureCollection', features: state.events.map((p) => ({ type: 'Feature', geometry: { type: 'Point', coordinates: [p.lon, p.lat] }, properties: { grp: GROUP[p.cat] || 'diplo', cat: p.cat, cc: p.cc, m: p.m || 0, t: p.t, title: p.title || '', url: p.url || '', src: p.src, w: p.w || 3, ts: Date.parse(p.ts) / 1000, r: Math.min(6.5, 1.8 + Math.sqrt(p.m || 0) * 0.36), }, })), }; } function cdheFeatures() { return { type: 'FeatureCollection', features: state.cdhe.points.map((p) => ({ type: 'Feature', geometry: { type: 'Point', coordinates: [p[0], p[1]] }, properties: { s: p[2], spi: p[3], sti: p[4] }, })), }; } function oceanSource() { const bands = []; for (let lon = -180; lon < 180; lon += 40) { const a = lon - 0.1, b = Math.min(180, lon + 40.1); bands.push({ type: 'Feature', geometry: { type: 'Polygon', coordinates: [[ [a, -89], [b, -89], [b, 89], [a, 89], [a, -89]]] } }); } return { type: 'FeatureCollection', features: bands }; } const TILES = '/tiles/gibs/'; function addBaseRaster() { // 真实地表:NASA GIBS 静态影像,本地 nginx 已做 30 天缓存 map.addSource('base-night', { type: 'raster', tileSize: 256, maxzoom: 6, attribution: 'NASA GIBS / VIIRS', tiles: [TILES + 'VIIRS_CityLights_2012/default/2012-01-01/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpg'], }); map.addLayer({ id: 'base-night', type: 'raster', source: 'base-night', paint: { 'raster-opacity': 0.9, 'raster-brightness-max': 0.95, 'raster-contrast': 0.1, 'raster-fade-duration': 260 } }); map.addSource('base-relief', { type: 'raster', tileSize: 256, maxzoom: 6, attribution: 'NASA GIBS / Blue Marble', tiles: [TILES + 'BlueMarble_ShadedRelief_Bathymetry/default/500m/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpeg'], }); map.addLayer({ id: 'base-relief', type: 'raster', source: 'base-relief', paint: { 'raster-opacity': 0.96, 'raster-saturation': 0.05, 'raster-brightness-min': 0.04, 'raster-brightness-max': 0.95, 'raster-contrast': -0.12, 'raster-fade-duration': 180 } }); map.setLayoutProperty('base-relief', 'visibility', 'none'); } function addLayers() { addBaseRaster(); // 极简模式下的球体:海面给一点深蓝,陆地压暗,形成"球"而不是"平面" map.addSource('ocean', { type: 'geojson', data: oceanSource() }); map.addLayer({ id: 'ocean', type: 'fill', source: 'ocean', paint: { 'fill-color': '#081726', 'fill-opacity': 1 } }); map.setLayoutProperty('ocean', 'visibility', 'none'); map.addSource('countries', { type: 'geojson', data: state.countries }); map.addLayer({ id: 'c-fill', type: 'fill', source: 'countries', paint: { 'fill-color': ['case', ['==', ['get', 'p'], null], '#0e1a28', ['interpolate', ['linear'], ['get', 'p'], 0.00, '#132030', 0.30, '#2b3038', 0.55, '#5e4433', 0.78, '#a3553c', 1.00, '#ff6b5a']], 'fill-opacity': ['case', ['==', ['get', 'p'], null], 0.18, ['interpolate', ['linear'], ['get', 'p'], 0, 0.08, 1, 0.34]], } }); // 两层描边:外层低透明度做微光,内层细线做边界 map.addLayer({ id: 'c-glow', type: 'line', source: 'countries', paint: { 'line-color': '#2f6f9e', 'line-width': 2.2, 'line-opacity': 0.10, 'line-blur': 1.6 } }); map.addLayer({ id: 'c-line', type: 'line', source: 'countries', paint: { 'line-color': '#29506f', 'line-width': 0.45, 'line-opacity': 0.85 } }); map.addSource('grat', { type: 'geojson', data: graticule() }); map.addLayer({ id: 'grat', type: 'line', source: 'grat', paint: { 'line-color': '#12324c', 'line-width': 0.4, 'line-opacity': 0.55 } }); // CDHE:按三态各一层热力,格点融合成连续场 map.addSource('cdhe', { type: 'geojson', data: cdheFeatures() }); const heat = (id, s, ramp) => map.addLayer({ id, type: 'heatmap', source: 'cdhe', filter: ['==', ['get', 's'], s], paint: { 'heatmap-weight': 1, 'heatmap-intensity': ['interpolate', ['linear'], ['zoom'], 1, 0.5, 3, 0.44, 6, 0.38], 'heatmap-radius': ['interpolate', ['exponential', 1.9], ['zoom'], 1, 12, 2, 20, 3, 34, 4, 64, 5, 120, 6, 240], 'heatmap-color': ['interpolate', ['linear'], ['heatmap-density'], 0, 'rgba(0,0,0,0)', 0.35, ramp[0], 0.7, ramp[1], 1, ramp[2]], 'heatmap-opacity': 0.58, }, }); heat('cdhe-h1', 1, ['rgba(217,165,43,.10)', 'rgba(217,165,43,.26)', 'rgba(236,190,80,.46)']); heat('cdhe-h2', 2, ['rgba(224,85,63,.10)', 'rgba(224,85,63,.26)', 'rgba(255,120,95,.46)']); heat('cdhe-h3', 3, ['rgba(185,140,255,.12)', 'rgba(185,140,255,.34)', 'rgba(214,180,255,.62)']); // 放大后补一圈细点,保留"这是格点"的信息 map.addLayer({ id: 'cdhe-pts', type: 'circle', source: 'cdhe', minzoom: 2.4, paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 2.4, 1.2, 6, 3.2], 'circle-color': ['match', ['get', 's'], 1, CCOLOR[1], 2, CCOLOR[2], 3, CCOLOR[3], '#888'], 'circle-opacity': ['interpolate', ['linear'], ['zoom'], 2.4, 0, 3.6, 0.4], 'circle-blur': 0.2, } }); // 事件点 map.addSource('events', { type: 'geojson', data: eventFeatures() }); const paint = (c, op) => ({ 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, ['*', ['get', 'r'], 0.8], 3, ['*', ['get', 'r'], 1.0], 6, ['*', ['get', 'r'], 1.6]], 'circle-color': c, 'circle-opacity': op, 'circle-blur': 0.1, 'circle-stroke-width': 0.45, 'circle-stroke-color': 'rgba(230,242,255,.5)', }); [ ['ev-conflict', 'conflict', 0.85], ['ev-mil', 'mil', 0.8], ['ev-social', 'social', 0.8], ['ev-diplo', 'diplo', 0.4], ['ev-coop', 'coop', 0.45], ['ev-quake', 'quake', 0.9], ['ev-nature', 'nature', 0.8], ].forEach(([id, k, op]) => { map.addLayer({ id, type: 'circle', source: 'events', filter: ['==', ['get', 'grp'], k], paint: paint(COLOR[k], op) }); }); // 地震单独放在 nature 组之外,按类别判断 map.setFilter('ev-quake', ['==', ['get', 'cat'], '地震']); map.setFilter('ev-nature', ['==', ['get', 'cat'], '自然事件']); addDisasters(); addFires(); addFlights(); addSats(); addLabels(); } const DIS = [ ['dis-flood', ['FL'], '#3fa9ff'], ['dis-cyclone', ['TC'], '#35e0c8'], ['dis-drought', ['DR'], '#d9a52b'], ['dis-volcano', ['VO'], '#b06ef0'], ['dis-wildfire', ['WF'], '#ff8a3d'], ]; const DIS_TOG = { FL: 'l-flood', TC: 'l-cyclone', DR: 'l-drought', VO: 'l-volcano', WF: 'l-wildfire' }; const ALR = ['match', ['get', 'al'], 'Red', 1.65, 'Orange', 1.3, 1]; function disasterFC() { return { type: 'FeatureCollection', features: (state.disasters || []).map((d) => ({ type: 'Feature', geometry: { type: 'Point', coordinates: [d.lon, d.lat] }, properties: d })) }; } function addDisasters() { map.addSource('disasters', { type: 'geojson', data: disasterFC() }); DIS.forEach(([id, types, color]) => { map.addLayer({ id, type: 'circle', source: 'disasters', filter: ['in', ['get', 'et'], ['literal', types]], paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, ['*', 2.4, ALR], 4, ['*', 3.9, ALR], 7, ['*', 5.8, ALR]], 'circle-color': color, 'circle-opacity': 0.2, 'circle-stroke-color': ['match', ['get', 'al'], 'Red', '#ff4d4d', 'Orange', '#ffb03a', color], 'circle-stroke-width': ['match', ['get', 'al'], 'Red', 1.8, 'Orange', 1.4, 0.9], 'circle-stroke-opacity': 0.82, } }); }); } function addFires() { map.addSource('fires', { type: 'geojson', data: state.fires }); map.addLayer({ id: 'fire-heat', type: 'heatmap', source: 'fires', paint: { 'heatmap-weight': ['interpolate', ['linear'], ['get', 'f'], 0, 0.22, 40, 0.6, 200, 1], 'heatmap-intensity': ['interpolate', ['linear'], ['zoom'], 1, 0.5, 3, 0.7, 6, 0.85], 'heatmap-radius': ['interpolate', ['exponential', 1.6], ['zoom'], 1, 8, 2, 11, 3, 15, 5, 22, 7, 32], 'heatmap-color': ['interpolate', ['linear'], ['heatmap-density'], 0, 'rgba(0,0,0,0)', 0.3, 'rgba(255,130,60,.16)', 0.65, 'rgba(255,110,50,.4)', 1, 'rgba(255,205,130,.7)'], 'heatmap-opacity': 0.8, } }); map.addLayer({ id: 'fire-pt', type: 'circle', source: 'fires', minzoom: 2.6, paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 2.6, 0.9, 6, 2.2], 'circle-color': '#ffc078', 'circle-opacity': 0.55, 'circle-blur': 0.3, } }); } const AIRLINE = { CCA: '中国国航', CES: '东方航空', CSN: '南方航空', CHH: '海南航空', CXA: '厦门航空', CSC: '四川航空', CSZ: '深圳航空', CQH: '春秋航空', CUA: '中国联航', CYZ: '顺丰航空', CAL: '中华航空', EVA: '长荣航空', CPA: '国泰航空', HKE: '香港快运', CRK: '香港航空', JAL: '日本航空', ANA: '全日空', APJ: '乐桃航空', KAL: '大韩航空', AAR: '韩亚航空', SIA: '新加坡航空', SLK: '酷航', MAS: '马来西亚航空', AXM: '亚洲航空', GIA: '印尼鹰航', LNI: '狮航', CEB: '宿务太平洋', PAL: '菲律宾航空', THA: '泰国国际', AIQ: '泰亚航', VNL: '越捷', HVN: '越南航空', AIC: '印度航空', IGO: '靛蓝航空', VTI: '印度快运', UAE: '阿联酋航空', ETD: '阿提哈德', QTR: '卡塔尔航空', SVA: '沙特航空', KAC: '科威特航空', THY: '土耳其航空', MSR: '埃及航空', ETH: '埃塞俄比亚航空', KQA: '肯尼亚航空', SAA: '南非航空', RAM: '摩洛哥皇家', RWD: '卢旺达航空', BAW: '英国航空', EZY: '易捷航空', VIR: '维珍航空', AFR: '法国航空', TVF: '泛航法国', DLH: '汉莎航空', EWG: '欧洲之翼', SWR: '瑞士航空', AUA: '奥地利航空', KLM: '荷兰皇家', TRA: '泛航荷兰', IBE: '西班牙航空', VLG: '伏林航空', AEA: '欧罗巴航空', TAP: '葡萄牙航空', ITY: 'ITA 航空', SAS: '北欧航空', NAX: '挪威航空', RYR: '瑞安航空', EIN: '爱尔兰航空', WZZ: '维兹航空', LOT: '波兰航空', CSA: '捷克航空', AEE: '爱琴海航空', ELL: '波罗的海航空', QFA: '澳洲航空', JST: '捷星航空', ANZ: '新西兰航空', VOZ: '维珍澳洲', ACA: '加拿大航空', WJA: '西捷航空', UAL: '美联航', DAL: '达美航空', AAL: '美国航空', SWA: '西南航空', JBU: '捷蓝航空', ASA: '阿拉斯加航空', FFT: '边疆航空', NKS: '精神航空', HAL: '夏威夷航空', AMX: '墨西哥航空', AVA: '哥伦比亚航空', LAN: '智利航空', GLO: '戈尔航空', AZU: '蓝色巴西', TAM: '巴西天马', ARG: '阿根廷航空', }; function airlineOf(cs) { return AIRLINE[(cs || '').slice(0, 3)] || ''; } const FL = { rows: [], mil: [], trails: {}, t0: 0, timer: null, sel: null }; function initFlights(data) { FL.rows = data.flights || []; FL.mil = data.mil || []; FL.trails = data.trails || {}; FL.t0 = data.generated || Date.now() / 1000; } /* 球面正算:从 (lat,lon) 沿方位角 brg 前进 dist 米 */ function dest(lat, lon, brg, dist) { const R = 6371000, d = dist / R, b = (brg * Math.PI) / 180; const p1 = (lat * Math.PI) / 180, l1 = (lon * Math.PI) / 180; const p2 = Math.asin(Math.sin(p1) * Math.cos(d) + Math.cos(p1) * Math.sin(d) * Math.cos(b)); const l2 = l1 + Math.atan2(Math.sin(b) * Math.sin(d) * Math.cos(p1), Math.cos(d) - Math.sin(p1) * Math.sin(p2)); let lo = (l2 * 180) / Math.PI; if (lo > 180) lo -= 360; if (lo < -180) lo += 360; return [lo, (p2 * 180) / Math.PI]; } /* 已飞航迹:用后端的真实历史点 */ function trailFC() { const now = Date.now() / 1000; const dt = Math.min(Math.max(now - FL.t0, 0), 1800); const feats = []; Object.entries(FL.trails).forEach(([hexid, pts]) => { if (!pts || !pts.length) return; const r = FL.rows.find((x) => x[7] === hexid); if (!r) return; const pos = flightPos(r); if (pts.length >= 2) { feats.push({ type: 'Feature', geometry: { type: 'LineString', coordinates: pts.map((p) => [p[0], p[1]]) }, properties: { h: hexid } }); } else if (r[3] > 30) { // 历史还没攒够 2 点:按航向反推 20 分钟,先给出航迹尾 const back = dest(pos[1], pos[0], (r[4] + 180) % 360, r[3] * 20 * 60); feats.push({ type: 'Feature', geometry: { type: 'LineString', coordinates: [back, pos] }, properties: { h: hexid } }); } }); return { type: 'FeatureCollection', features: feats }; } /* 预测线:从当前位置沿航向按速度外推 45 分钟 */ function forecastFC() { const now = Date.now() / 1000; const dt = Math.min(Math.max(now - FL.t0, 0), 1800); const feats = []; if (!FL.sel) return { type: 'FeatureCollection', features: feats }; // 只给选中的飞机画前向外推线,否则满屏虚线 for (let i = 0; i < FL.rows.length; i += 1) { const r = FL.rows[i]; if (r[7] !== FL.sel || !r[3]) continue; const rad = (r[4] * Math.PI) / 180; const lat = r[1] + (r[3] * Math.cos(rad) * dt) / 111320; const cosLat = Math.cos((lat * Math.PI) / 180) || 0.01; let lon = r[0] + (r[3] * Math.sin(rad) * dt) / (111320 * cosLat); if (lon > 180) lon -= 360; if (lon < -180) lon += 360; const ahead = dest(lat, lon, r[4], r[3] * 60 * 18); feats.push({ type: 'Feature', geometry: { type: 'LineString', coordinates: [[lon, lat], ahead] }, properties: { h: r[7] } }); } return { type: 'FeatureCollection', features: feats }; } function milFC() { const now = Date.now() / 1000; const dt = Math.min(Math.max(now - FL.t0, 0), 1800); return { type: 'FeatureCollection', features: FL.mil.map((r) => { const rad = (r[4] * Math.PI) / 180; const lat = r[1] + (r[3] * Math.cos(rad) * dt) / 111320; const cosLat = Math.cos((lat * Math.PI) / 180) || 0.01; let lon = r[0] + (r[3] * Math.sin(rad) * dt) / (111320 * cosLat); if (lon > 180) lon -= 360; if (lon < -180) lon += 360; return { type: 'Feature', geometry: { type: 'Point', coordinates: [lon, lat] }, properties: { c: r[5], t: r[6], a: r[2], h: r[7] } }; }) }; } /* 两次拉取之间用速度与航向做推算,让目标真的在动而不是每 20 分钟跳一次 */ function flightFC() { const now = Date.now() / 1000; const dt = Math.min(Math.max(now - FL.t0, 0), 1800); const feats = new Array(FL.rows.length); for (let i = 0; i < FL.rows.length; i++) { const r = FL.rows[i]; const rad = (r[4] * Math.PI) / 180; const lat = r[1] + (r[3] * Math.cos(rad) * dt) / 111320; const cosLat = Math.cos((lat * Math.PI) / 180) || 0.01; let lon = r[0] + (r[3] * Math.sin(rad) * dt) / (111320 * cosLat); if (lon > 180) lon -= 360; if (lon < -180) lon += 360; feats[i] = { type: 'Feature', geometry: { type: 'Point', coordinates: [lon, lat] }, properties: { c: r[5], o: r[6], a: r[2], v: r[3], q: r[7], t: r[4], s: i % 3 === 0 ? 1 : 0, i, h: r[7] } }; } return { type: 'FeatureCollection', features: feats }; } function planeIcon() { const size = 40; const cv = document.createElement('canvas'); cv.width = cv.height = size; const g = cv.getContext('2d'); g.translate(size / 2, size / 2); // 机身 + 后掠翼 + 平尾,机头朝上(-y) g.beginPath(); g.moveTo(0, -16); g.quadraticCurveTo(1.9, -13.5, 2.1, -6); g.lineTo(2.1, -2.6); g.lineTo(15, 3.4); g.lineTo(15, 5.4); g.lineTo(2.1, 1.6); g.lineTo(1.9, 6.8); g.lineTo(6.2, 10.2); g.lineTo(6.2, 11.6); g.lineTo(0, 10.2); g.lineTo(-6.2, 11.6); g.lineTo(-6.2, 10.2); g.lineTo(-1.9, 6.8); g.lineTo(-2.1, 1.6); g.lineTo(-15, 5.4); g.lineTo(-15, 3.4); g.lineTo(-2.1, -2.6); g.lineTo(-2.1, -6); g.quadraticCurveTo(-1.9, -13.5, 0, -16); g.closePath(); g.fillStyle = '#e8f6ff'; g.fill(); g.lineWidth = 1.6; g.strokeStyle = 'rgba(6,16,26,.85)'; g.stroke(); return g.getImageData(0, 0, size, size); } function addFlights() { map.addSource('flights', { type: 'geojson', data: flightFC() }); try { map.addImage('plane-icon', planeIcon(), { pixelRatio: 2 }); } catch (e) { /* 忽略重复 */ } map.addLayer({ id: 'fl-air', type: 'symbol', source: 'flights', filter: ['!', ['in', ['get', 'q'], ['literal', ['7700', '7600', '7500']]]], layout: { 'icon-image': 'plane-icon', 'icon-size': ['interpolate', ['linear'], ['zoom'], 1, 0.26, 3, 0.4, 5, 0.58, 7, 0.72], 'icon-rotate': ['get', 't'], 'icon-rotation-alignment': 'map', 'icon-allow-overlap': true, 'icon-ignore-placement': true, }, paint: { 'icon-opacity': ['interpolate', ['linear'], ['zoom'], 1, 0.38, 3, 0.5, 5, 0.62, 7, 0.72] } }); map.addSource('trails', { type: 'geojson', data: trailFC() }); map.addLayer({ id: 'tr-line', type: 'line', source: 'trails', paint: { 'line-color': '#7fd0f5', 'line-width': 0.65, 'line-opacity': 0.26, 'line-blur': 0.2, } }); map.addSource('forecast', { type: 'geojson', data: forecastFC() }); map.addLayer({ id: 'fc-line', type: 'line', source: 'forecast', paint: { 'line-color': '#7fd0f5', 'line-width': 0.65, 'line-opacity': 0.15, 'line-dasharray': [1.6, 2.2], } }); map.addSource('sel', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } }); map.addLayer({ id: 'sel-ring', type: 'circle', source: 'sel', paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, 13, 5, 22, 8, 30], 'circle-color': 'rgba(0,0,0,0)', 'circle-stroke-width': 1.2, 'circle-stroke-color': 'rgba(90,210,255,.85)', 'circle-stroke-opacity': 0.9, } }); map.addLayer({ id: 'sel-ring2', type: 'circle', source: 'sel', paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, 7, 5, 12, 8, 16], 'circle-color': 'rgba(0,0,0,0)', 'circle-stroke-width': 0.7, 'circle-stroke-color': 'rgba(90,210,255,.45)', 'circle-stroke-opacity': 0.8, } }); map.addSource('mil', { type: 'geojson', data: milFC() }); map.addLayer({ id: 'mil-pt', type: 'circle', source: 'mil', paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, 2.6, 4, 4, 7, 6], 'circle-color': 'rgba(255,140,60,.14)', 'circle-stroke-width': 1.1, 'circle-stroke-color': '#ff9c4a', 'circle-stroke-opacity': 0.9, } }); map.addLayer({ id: 'fl-em', type: 'circle', source: 'flights', filter: ['in', ['get', 'q'], ['literal', ['7700', '7600', '7500']]], paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, 3, 5, 6], 'circle-color': '#ff5040', 'circle-opacity': 0.9, 'circle-stroke-width': 1, 'circle-stroke-color': 'rgba(255,179,168,.8)', } }); } function setFlightFilter() { if (!map.getLayer('fl-air')) return; const base = ['!', ['in', ['get', 'q'], ['literal', ['7700', '7600', '7500']]]]; const z = map.getZoom(); ['tr-line', 'fc-line'].forEach((id) => { if (map.getLayer(id)) { map.setLayoutProperty(id, 'visibility', (z > 2.6 && document.getElementById('l-flights').checked) ? 'visible' : 'none'); } }); // 分级采样:全球看分布,区域看密度,近景看全部 if (z < 2.8) map.setFilter('fl-air', ['all', base, ['==', ['get', 's'], 1]]); else if (z < 4.6) map.setFilter('fl-air', ['all', base, ['<', ['%', ['get', 'i'], 2], 1]]); else map.setFilter('fl-air', base); } function startFlights() { map.on('zoomend', setFlightFilter); const tick = () => { const src = map.getSource('flights'); if (!src) return; if (!document.getElementById('l-flights').checked) return; const fc = flightFC(); src.setData(fc); const msrc = map.getSource('mil'); if (msrc) msrc.setData(milFC()); const tsrc = map.getSource('trails'); if (tsrc) tsrc.setData(trailFC()); const fsrc = map.getSource('forecast'); if (fsrc) fsrc.setData(forecastFC()); window.__flt = { n: (window.__flt ? window.__flt.n : 0) + 1, p: fc.features.length ? fc.features[0].geometry.coordinates : null }; }; tick(); if (FL.timer) clearInterval(FL.timer); FL.timer = setInterval(tick, 2500); } const SAT = { items: [], timer: null }; const SATC = { stations: '#eaf4ff', 'gps-ops': '#ffd166', weather: '#6fd3ff', starlink: '#b9e6ff', visual: '#ffd9a8', science: '#a9ffd0' }; function initSats(data) { SAT.items = (data.satellites || []).map((s) => { try { return { n: s.n, g: s.g, rec: satellite.twoline2satrec(s.l1, s.l2) }; } catch (e) { return null; } }).filter(Boolean); } function satFC() { const now = new Date(); const gmst = satellite.gstime(now); const feats = []; SAT.items.forEach((s) => { const pv = satellite.propagate(s.rec, now); if (!pv || !pv.position) return; const g = satellite.eciToGeodetic(pv.position, gmst); const lon = satellite.degreesLong(g.longitude); const lat = satellite.degreesLat(g.latitude); if (!isFinite(lon) || !isFinite(lat)) return; feats.push({ type: 'Feature', geometry: { type: 'Point', coordinates: [lon, lat] }, properties: { n: s.n, g: s.g, h: Math.round(g.height) } }); }); return { type: 'FeatureCollection', features: feats }; } function satFilter() { if (!document.getElementById('l-sat').checked) return ['==', ['get', 'g'], '__off']; const on = ['stations', 'visual', 'science']; if (document.getElementById('l-sat-gps').checked) on.push('gps-ops'); if (document.getElementById('l-sat-wx').checked) on.push('weather'); if (document.getElementById('l-sat-sx').checked) on.push('starlink'); return ['in', ['get', 'g'], ['literal', on]]; } const ISSK = ['case', ['==', ['get', 'n'], 'ISS (ZARYA)'], 1.9, ['==', ['get', 'n'], 'CSS (TIANHE)'], 1.9, 1]; function addSats() { map.addSource('sats', { type: 'geojson', data: { type: 'FeatureCollection', features: [] } }); map.addLayer({ id: 'sat-glow', type: 'circle', source: 'sats', filter: ['!=', ['get', 'g'], 'starlink'], paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, ['*', 4, ISSK], 4, ['*', 9, ISSK], 7, ['*', 15, ISSK]], 'circle-color': ['match', ['get', 'g'], 'stations', SATC.stations, 'gps-ops', SATC['gps-ops'], 'weather', SATC.weather, '#9fb6cc'], 'circle-opacity': 0.14, 'circle-blur': 1.1, } }); map.addLayer({ id: 'sat-starlink', type: 'circle', source: 'sats', filter: ['==', ['get', 'g'], 'starlink'], paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, 0.72, 4, 1.05, 7, 1.5], 'circle-color': SATC.starlink, // 星链是星座结构,低缩放看带、放大后让位给态势 'circle-opacity': ['interpolate', ['linear'], ['zoom'], 1, 0.20, 2.6, 0.18, 3.4, 0.08, 4.2, 0], } }); map.addLayer({ id: 'sat-dot', type: 'circle', source: 'sats', filter: ['!=', ['get', 'g'], 'starlink'], paint: { 'circle-radius': ['interpolate', ['linear'], ['zoom'], 1, ['*', 1.3, ISSK], 4, ['*', 2.4, ISSK], 7, ['*', 3.6, ISSK]], 'circle-color': ['match', ['get', 'g'], 'stations', SATC.stations, 'gps-ops', SATC['gps-ops'], 'weather', SATC.weather, 'visual', SATC.visual, 'science', SATC.science, '#9fb6cc'], 'circle-opacity': 0.95, 'circle-stroke-width': 0.5, 'circle-stroke-color': 'rgba(255,255,255,.55)', } }); } const SAT_TAGS = [['ISS (ZARYA)', 'ISS'], ['CSS (TIANHE)', '天宫']]; const TAG_MK = {}; function startSats() { const tick = () => { const on = document.getElementById('l-sat').checked; Object.values(TAG_MK).forEach((m) => { m.getElement().style.display = on ? '' : 'none'; }); const src = map.getSource('sats'); if (!src || !on) return; const fc = satFC(); src.setData(fc); SAT_TAGS.forEach(([id, tag]) => { const f = fc.features.find((x) => x.properties.n === id); if (!TAG_MK[id]) { if (!f) return; const el = document.createElement('div'); el.className = 'satiss'; el.textContent = tag; TAG_MK[id] = new maplibregl.Marker({ element: el, anchor: 'left', offset: [7, 0] }) .setLngLat(f.geometry.coordinates).addTo(map); } if (f) { TAG_MK[id].setLngLat(f.geometry.coordinates); TAG_MK[id].getElement().style.display = ''; } else TAG_MK[id].getElement().style.display = 'none'; }); }; tick(); if (SAT.timer) clearInterval(SAT.timer); SAT.timer = setInterval(tick, 2000); } function applyBasemode(m) { const night = m === 'night', relief = m === 'relief'; if (map.getLayer('base-night')) map.setLayoutProperty('base-night', 'visibility', night ? 'visible' : 'none'); // 真彩底图有轨道缝隙,垫一层晕渲,缝里露地表而不是黑洞 if (map.getLayer('base-relief')) map.setLayoutProperty('base-relief', 'visibility', relief ? 'visible' : 'none'); if (map.getLayer('ocean')) map.setLayoutProperty('ocean', 'visibility', m === 'plain' ? 'visible' : 'none'); if (map.getLayer('c-fill')) { map.setPaintProperty('c-fill', 'fill-opacity', m === 'plain' ? ['case', ['==', ['get', 'p'], null], 0.95, ['interpolate', ['linear'], ['get', 'p'], 0, 0.34, 1, 0.66]] : ['case', ['==', ['get', 'p'], null], 0.05, ['interpolate', ['linear'], ['get', 'p'], 0, 0, 0.4, 0.02, 0.68, 0.14, 1, 0.30]]); } document.querySelectorAll('#basemode .chip').forEach((c) => c.classList.toggle('on', c.dataset.m === m)); } function addLabels() { LABELS.forEach(([name, lon, lat]) => { const el = document.createElement('div'); el.className = 'cty'; el.textContent = name; const mk = new maplibregl.Marker({ element: el, anchor: 'center' }) .setLngLat([lon, lat]).addTo(map); state.markers.push(mk); }); const style = document.createElement('style'); style.textContent = `.cty{display:none}.cty.on{display:block}.cty2{display:none}.cty2.on{display:block} .cty{font-size:9px;letter-spacing:.16em;color:rgba(150,185,220,.42); text-shadow:0 0 6px rgba(4,7,12,.9);pointer-events:none;white-space:nowrap; font-family:-apple-system,"PingFang SC",sans-serif}`; document.head.appendChild(style); updateLabelOpacity(); } function applyPadding() { const w = window.innerWidth; if (w < 1180) { map.setPadding({ left: 0, right: 0, top: 60, bottom: 10 }); return; } map.setPadding({ left: 286, right: 396, top: 68, bottom: 26 }); } function bindRegions() { $('regions').innerHTML = REGIONS.map(([n]) => `