Files
gods-eye-view/src/data/osmOverlays.js
T
Sameh Khamis 2e91579f8f Add a globe visibility getter to the renderer contract
The globe port could show or hide the globe surface but not say whether it
is shown. It now has an optional isGlobeVisible(), gated by a new
globeVisibility capability, in the Cesium adapter, the stub and a
conformance check. A Cesium scene without a globe does not offer it, so
callers keep their own default for an unknown surface.

The OSM overlays, the CCTV ground regime and the submarine cable drape now
read the getter instead of the scene's globe. cableClassificationTypeForScene
takes the viewer rather than its scene.
2026-11-07 19:44:15 -08:00

171 lines
5.6 KiB
JavaScript

import { NO_IMAGERY_HOST } from '../maps/imageryHost.js';
// The application's renderer gives the globe port; a viewer with no
// renderer bound gets one from the factory its Cesium side loads.
import { findGlobe } from '../render/rendererOf.js';
/**
* @file Open-data raster overlays: OpenSeaMap sea marks and OpenSnowMap ski
* pistes (#10). Each overlay is its own layer with its own row under
* Infrastructure; both are built from the descriptors below.
*
* OpenRailwayMap and OpenInfraMap, also named in #10, are deliberately not
* included:
* - OpenRailwayMap's public tile server (tiles.openrailwaymap.org) only
* serves requests whose Referer is openrailwaymap.org; every other Referer,
* including none, gets 403. A tab on this app sends its own origin, so the
* tiles would fail, and embedding them would run against their tile-usage
* policy either way.
* - OpenInfraMap serves MapLibre vector tiles, not raster XYZ tiles. That
* needs a vector-tile rendering pipeline, not an imagery-layer drop-in.
*
* Each overlay is an imagery layer of the renderer's globe port, draped on
* whatever is drawn: the globe on the OSM and Bing stacks, the Google
* Photorealistic tiles on Google 3D. The map stack's own record says which
* (`getDrapeSurface`); only a stack with neither leaves them hidden, and the
* row says so.
* @module data/osmOverlays
*/
/** @type {ReadonlyArray<{id:string,name:string,icon:string,source:string,urlTemplate:string,credit:string,maximumLevel:number}>} */
export const OSM_OVERLAYS = Object.freeze([
Object.freeze({
id: 'sea-marks',
name: 'Sea Marks',
icon: '⚓',
source: 'OpenSeaMap',
urlTemplate: 'https://tiles.openseamap.org/seamark/{z}/{x}/{y}.png',
// Chart tiles CC BY-SA 2.0; the underlying data is OpenStreetMap (ODbL).
credit:
'Sea marks © OpenSeaMap contributors (CC BY-SA 2.0) · Data © OpenStreetMap contributors (ODbL)',
maximumLevel: 18,
}),
Object.freeze({
id: 'ski-pistes',
name: 'Ski Pistes',
icon: '⛷',
source: 'OpenSnowMap',
urlTemplate: 'https://www.opensnowmap.org/pistes/{z}/{x}/{y}.png',
// OpenSnowMap asks for "(c)www.opensnowmap.org" wherever its tiles appear.
credit:
'Ski pistes © www.opensnowmap.org (CC BY-SA) · Data © OpenStreetMap contributors (ODbL)',
maximumLevel: 18,
}),
]);
/** Why an enabled overlay is showing nothing, or null. */
export function osmOverlayHiddenReason(enabled, hostKind) {
return enabled && hostKind === 'none' ? NO_IMAGERY_HOST : null;
}
/** The imagery spec of one overlay. */
export function osmOverlaySpec(overlay) {
return {
kind: 'xyz',
url: overlay.urlTemplate,
maxLevel: overlay.maximumLevel,
credit: { text: overlay.credit },
};
}
/**
* Create the layer for one overlay descriptor. Each application catalog gets
* its own instances; `globeOf` (the viewer's globe port) is injectable so
* tests run without WebGL.
*/
export function createOsmOverlayLayer(overlay, { globeOf = findGlobe } = {}) {
if (!OSM_OVERLAYS.includes(overlay))
throw new TypeError(`Unknown open map overlay: ${overlay?.id}`);
let viewer = null;
let mapStack = null;
let unsubscribeMap = null;
let enabled = false;
let surface = 'none';
/** The imagery layer this instance owns while enabled. */
let owned = null;
/**
* What the overlay drapes on: the map stack's record, or with no map
* stack, whether the renderer shows the globe.
*/
function drapeSurface() {
const recorded = mapStack?.getDrapeSurface?.();
if (recorded) return recorded;
return globeOf(viewer)?.isGlobeVisible?.() === true ? 'globe' : 'none';
}
/** Add or remove the owned imagery layer to match the state. */
function sync() {
if (!viewer) return;
surface = enabled ? drapeSurface() : 'none';
if (!enabled) {
owned?.remove();
owned = null;
} else if (!owned) {
// Added last, so it draws above the map stack's base imagery.
owned = globeOf(viewer)?.imagery.add(osmOverlaySpec(overlay)) ?? null;
}
}
return {
id: overlay.id,
name: overlay.name,
icon: overlay.icon,
source: overlay.source,
updateInterval: 0,
init(nextViewer) {
viewer = nextViewer;
},
/** Follow map-stack switches, for the row's hidden reason. */
attachMapStackController(controller) {
unsubscribeMap?.();
unsubscribeMap = null;
mapStack = controller || null;
const off = controller?.subscribe?.(() => {
if (enabled) sync();
});
unsubscribeMap = typeof off === 'function' ? off : null;
},
enable() {
enabled = true;
sync();
},
disable() {
enabled = false;
sync();
},
update() {
return true;
},
getStats() {
// Without a controller subscription the stats poll doubles as the
// map-stack watch.
if (enabled && !unsubscribeMap) surface = drapeSurface();
// No globe port yet (a viewer whose renderer came later): try again.
if (enabled && !owned) sync();
return {
count: enabled ? 1 : 0,
error: osmOverlayHiddenReason(enabled, surface),
};
},
/** Owned layer count and drape surface, for tests and live gates. */
diagnostics() {
return { host: surface, ownedCount: owned ? 1 : 0 };
},
destroy() {
enabled = false;
owned?.remove();
owned = null;
unsubscribeMap?.();
unsubscribeMap = null;
mapStack = null;
viewer = null;
surface = 'none';
},
};
}
/** One layer per overlay, in panel order. */
export function createOsmOverlayLayers(options) {
return OSM_OVERLAYS.map((overlay) => createOsmOverlayLayer(overlay, options));
}