From 2863bd89511ca08936d4da905a3d56a63b333168 Mon Sep 17 00:00:00 2001 From: Kevin Rajan <7121943+kvnloo@users.noreply.github.com> Date: Tue, 29 Sep 2026 01:27:27 -0500 Subject: [PATCH 1/4] feat(cctv): focus nearest camera to an explicit point --- scripts/format-scope.json | 1 + src/layers/cctv/controls.js | 19 +++++++++ src/layers/cctv/navigation.js | 41 ++++++++++++++----- src/layers/cctv/navigation.test.mjs | 61 +++++++++++++++++++++++++++++ 4 files changed, 113 insertions(+), 9 deletions(-) create mode 100644 src/layers/cctv/navigation.test.mjs diff --git a/scripts/format-scope.json b/scripts/format-scope.json index cd836a5cb2..2fd5ab2d14 100644 --- a/scripts/format-scope.json +++ b/scripts/format-scope.json @@ -502,6 +502,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 af9f1f4275..e6839cb5f1 100644 --- a/src/layers/cctv/controls.js +++ b/src/layers/cctv/controls.js @@ -336,6 +336,25 @@ export function createControls({ state: layerState, services, parts, source }) { return nextId; }, + /** + * Selects the camera nearest an explicit WGS84 point and optionally flies. + * @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} The nearest camera ID, or null if none found. + */ + 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 6c576bb4f1..2c3bdbcb3f 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -8,21 +8,30 @@ export function createNavigation({ source, }) { /** - * Finds the camera closest to the Cesium viewer's current position. + * Find the catalog camera nearest one WGS84 point. + * @param {number} lat Latitude in degrees. + * @param {number} lon Longitude in degrees. * @returns {string|null} Camera ID of the nearest camera, or null. */ - - function nearestCameraIdToViewer() { - const carto = layerState._viewer?.camera?.positionCartographic; - if (!carto || !layerState._records.length) return null; - const lat = Cesium.Math.toDegrees(carto.latitude); - const lon = Cesium.Math.toDegrees(carto.longitude); + function nearestCameraIdToPoint(lat, lon) { + const latitude = Number(lat); + const longitude = Number(lon); + if ( + !Number.isFinite(latitude) || + !Number.isFinite(longitude) || + latitude < -90 || + latitude > 90 || + longitude < -180 || + longitude > 180 || + !layerState._records.length + ) + return null; let best = null; for (const record of layerState._records) { const distKm = parts.model.haversineKm( - lat, - lon, + latitude, + longitude, record.camera.lat, record.camera.lon, ); @@ -33,6 +42,19 @@ export function createNavigation({ return best?.id || null; } + /** + * Finds the camera closest to the Cesium viewer's current position. + * @returns {string|null} Camera ID of the nearest camera, or null. + */ + function nearestCameraIdToViewer() { + const carto = layerState._viewer?.camera?.positionCartographic; + if (!carto) return null; + return nearestCameraIdToPoint( + Cesium.Math.toDegrees(carto.latitude), + Cesium.Math.toDegrees(carto.longitude), + ); + } + /** * Flies the Cesium viewer camera to frame the specified CCTV camera, * looking along its heading from above. @@ -148,6 +170,7 @@ export function createNavigation({ return (((Math.floor(currentIdx) + delta) % total) + total) % total; } return { + nearestCameraIdToPoint, nearestCameraIdToViewer, focusCctvRecord, focusCamera, diff --git a/src/layers/cctv/navigation.test.mjs b/src/layers/cctv/navigation.test.mjs new file mode 100644 index 0000000000..59ae59fb13 --- /dev/null +++ b/src/layers/cctv/navigation.test.mjs @@ -0,0 +1,61 @@ +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, + _viewer: { + camera: { + positionCartographic: { + latitude: (40 * Math.PI) / 180, + longitude: (-101 * Math.PI) / 180, + }, + }, + }, + }; + const parts = { + model: { + haversineKm(lat1, lon1, lat2, lon2) { + return Math.hypot(lat1 - lat2, lon1 - lon2); + }, + }, + }; + const navigation = createNavigation({ + state, + services: {}, + parts, + source: {}, + }); + return { state, 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], + [0, Infinity], + [90.00001, 0], + [-90.00001, 0], + [0, 180.00001], + [0, -180.00001], + ]) { + assert.equal(navigation.nearestCameraIdToPoint(lat, lon), null); + } +}); From a0f70da1d22751408bae039e7379ce9de9bb853c Mon Sep 17 00:00:00 2001 From: Said Turksever Date: Tue, 3 Nov 2026 16:09:47 +0000 Subject: [PATCH 2/4] docs: complete review bookkeeping for #808 --- changelog.d/808-cctv-focus-nearest-point.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 changelog.d/808-cctv-focus-nearest-point.md 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). From ed586a40a14704b68458dab570ecdb6a4f44bbc0 Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Wed, 18 Nov 2026 16:38:25 +0400 Subject: [PATCH 3/4] Tighten the point-navigation doc comments and state note --- docs/CURRENT-STATE.md | 6 +++--- src/layers/cctv/controls.js | 4 ++-- src/layers/cctv/navigation.js | 4 ++-- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index 9033ccd79c..5bed2b8408 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2867,9 +2867,9 @@ 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 layer control `focusNearestToPoint(lat, lon, options)` selects the -nearest catalog camera to a valid WGS84 point and optionally flies to it. -`focus: false` selects without flying; no current UI action calls this method. +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 diff --git a/src/layers/cctv/controls.js b/src/layers/cctv/controls.js index 980005feec..5daa130e75 100644 --- a/src/layers/cctv/controls.js +++ b/src/layers/cctv/controls.js @@ -336,13 +336,13 @@ export function createControls({ state: layerState, services, parts, source }) { }, /** - * Selects the camera nearest an explicit WGS84 point and optionally flies. + * 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} The nearest camera ID, or null if none found. + * @returns {string|null} Camera ID, or null (invalid point, empty catalog). */ focusNearestToPoint(lat, lon, options = {}) { const nearest = parts.navigation.nearestCameraIdToPoint(lat, lon); diff --git a/src/layers/cctv/navigation.js b/src/layers/cctv/navigation.js index 61f0b489a6..aa5ba463e5 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -9,10 +9,10 @@ export function createNavigation({ source, }) { /** - * Find the catalog camera nearest one WGS84 point. + * Nearest catalog camera to a WGS84 point. * @param {number} lat Latitude in degrees. * @param {number} lon Longitude in degrees. - * @returns {string|null} Camera ID of the nearest camera, or null. + * @returns {string|null} Camera ID, or null (invalid point, empty catalog). */ function nearestCameraIdToPoint(lat, lon) { const latitude = Number(lat); From 93f848d8b1cada24199a9f5c242aca6ddd02fe2b Mon Sep 17 00:00:00 2001 From: German Khokhlov Date: Wed, 18 Nov 2026 16:45:59 +0400 Subject: [PATCH 4/4] Check point coordinates without coercion and drop tests main already covers --- src/layers/cctv/navigation.js | 16 +++++------ src/layers/cctv/navigation.test.mjs | 42 +++-------------------------- 2 files changed, 9 insertions(+), 49 deletions(-) diff --git a/src/layers/cctv/navigation.js b/src/layers/cctv/navigation.js index aa5ba463e5..6b2ba96aaf 100644 --- a/src/layers/cctv/navigation.js +++ b/src/layers/cctv/navigation.js @@ -15,15 +15,11 @@ export function createNavigation({ * @returns {string|null} Camera ID, or null (invalid point, empty catalog). */ function nearestCameraIdToPoint(lat, lon) { - const latitude = Number(lat); - const longitude = Number(lon); if ( - !Number.isFinite(latitude) || - !Number.isFinite(longitude) || - latitude < -90 || - latitude > 90 || - longitude < -180 || - longitude > 180 || + !Number.isFinite(lat) || + !Number.isFinite(lon) || + Math.abs(lat) > 90 || + Math.abs(lon) > 180 || !layerState._records.length ) return null; @@ -31,8 +27,8 @@ export function createNavigation({ let best = null; for (const record of layerState._records) { const distKm = parts.model.haversineKm( - latitude, - longitude, + lat, + lon, record.camera.lat, record.camera.lon, ); diff --git a/src/layers/cctv/navigation.test.mjs b/src/layers/cctv/navigation.test.mjs index dd34b72553..486f897843 100644 --- a/src/layers/cctv/navigation.test.mjs +++ b/src/layers/cctv/navigation.test.mjs @@ -9,30 +9,21 @@ function fixture() { { camera: { id: 'east', lat: 40, lon: -100 } }, ]; const state = { _records: records }; - const eye = { pose: { lat: 40, lon: -101 } }; const parts = { model: { - eyePose: () => eye.pose, + eyePose: () => ({ lat: 40, lon: -101 }), haversineKm(lat1, lon1, lat2, lon2) { return Math.hypot(lat1 - lat2, lon1 - lon2); }, }, }; - const selected = []; - parts.selection = { - setActiveCamera(cameraId) { - selected.push(cameraId); - state._activeCameraId = cameraId; - state._openingCameraProvisional = false; - }, - }; const navigation = createNavigation({ state, services: {}, parts, source: {}, }); - return { state, eye, navigation, selected }; + return { navigation }; } test('nearest camera can be resolved from an explicit WGS84 target', () => { @@ -50,6 +41,7 @@ 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], @@ -59,31 +51,3 @@ test('invalid target coordinates fail closed', () => { assert.equal(navigation.nearestCameraIdToPoint(lat, lon), null); } }); - -test('opening camera keeps viewer-nearest and first-record fallback', () => { - const { state, eye, navigation } = fixture(); - assert.equal(navigation.openingCameraId(), 'east'); - eye.pose = null; - assert.equal(navigation.openingCameraId(), 'west'); - state._records = []; - assert.equal(navigation.openingCameraId(), null); -}); - -test('settled move retargets only a provisional opening camera', () => { - const { state, navigation, selected } = fixture(); - state._enabled = true; - state._openingCameraProvisional = true; - state._activeCameraId = 'west'; - assert.equal(navigation.reopenNearestCamera(), true); - assert.deepEqual(selected, ['east']); - assert.equal(state._activeCameraId, 'east'); - assert.equal(state._openingCameraProvisional, true); - assert.equal(navigation.reopenNearestCamera(), false); - state._activeCameraId = 'west'; - state._openingCameraProvisional = false; - assert.equal(navigation.reopenNearestCamera(), false); - state._openingCameraProvisional = true; - state._enabled = false; - assert.equal(navigation.reopenNearestCamera(), false); - assert.deepEqual(selected, ['east']); -});