90 lines
3.4 KiB
JavaScript
90 lines
3.4 KiB
JavaScript
(() => {
|
|
'use strict';
|
|
|
|
const MAX_LINK_GAP_MS = 30000;
|
|
const BASE_JUMP_M = 260;
|
|
const MAX_SPEED_MPS = 120;
|
|
const TO_RAD = Math.PI / 180;
|
|
|
|
function registrationColors(registered) {
|
|
if (registered === true) return {fill:'#26ad72', glow:'rgba(38,210,133,.82)'};
|
|
if (registered === false) return {fill:'#ec4361', glow:'rgba(255,64,96,.86)'};
|
|
return {fill:'#637c91', glow:'rgba(128,158,180,.45)'};
|
|
}
|
|
|
|
function createMarker({heading = 0, fill = 'rgba(255,211,90,.96)', glow = 'rgba(255,211,90,.86)', opacity = 1} = {}) {
|
|
const badge = document.createElement('div');
|
|
badge.className = 'rid-drone-marker';
|
|
badge.setAttribute('aria-hidden', 'true');
|
|
badge.style.setProperty('--rid-drone-fill', fill);
|
|
badge.style.setProperty('--rid-drone-glow', glow);
|
|
badge.style.opacity = opacity;
|
|
const plane = document.createElement('span');
|
|
plane.className = 'rid-drone-plane';
|
|
plane.style.transform = 'rotate(' + heading + 'deg)';
|
|
badge.append(plane);
|
|
return badge;
|
|
}
|
|
|
|
function validPosition(point) {
|
|
return Number.isFinite(point?.longitude) && Math.abs(point.longitude) <= 180
|
|
&& Number.isFinite(point?.latitude) && Math.abs(point.latitude) <= 90
|
|
&& Number.isFinite(point?.timeMs);
|
|
}
|
|
|
|
function jumping(from, to) {
|
|
const dy = Math.sin((to.latitude - from.latitude) * TO_RAD / 2);
|
|
const dx = Math.sin((to.longitude - from.longitude) * TO_RAD / 2);
|
|
const latitude = Math.cos(from.latitude * TO_RAD) * Math.cos(to.latitude * TO_RAD);
|
|
const metres = 12742000 * Math.asin(Math.sqrt(Math.min(1, dy * dy + latitude * dx * dx)));
|
|
const seconds = Math.max(0.5, (to.timeMs - from.timeMs) / 1000);
|
|
return metres > BASE_JUMP_M + MAX_SPEED_MPS * seconds;
|
|
}
|
|
|
|
function shortInterval(from, to) {
|
|
const elapsed = to.timeMs - from.timeMs;
|
|
return elapsed >= 0 && elapsed < MAX_LINK_GAP_MS;
|
|
}
|
|
|
|
function trackHeading(fromLat, fromLng, toLat, toLng) {
|
|
const east = (((toLng - fromLng + 540) % 360) - 180) * Math.cos((fromLat + toLat) * TO_RAD / 2);
|
|
const north = toLat - fromLat;
|
|
return east || north ? (Math.atan2(east, north) / TO_RAD + 360) % 360 : null;
|
|
}
|
|
|
|
function prepareTrack(samples) {
|
|
const points = [];
|
|
let pendingBreak = false;
|
|
let skipped = 0;
|
|
let newSegments = 0;
|
|
for (let index = 0; index < samples.length; index += 1) {
|
|
const point = samples[index];
|
|
if (!validPosition(point)) {
|
|
pendingBreak = true;
|
|
skipped += 1;
|
|
continue;
|
|
}
|
|
|
|
const previous = points.at(-1);
|
|
const next = samples[index + 1];
|
|
const connectedBefore = previous && !point.segmentStart && !pendingBreak && shortInterval(previous, point);
|
|
// Discard an isolated out-and-back spike only when the adjacent run is still continuous.
|
|
if (connectedBefore && validPosition(next) && !next.segmentStart && shortInterval(previous, next)
|
|
&& shortInterval(point, next) && jumping(previous, point) && jumping(point, next) && !jumping(previous, next)) {
|
|
skipped += 1;
|
|
continue;
|
|
}
|
|
if (previous && !point.segmentStart && (!connectedBefore || jumping(previous, point))) {
|
|
points.push({...point, segmentStart: true});
|
|
newSegments += 1;
|
|
} else {
|
|
points.push(point);
|
|
}
|
|
pendingBreak = false;
|
|
}
|
|
return {points, skipped, newSegments};
|
|
}
|
|
|
|
window.ridDroneMap = Object.freeze({createMarker, prepareTrack, trackHeading, registrationColors, MAX_LINK_GAP_MS});
|
|
})();
|