fix: bearing maths
All checks were successful
Auto Tag on Merge to Main / auto-tag (push) Successful in 4s
CI - Lint and Build / lint-backend (push) Successful in 18s
CI - Lint and Build / lint-frontend (push) Successful in 13s
CI - Lint and Build / build-frontend (push) Successful in 15s
CI - Lint and Build / docker-build-test (push) Successful in 38s
All checks were successful
Auto Tag on Merge to Main / auto-tag (push) Successful in 4s
CI - Lint and Build / lint-backend (push) Successful in 18s
CI - Lint and Build / lint-frontend (push) Successful in 13s
CI - Lint and Build / build-frontend (push) Successful in 15s
CI - Lint and Build / docker-build-test (push) Successful in 38s
This commit is contained in:
@@ -68,8 +68,8 @@ function App() {
|
||||
{activeView === 'dashboard'
|
||||
? 'Dashboard de Trenes'
|
||||
: isTimelineMode
|
||||
? 'Reproduccion Historica - Espana'
|
||||
: 'Trenes en Tiempo Real - Espana'}
|
||||
? 'Reproduccion Historica - España'
|
||||
: 'Trenes en Tiempo Real - España'}
|
||||
</span>
|
||||
</div>
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@ import { calculateBearing, calculateDistance, MIN_DISTANCE_FOR_BEARING } from '.
|
||||
const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
||||
|
||||
/**
|
||||
* Pre-calculate bearings for historical data based on consecutive positions
|
||||
* Pre-calculate bearings for historical data based on the last different position
|
||||
* Searches backwards to find a position with significant distance for accurate bearing
|
||||
* @param {Array} positions - Array of positions sorted by timestamp ASC
|
||||
* @returns {Array} - Positions with calculated bearings
|
||||
*/
|
||||
@@ -26,19 +27,43 @@ function calculateHistoricalBearings(positions) {
|
||||
// Sort by timestamp (should already be sorted, but ensure)
|
||||
trainPos.sort((a, b) => new Date(a.timestamp) - new Date(b.timestamp));
|
||||
|
||||
let lastSignificantPos = null; // Track last position with significant movement
|
||||
let lastBearing = null; // Track last calculated bearing
|
||||
|
||||
for (let i = 0; i < trainPos.length; i++) {
|
||||
const current = trainPos[i];
|
||||
let bearing = current.bearing; // Use existing bearing if available
|
||||
|
||||
if (bearing === null || bearing === undefined) {
|
||||
// Calculate bearing from previous position
|
||||
if (i > 0) {
|
||||
const prev = trainPos[i - 1];
|
||||
const distance = calculateDistance(prev.latitude, prev.longitude, current.latitude, current.longitude);
|
||||
// Search backwards for the last position with significant distance
|
||||
if (lastSignificantPos) {
|
||||
const distance = calculateDistance(
|
||||
lastSignificantPos.latitude,
|
||||
lastSignificantPos.longitude,
|
||||
current.latitude,
|
||||
current.longitude
|
||||
);
|
||||
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
||||
bearing = calculateBearing(prev.latitude, prev.longitude, current.latitude, current.longitude);
|
||||
bearing = calculateBearing(
|
||||
lastSignificantPos.latitude,
|
||||
lastSignificantPos.longitude,
|
||||
current.latitude,
|
||||
current.longitude
|
||||
);
|
||||
lastSignificantPos = current;
|
||||
lastBearing = bearing;
|
||||
} else {
|
||||
// No significant movement, keep the last known bearing
|
||||
bearing = lastBearing;
|
||||
}
|
||||
} else {
|
||||
// First position for this train
|
||||
lastSignificantPos = current;
|
||||
}
|
||||
} else {
|
||||
// Has bearing from API, update tracking
|
||||
lastSignificantPos = current;
|
||||
lastBearing = bearing;
|
||||
}
|
||||
|
||||
result.push({
|
||||
|
||||
@@ -6,28 +6,31 @@ const API_URL = import.meta.env.VITE_API_URL || 'http://localhost:3000';
|
||||
const WS_URL = import.meta.env.VITE_WS_URL || 'http://localhost:3000';
|
||||
|
||||
// Calculate bearing for trains based on previous positions
|
||||
// Only updates the stored position when there's significant movement
|
||||
function addCalculatedBearings(newTrains, previousPositions) {
|
||||
return newTrains.map(train => {
|
||||
// If train already has bearing from API, use it
|
||||
if (train.bearing !== null && train.bearing !== undefined) {
|
||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude });
|
||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: train.bearing });
|
||||
return train;
|
||||
}
|
||||
|
||||
const prevPos = previousPositions.get(train.train_id);
|
||||
let calculatedBearing = null;
|
||||
let calculatedBearing = prevPos?.bearing ?? null;
|
||||
|
||||
if (prevPos) {
|
||||
const distance = calculateDistance(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
||||
// Only calculate bearing if the train moved enough
|
||||
// Only calculate bearing and update position if the train moved enough
|
||||
if (distance >= MIN_DISTANCE_FOR_BEARING) {
|
||||
calculatedBearing = calculateBearing(prevPos.lat, prevPos.lon, train.latitude, train.longitude);
|
||||
// Only update stored position when there's significant movement
|
||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: calculatedBearing });
|
||||
}
|
||||
} else {
|
||||
// First time seeing this train, store position without bearing
|
||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude, bearing: null });
|
||||
}
|
||||
|
||||
// Update previous position
|
||||
previousPositions.set(train.train_id, { lat: train.latitude, lon: train.longitude });
|
||||
|
||||
return {
|
||||
...train,
|
||||
bearing: calculatedBearing,
|
||||
|
||||
Reference in New Issue
Block a user