diff --git a/changelog.d/808-cctv-focus-nearest-point.md b/changelog.d/808-cctv-focus-nearest-point.md new file mode 100644 index 0000000000..1c9a691a12 --- /dev/null +++ b/changelog.d/808-cctv-focus-nearest-point.md @@ -0,0 +1,3 @@ +- Add a CCTV control method to select the catalog camera nearest an explicit + WGS84 point, optionally flying to it. Viewer-nearest navigation uses the same + coordinate lookup (Kevin Rajan, #808). diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index e03f5fd243..879c2135fb 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2867,6 +2867,10 @@ while `_clearLayersOutsideContextMode` switches the layer off): the user switching CCTV off inside Contacts closes the panel as anywhere else. A panel opened by hand while the layer is off stays open, and its meta line reads the catalog count, not the last active camera. +The CCTV control `focusNearestToPoint(lat, lon, options)` selects the catalog +camera nearest a WGS84 point and flies to it unless `focus: false`. No UI action +calls it yet. + ## UI disposal UI disposal releases the CCTV subscription, command-dock observer, legacy drag diff --git a/scripts/format-scope.json b/scripts/format-scope.json index f3567a7066..aa51f28c05 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -614,6 +614,7 @@ "src/layers/cctv/ingestion.js", "src/layers/cctv/hover.js", "src/layers/cctv/navigation.js", + "src/layers/cctv/navigation.test.mjs", "src/layers/cctv/index.js", "src/layers/cctv/model.js", "src/layers/cctv/geometryQueue.js", diff --git a/src/layers/cctv/controls.js b/src/layers/cctv/controls.js index 1f83b7468a..5daa130e75 100644 --- a/src/layers/cctv/controls.js +++ b/src/layers/cctv/controls.js @@ -335,6 +335,25 @@ export function createControls({ state: layerState, services, parts, source }) { return nextId; }, + /** + * Selects the camera nearest a WGS84 point and optionally flies to it. + * @param {number} lat Latitude in degrees. + * @param {number} lon Longitude in degrees. + * @param {Object} [options={}] + * @param {boolean} [options.focus=true] Whether to fly after selection. + * @param {number} [options.durationSec] Fly-to duration in seconds. + * @returns {string|null} Camera ID, or null (invalid point, empty catalog). + */ + focusNearestToPoint(lat, lon, options = {}) { + const nearest = parts.navigation.nearestCameraIdToPoint(lat, lon); + if (!nearest) return null; + parts.selection.setActiveCamera(nearest); + if (options.focus !== false) { + parts.navigation.focusCamera(nearest, options.durationSec || 1.8); + } + return nearest; + }, + /** * Selects and flies to the camera nearest the current viewer position. * @param {Object} [options={}] diff --git a/src/layers/cctv/navigation.js b/src/layers/cctv/navigation.js index eaa9a888fc..6b2ba96aaf 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -9,14 +9,20 @@ export function createNavigation({ source, }) { /** - * Finds the camera closest to the viewer's current position. - * @returns {string|null} Camera ID of the nearest camera, or null. + * Nearest catalog camera to a WGS84 point. + * @param {number} lat Latitude in degrees. + * @param {number} lon Longitude in degrees. + * @returns {string|null} Camera ID, or null (invalid point, empty catalog). */ - - function nearestCameraIdToViewer() { - const pose = parts.model.eyePose(); - if (!pose || !layerState._records.length) return null; - const { lat, lon } = pose; + function nearestCameraIdToPoint(lat, lon) { + if ( + !Number.isFinite(lat) || + !Number.isFinite(lon) || + Math.abs(lat) > 90 || + Math.abs(lon) > 180 || + !layerState._records.length + ) + return null; let best = null; for (const record of layerState._records) { @@ -33,6 +39,16 @@ export function createNavigation({ return best?.id || null; } + /** + * Finds the camera closest to the viewer's current position. + * @returns {string|null} Camera ID of the nearest camera, or null. + */ + function nearestCameraIdToViewer() { + const pose = parts.model.eyePose(); + if (!pose) return null; + return nearestCameraIdToPoint(pose.lat, pose.lon); + } + /** * The camera the layer opens on when none is selected: the one nearest the * viewer, or the first record when the viewer has no position yet. @@ -175,6 +191,7 @@ export function createNavigation({ return (((Math.floor(currentIdx) + delta) % total) + total) % total; } return { + nearestCameraIdToPoint, nearestCameraIdToViewer, openingCameraId, reopenNearestCamera, diff --git a/src/layers/cctv/navigation.test.mjs b/src/layers/cctv/navigation.test.mjs new file mode 100644 index 0000000000..486f897843 --- /dev/null +++ b/src/layers/cctv/navigation.test.mjs @@ -0,0 +1,53 @@ +import assert from 'node:assert/strict'; +import test from 'node:test'; + +import { createNavigation } from './navigation.js'; + +function fixture() { + const records = [ + { camera: { id: 'west', lat: 40, lon: -120 } }, + { camera: { id: 'east', lat: 40, lon: -100 } }, + ]; + const state = { _records: records }; + const parts = { + model: { + eyePose: () => ({ lat: 40, lon: -101 }), + haversineKm(lat1, lon1, lat2, lon2) { + return Math.hypot(lat1 - lat2, lon1 - lon2); + }, + }, + }; + const navigation = createNavigation({ + state, + services: {}, + parts, + source: {}, + }); + return { navigation }; +} + +test('nearest camera can be resolved from an explicit WGS84 target', () => { + const { navigation } = fixture(); + assert.equal(navigation.nearestCameraIdToPoint(40, -119), 'west'); + assert.equal(navigation.nearestCameraIdToPoint(40, -101), 'east'); +}); + +test('viewer-nearest delegates to the same target-point owner', () => { + const { navigation } = fixture(); + assert.equal(navigation.nearestCameraIdToViewer(), 'east'); +}); + +test('invalid target coordinates fail closed', () => { + const { navigation } = fixture(); + for (const [lat, lon] of [ + [NaN, 0], + [null, 0], + [0, Infinity], + [90.00001, 0], + [-90.00001, 0], + [0, 180.00001], + [0, -180.00001], + ]) { + assert.equal(navigation.nearestCameraIdToPoint(lat, lon), null); + } +});