2022-08-07 18:38:22 +02:00
|
|
|
const earthRadius = 6371; // km
|
|
|
|
|
|
|
|
function toRad(val: number) {
|
|
|
|
return val * Math.PI / 180;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function lineLengthInM(start: number[], end: number[]) {
|
2022-08-07 21:12:54 +02:00
|
|
|
let lat1 = start[1]
|
|
|
|
let lat2 = end[1]
|
|
|
|
const lon1 = start[0]
|
|
|
|
const lon2 = end[0]
|
2022-08-07 18:38:22 +02:00
|
|
|
|
|
|
|
let dLat = toRad(lat2 - lat1)
|
|
|
|
let dLon = toRad(lon2 - lon1)
|
|
|
|
lat1 = toRad(lat1);
|
|
|
|
lat2 = toRad(lat2);
|
|
|
|
|
|
|
|
let a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
|
|
|
Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
|
|
|
|
let c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
|
|
|
const value = earthRadius * c * 1000
|
|
|
|
return Math.round(value * 100) / 100
|
|
|
|
}
|
2022-08-15 15:54:18 +02:00
|
|
|
|
|
|
|
export function averageCoords(coords: number[][]): number[] {
|
|
|
|
return [
|
|
|
|
(coords[0][0] + coords[1][0]) / 2,
|
|
|
|
(coords[0][1] + coords[1][1]) / 2
|
|
|
|
]
|
|
|
|
}
|