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); + } +});