/** * @module geo/geodesic * @description Surface distance along the WGS84 geodesic between two * points, by the default renderer's own method (`EllipsoidGeodesic`, * Vincenty's inverse formula): the same series in the same order, so the * distance agrees with the renderer's to floating-point noise. Angles in * radians, distances in metres. */ import { WGS84 } from './ellipsoid.js'; const MAJOR = WGS84.radii.x; const MINOR = WGS84.radii.z; const FLATTENING = (MAJOR - MINOR) / MAJOR; const EPSILON12 = 1e-12; /** * Vincenty converges in a handful of steps except between nearly antipodal * points, where it may not converge at all; stop there instead of hanging. */ const MAX_ITERATIONS = 200; /** Vincenty's longitude correction, shared with the direct series. */ export function deltaLambda( sineAlpha, cosineSquaredAlpha, sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint, ) { const f = FLATTENING; const C = (f * cosineSquaredAlpha * (4.0 + f * (4.0 - 3.0 * cosineSquaredAlpha))) / 16.0; return ( (1.0 - C) * f * sineAlpha * (sigma + C * sineSigma * (cosineTwiceSigmaMidpoint + C * cosineSigma * (2.0 * cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint - 1.0))) ); } /** * Vincenty's inverse: the geodesic from (lon1, lat1) to (lon2, lat2), * radians, as its length in metres, its start heading and its u². */ export function geodesicInverse(lon1, lat1, lon2, lat2) { const l = lon2 - lon1; const u1 = Math.atan((1 - FLATTENING) * Math.tan(lat1)); const u2 = Math.atan((1 - FLATTENING) * Math.tan(lat2)); const cosineU1 = Math.cos(u1); const sineU1 = Math.sin(u1); const cosineU2 = Math.cos(u2); const sineU2 = Math.sin(u2); const cc = cosineU1 * cosineU2; const cs = cosineU1 * sineU2; const ss = sineU1 * sineU2; const sc = sineU1 * cosineU2; let lambda = l; let lambdaDot; let cosineLambda; let sineLambda; let sigma; let cosineSigma; let sineSigma; let cosineSquaredAlpha; let cosineTwiceSigmaMidpoint; let iterations = 0; do { cosineLambda = Math.cos(lambda); sineLambda = Math.sin(lambda); const temp = cs - sc * cosineLambda; sineSigma = Math.sqrt( cosineU2 * cosineU2 * sineLambda * sineLambda + temp * temp, ); cosineSigma = ss + cc * cosineLambda; sigma = Math.atan2(sineSigma, cosineSigma); let sineAlpha; if (sineSigma === 0.0) { sineAlpha = 0.0; cosineSquaredAlpha = 1.0; } else { sineAlpha = (cc * sineLambda) / sineSigma; cosineSquaredAlpha = 1.0 - sineAlpha * sineAlpha; } lambdaDot = lambda; cosineTwiceSigmaMidpoint = cosineSigma - (2.0 * ss) / cosineSquaredAlpha; if (!isFinite(cosineTwiceSigmaMidpoint)) cosineTwiceSigmaMidpoint = 0.0; lambda = l + deltaLambda( sineAlpha, cosineSquaredAlpha, sigma, sineSigma, cosineSigma, cosineTwiceSigmaMidpoint, ); } while ( Math.abs(lambda - lambdaDot) > EPSILON12 && ++iterations < MAX_ITERATIONS ); const uSquared = (cosineSquaredAlpha * (MAJOR * MAJOR - MINOR * MINOR)) / (MINOR * MINOR); const A = 1.0 + (uSquared * (4096.0 + uSquared * (uSquared * (320.0 - 175.0 * uSquared) - 768.0))) / 16384.0; const B = (uSquared * (256.0 + uSquared * (uSquared * (74.0 - 47.0 * uSquared) - 128.0))) / 1024.0; const cosineSquaredTwiceSigmaMidpoint = cosineTwiceSigmaMidpoint * cosineTwiceSigmaMidpoint; const deltaSigma = B * sineSigma * (cosineTwiceSigmaMidpoint + (B * (cosineSigma * (2.0 * cosineSquaredTwiceSigmaMidpoint - 1.0) - (B * cosineTwiceSigmaMidpoint * (4.0 * sineSigma * sineSigma - 3.0) * (4.0 * cosineSquaredTwiceSigmaMidpoint - 3.0)) / 6.0)) / 4.0); return { distance: MINOR * A * (sigma - deltaSigma), startHeading: Math.atan2(cosineU2 * sineLambda, cs - sc * cosineLambda), uSquared, }; } /** * Geodesic surface distance between two points; heights are ignored. * @param {{lon: number, lat: number}} start Radians. * @param {{lon: number, lat: number}} end Radians. * @returns {number} Metres. */ export function surfaceDistance(start, end) { return geodesicInverse(start.lon, start.lat, end.lon, end.lat).distance; } /** * The WGS84 geodesic from (lon1, lat1) to (lon2, lat2), radians, solved once * as the default renderer's `EllipsoidGeodesic` solves it: its length in * metres, its start heading, and `at(fraction)`, the longitude and latitude * (radians) that fraction of the way along it, by the series the renderer * interpolates with, term for term. The longitude is not wrapped, as the * renderer's is not. */ export function geodesicLine(lon1, lat1, lon2, lat2) { const { distance, startHeading, uSquared } = geodesicInverse( lon1, lat1, lon2, lat2, ); const cosH = Math.cos(startHeading); const sinH = Math.sin(startHeading); const tanU = (1 - FLATTENING) * Math.tan(lat1); const cosU = 1 / Math.sqrt(1 + tanU * tanU); const sinU = cosU * tanU; const sigma1 = Math.atan2(tanU, cosH); const sineAlpha = cosU * sinH; const cos2Alpha = 1 - sineAlpha * sineAlpha; const cosAlpha = Math.sqrt(cos2Alpha); const u2 = uSquared / 4; const u4 = u2 * u2; const u6 = u4 * u2; const u8 = u4 * u4; const a0 = 1 + u2 - (3 * u4) / 4 + (5 * u6) / 4 - (175 * u8) / 64; const a1 = 1 - u2 + (15 * u4) / 8 - (35 * u6) / 8; const a2 = 1 - 3 * u2 + (35 * u4) / 4; const a3 = 1 - 5 * u2; const distanceRatio = a0 * sigma1 - (a1 * Math.sin(2 * sigma1) * u2) / 2 - (a2 * Math.sin(4 * sigma1) * u4) / 16 - (a3 * Math.sin(6 * sigma1) * u6) / 48 - (Math.sin(8 * sigma1) * 5 * u8) / 512; return { distance, startHeading, at(fraction) { const s = distanceRatio + (distance * fraction) / MINOR; const cos2S = Math.cos(2 * s); const cos4S = Math.cos(4 * s); const cos6S = Math.cos(6 * s); const sin2S = Math.sin(2 * s); const sin4S = Math.sin(4 * s); const sin6S = Math.sin(6 * s); const sin8S = Math.sin(8 * s); const s2 = s * s; const s3 = s * s2; let sigma = (2 * s3 * u8 * cos2S) / 3 + s * (1 - u2 + (7 * u4) / 4 - (15 * u6) / 4 + (579 * u8) / 64 - (u4 - (15 * u6) / 4 + (187 * u8) / 16) * cos2S - ((5 * u6) / 4 - (115 * u8) / 16) * cos4S - (29 * u8 * cos6S) / 16) + (u2 / 2 - u4 + (71 * u6) / 32 - (85 * u8) / 16) * sin2S + ((5 * u4) / 16 - (5 * u6) / 4 + (383 * u8) / 96) * sin4S - s2 * ((u6 - (11 * u8) / 2) * sin2S + (5 * u8 * sin4S) / 2) + ((29 * u6) / 96 - (29 * u8) / 16) * sin6S + (539 * u8 * sin8S) / 1536; const theta = Math.asin(Math.sin(sigma) * cosAlpha); const lat = Math.atan((MAJOR / MINOR) * Math.tan(theta)); sigma -= sigma1; const cos2Sm = Math.cos(2 * sigma1 + sigma); const sinSigma = Math.sin(sigma); const cosSigma = Math.cos(sigma); const lambda = Math.atan2( sinSigma * sinH, cosU * cosSigma - sinU * sinSigma * cosH, ); const l = lambda - deltaLambda(sineAlpha, cos2Alpha, sigma, sinSigma, cosSigma, cos2Sm); return { lon: lon1 + l, lat }; }, }; } /** `geodesicLine(lon1, lat1, lon2, lat2).at(fraction)`. */ export function geodesicAt(lon1, lat1, lon2, lat2, fraction) { return geodesicLine(lon1, lat1, lon2, lat2).at(fraction); }