@johnmorrisdotca/chikyu-earth 0.41.0 → 0.43.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,171 @@
1
+ import type { GeoPoint } from "./types.ts";
2
+ /**
3
+ * WGS84 equatorial radius, metres (NIMA TR8350.2).
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { WGS84_A_M } from "@johnmorrisdotca/chikyu-earth";
8
+ *
9
+ * console.log(WGS84_A_M);
10
+ * ```
11
+ */
12
+ export declare const WGS84_A_M = 6378137;
13
+ /**
14
+ * WGS84 flattening (NIMA TR8350.2).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { WGS84_F } from "@johnmorrisdotca/chikyu-earth";
19
+ *
20
+ * console.log(WGS84_F.toFixed(12));
21
+ * ```
22
+ */
23
+ export declare const WGS84_F: number;
24
+ /**
25
+ * Mean Earth radius, kilometres, the same sphere the city distances use.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { MEAN_RADIUS_KM } from "@johnmorrisdotca/chikyu-earth";
30
+ *
31
+ * console.log(MEAN_RADIUS_KM);
32
+ * ```
33
+ */
34
+ export declare const MEAN_RADIUS_KM = 6371.0088;
35
+ /**
36
+ * The speed a flight-time estimate assumes, in kilometres an hour: a jet's cruise, with no wind, climb or holding.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { FLIGHT_SPEED_KM_H } from "@johnmorrisdotca/chikyu-earth";
41
+ *
42
+ * console.log(FLIGHT_SPEED_KM_H);
43
+ * ```
44
+ */
45
+ export declare const FLIGHT_SPEED_KM_H = 900;
46
+ /**
47
+ * The distance and the bearings between two places.
48
+ *
49
+ * @example
50
+ * ```ts
51
+ * import type { RouteLeg } from "@johnmorrisdotca/chikyu-earth";
52
+ *
53
+ * const none: RouteLeg | null = null;
54
+ * console.log(none);
55
+ * ```
56
+ */
57
+ export interface RouteLeg {
58
+ /** Geodesic distance on WGS84, kilometres. The great-circle distance when the geodesic did not converge. */
59
+ kilometres: number;
60
+ /** Great-circle distance on a sphere of {@link MEAN_RADIUS_KM}, kilometres. */
61
+ greatCircleKm: number;
62
+ /** Degrees clockwise from north, 0 to 360, at the start. */
63
+ initialBearing: number;
64
+ /** Degrees clockwise from north, 0 to 360, at the end. */
65
+ finalBearing: number;
66
+ /** Hours at {@link FLIGHT_SPEED_KM_H}. */
67
+ flightHours: number;
68
+ /** The point halfway along the geodesic (or the great circle, when the geodesic did not converge). */
69
+ midpoint: GeoPoint;
70
+ /** False when Vincenty's formula did not converge (nearly antipodal): the distance is then the great circle. */
71
+ exact: boolean;
72
+ }
73
+ /**
74
+ * The great-circle distance, kilometres, on a sphere of {@link MEAN_RADIUS_KM}. The same angle `trackSteps` uses between two unit vectors.
75
+ *
76
+ * @param from - The start, degrees.
77
+ * @param to - The end, degrees.
78
+ * @returns The distance, or null when a place is not on the Earth.
79
+ *
80
+ * @example
81
+ * ```ts
82
+ * import { greatCircleKm } from "@johnmorrisdotca/chikyu-earth";
83
+ *
84
+ * console.log(greatCircleKm({ lon: 0, lat: 0 }, { lon: 0, lat: 0 }));
85
+ * ```
86
+ */
87
+ export declare function greatCircleKm(from: GeoPoint, to: GeoPoint): number | null;
88
+ /**
89
+ * Distance, bearings, midpoint and a flight-time estimate between two places. The distance quoted is the WGS84 geodesic.
90
+ * The flight time is that distance at {@link FLIGHT_SPEED_KM_H}, an estimate with no wind.
91
+ *
92
+ * @param from - The start, degrees.
93
+ * @param to - The end, degrees.
94
+ * @returns The leg, or null when a place is not on the Earth.
95
+ *
96
+ * @example
97
+ * ```ts
98
+ * import { routeLeg } from "@johnmorrisdotca/chikyu-earth";
99
+ *
100
+ * const leg = routeLeg({ lon: 0, lat: 0 }, { lon: 1, lat: 0 });
101
+ * console.log(leg?.kilometres.toFixed(3), leg?.initialBearing);
102
+ * ```
103
+ */
104
+ export declare function routeLeg(from: GeoPoint, to: GeoPoint): RouteLeg | null;
105
+ /**
106
+ * Points along the geodesic from `from` to `to`, including both ends. `count` is how many (2 when they are the same place, 64 when left out).
107
+ * When the geodesic does not converge, the points are the great circle (`trackSteps`).
108
+ *
109
+ * @param from - The start.
110
+ * @param to - The end.
111
+ * @param count - How many points, at least 2.
112
+ * @returns The points, or an empty list when a place is not on the Earth.
113
+ *
114
+ * @example
115
+ * ```ts
116
+ * import { routePoints } from "@johnmorrisdotca/chikyu-earth";
117
+ *
118
+ * console.log(routePoints({ lon: 0, lat: 0 }, { lon: 1, lat: 0 }, 3).length);
119
+ * ```
120
+ */
121
+ export declare function routePoints(from: GeoPoint, to: GeoPoint, count?: number): GeoPoint[];
122
+ /**
123
+ * The runs of a route after it is split at the date line, so a flat map draws two pieces instead of a line across the world.
124
+ * A run that does not cross is one piece. Uses `splitDateLine`.
125
+ *
126
+ * @param points - Points in order along the route, including the ends.
127
+ * @returns One list of points per piece.
128
+ *
129
+ * @example
130
+ * ```ts
131
+ * import { routeRuns } from "@johnmorrisdotca/chikyu-earth";
132
+ *
133
+ * console.log(routeRuns([{ lon: 179, lat: 0 }, { lon: -179, lat: 0 }]).length);
134
+ * ```
135
+ */
136
+ export declare function routeRuns(points: readonly GeoPoint[]): GeoPoint[][];
137
+ /**
138
+ * Read the `route` attribute: `lon,lat,lon,lat`. Anything else is no route.
139
+ *
140
+ * @param value - The attribute's text, or null.
141
+ * @returns The two places, or null.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import { readRoute } from "@johnmorrisdotca/chikyu-earth";
146
+ *
147
+ * console.log(readRoute("139.69,35.69,-0.13,51.51")?.from.lat);
148
+ * console.log(readRoute("no"));
149
+ * ```
150
+ */
151
+ export declare function readRoute(value: string | null | undefined): {
152
+ from: GeoPoint;
153
+ to: GeoPoint;
154
+ } | null;
155
+ /**
156
+ * The `route` attribute for two places, four decimals.
157
+ *
158
+ * @param route - The two places.
159
+ * @returns `lon,lat,lon,lat`.
160
+ *
161
+ * @example
162
+ * ```ts
163
+ * import { routeText } from "@johnmorrisdotca/chikyu-earth";
164
+ *
165
+ * console.log(routeText({ from: { lon: 1, lat: 2 }, to: { lon: 3, lat: 4 } }));
166
+ * ```
167
+ */
168
+ export declare function routeText(route: {
169
+ from: GeoPoint;
170
+ to: GeoPoint;
171
+ }): string;
package/dist/route.js ADDED
@@ -0,0 +1,315 @@
1
+ import { splitDateLine, trackSteps } from "./paint/math.js";
2
+ /**
3
+ * WGS84 equatorial radius, metres (NIMA TR8350.2).
4
+ *
5
+ * @example
6
+ * ```ts
7
+ * import { WGS84_A_M } from "@johnmorrisdotca/chikyu-earth";
8
+ *
9
+ * console.log(WGS84_A_M);
10
+ * ```
11
+ */
12
+ export const WGS84_A_M = 6_378_137;
13
+ /**
14
+ * WGS84 flattening (NIMA TR8350.2).
15
+ *
16
+ * @example
17
+ * ```ts
18
+ * import { WGS84_F } from "@johnmorrisdotca/chikyu-earth";
19
+ *
20
+ * console.log(WGS84_F.toFixed(12));
21
+ * ```
22
+ */
23
+ export const WGS84_F = 1 / 298.257223563;
24
+ /**
25
+ * Mean Earth radius, kilometres, the same sphere the city distances use.
26
+ *
27
+ * @example
28
+ * ```ts
29
+ * import { MEAN_RADIUS_KM } from "@johnmorrisdotca/chikyu-earth";
30
+ *
31
+ * console.log(MEAN_RADIUS_KM);
32
+ * ```
33
+ */
34
+ export const MEAN_RADIUS_KM = 6371.0088;
35
+ /**
36
+ * The speed a flight-time estimate assumes, in kilometres an hour: a jet's cruise, with no wind, climb or holding.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * import { FLIGHT_SPEED_KM_H } from "@johnmorrisdotca/chikyu-earth";
41
+ *
42
+ * console.log(FLIGHT_SPEED_KM_H);
43
+ * ```
44
+ */
45
+ export const FLIGHT_SPEED_KM_H = 900;
46
+ const RAD = Math.PI / 180;
47
+ const B_M = WGS84_A_M * (1 - WGS84_F);
48
+ const isPlace = (place) => Number.isFinite(place.lon) && Number.isFinite(place.lat) && Math.abs(place.lat) <= 90 && Math.abs(place.lon) <= 180;
49
+ const wrap360 = (degrees) => ((degrees % 360) + 360) % 360;
50
+ const wrap180 = (degrees) => {
51
+ const wrapped = ((((degrees + 180) % 360) + 360) % 360) - 180;
52
+ return wrapped === 0 ? 0 : wrapped;
53
+ };
54
+ const unit = (place) => {
55
+ const lat = place.lat * RAD;
56
+ const lon = place.lon * RAD;
57
+ const c = Math.cos(lat);
58
+ return [c * Math.sin(lon), Math.sin(lat), c * Math.cos(lon)];
59
+ };
60
+ /**
61
+ * The great-circle distance, kilometres, on a sphere of {@link MEAN_RADIUS_KM}. The same angle `trackSteps` uses between two unit vectors.
62
+ *
63
+ * @param from - The start, degrees.
64
+ * @param to - The end, degrees.
65
+ * @returns The distance, or null when a place is not on the Earth.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * import { greatCircleKm } from "@johnmorrisdotca/chikyu-earth";
70
+ *
71
+ * console.log(greatCircleKm({ lon: 0, lat: 0 }, { lon: 0, lat: 0 }));
72
+ * ```
73
+ */
74
+ export function greatCircleKm(from, to) {
75
+ if (!isPlace(from) || !isPlace(to))
76
+ return null;
77
+ const u = unit(from);
78
+ const v = unit(to);
79
+ const dot = Math.max(-1, Math.min(1, u[0] * v[0] + u[1] * v[1] + u[2] * v[2]));
80
+ const cross = Math.hypot(u[1] * v[2] - u[2] * v[1], u[2] * v[0] - u[0] * v[2], u[0] * v[1] - u[1] * v[0]);
81
+ return (MEAN_RADIUS_KM * Math.atan2(cross, dot));
82
+ }
83
+ /** Vincenty's inverse: metres and the two bearings, or null when it does not converge. */
84
+ function vincentyInverse(from, to) {
85
+ const u1 = Math.atan((1 - WGS84_F) * Math.tan(from.lat * RAD));
86
+ const u2 = Math.atan((1 - WGS84_F) * Math.tan(to.lat * RAD));
87
+ const sinU1 = Math.sin(u1);
88
+ const cosU1 = Math.cos(u1);
89
+ const sinU2 = Math.sin(u2);
90
+ const cosU2 = Math.cos(u2);
91
+ const L = Math.atan2(Math.sin((to.lon - from.lon) * RAD), Math.cos((to.lon - from.lon) * RAD));
92
+ let lambda = L;
93
+ let sinSigma = 0;
94
+ let cosSigma = 1;
95
+ let sigma = 0;
96
+ let cos2SigmaM = 0;
97
+ let cosSqAlpha = 1;
98
+ for (let i = 0; i < 100; i += 1) {
99
+ const sinLambda = Math.sin(lambda);
100
+ const cosLambda = Math.cos(lambda);
101
+ sinSigma = Math.hypot(cosU2 * sinLambda, cosU1 * sinU2 - sinU1 * cosU2 * cosLambda);
102
+ if (sinSigma === 0)
103
+ return { metres: 0, initial: 0, final: 0 };
104
+ cosSigma = sinU1 * sinU2 + cosU1 * cosU2 * cosLambda;
105
+ sigma = Math.atan2(sinSigma, cosSigma);
106
+ const sinAlpha = (cosU1 * cosU2 * sinLambda) / sinSigma;
107
+ cosSqAlpha = 1 - sinAlpha * sinAlpha;
108
+ cos2SigmaM = cosSqAlpha === 0 ? 0 : cosSigma - (2 * sinU1 * sinU2) / cosSqAlpha;
109
+ const C = (WGS84_F / 16) * cosSqAlpha * (4 + WGS84_F * (4 - 3 * cosSqAlpha));
110
+ const next = L + (1 - C) * WGS84_F * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
111
+ if (Math.abs(next - lambda) < 1e-12) {
112
+ lambda = next;
113
+ const uSq = (cosSqAlpha * (WGS84_A_M * WGS84_A_M - B_M * B_M)) / (B_M * B_M);
114
+ const A = 1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
115
+ const B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
116
+ const delta = B *
117
+ sinSigma *
118
+ (cos2SigmaM +
119
+ (B / 4) * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - (B / 6) * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
120
+ const metres = B_M * A * (sigma - delta);
121
+ const initial = Math.atan2(cosU2 * Math.sin(lambda), cosU1 * sinU2 - sinU1 * cosU2 * Math.cos(lambda));
122
+ const final = Math.atan2(cosU1 * Math.sin(lambda), -sinU1 * cosU2 + cosU1 * sinU2 * Math.cos(lambda));
123
+ return { metres, initial: wrap360(initial / RAD), final: wrap360(final / RAD) };
124
+ }
125
+ lambda = next;
126
+ }
127
+ return null;
128
+ }
129
+ /** Vincenty's direct: the place `metres` from `from` on the bearing `bearingDeg`. */
130
+ function vincentyDirect(from, bearingDeg, metres) {
131
+ if (metres === 0)
132
+ return { lon: from.lon, lat: from.lat };
133
+ const alpha1 = bearingDeg * RAD;
134
+ const tanU1 = (1 - WGS84_F) * Math.tan(from.lat * RAD);
135
+ const cosU1 = 1 / Math.hypot(1, tanU1);
136
+ const sinU1 = tanU1 * cosU1;
137
+ const sigma1 = Math.atan2(tanU1, Math.cos(alpha1));
138
+ const sinAlpha = cosU1 * Math.sin(alpha1);
139
+ const cosSqAlpha = 1 - sinAlpha * sinAlpha;
140
+ const uSq = (cosSqAlpha * (WGS84_A_M * WGS84_A_M - B_M * B_M)) / (B_M * B_M);
141
+ const A = 1 + (uSq / 16384) * (4096 + uSq * (-768 + uSq * (320 - 175 * uSq)));
142
+ const B = (uSq / 1024) * (256 + uSq * (-128 + uSq * (74 - 47 * uSq)));
143
+ let sigma = metres / (B_M * A);
144
+ let cos2SigmaM = 0;
145
+ for (let i = 0; i < 100; i += 1) {
146
+ cos2SigmaM = Math.cos(2 * sigma1 + sigma);
147
+ const sinSigma = Math.sin(sigma);
148
+ const cosSigma = Math.cos(sigma);
149
+ const delta = B *
150
+ sinSigma *
151
+ (cos2SigmaM +
152
+ (B / 4) * (cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM) - (B / 6) * cos2SigmaM * (-3 + 4 * sinSigma * sinSigma) * (-3 + 4 * cos2SigmaM * cos2SigmaM)));
153
+ const next = metres / (B_M * A) + delta;
154
+ if (Math.abs(next - sigma) < 1e-12) {
155
+ sigma = next;
156
+ break;
157
+ }
158
+ sigma = next;
159
+ }
160
+ const sinSigma = Math.sin(sigma);
161
+ const cosSigma = Math.cos(sigma);
162
+ const lat = Math.atan2(sinU1 * cosSigma + cosU1 * sinSigma * Math.cos(alpha1), (1 - WGS84_F) * Math.hypot(sinAlpha, sinU1 * sinSigma - cosU1 * cosSigma * Math.cos(alpha1)));
163
+ const lambda = Math.atan2(sinSigma * Math.sin(alpha1), cosU1 * cosSigma - sinU1 * sinSigma * Math.cos(alpha1));
164
+ const C = (WGS84_F / 16) * cosSqAlpha * (4 + WGS84_F * (4 - 3 * cosSqAlpha));
165
+ const L = lambda - (1 - C) * WGS84_F * sinAlpha * (sigma + C * sinSigma * (cos2SigmaM + C * cosSigma * (-1 + 2 * cos2SigmaM * cos2SigmaM)));
166
+ return { lon: wrap180(from.lon + L / RAD), lat: lat / RAD };
167
+ }
168
+ /** The place `angle` radians along the great circle that leaves `from` on `bearingDeg`, on a sphere. */
169
+ const sphereDirect = (from, bearingDeg, angle) => {
170
+ const lat1 = from.lat * RAD;
171
+ const bearing = bearingDeg * RAD;
172
+ const lat = Math.asin(Math.max(-1, Math.min(1, Math.sin(lat1) * Math.cos(angle) + Math.cos(lat1) * Math.sin(angle) * Math.cos(bearing))));
173
+ const lon = from.lon * RAD + Math.atan2(Math.sin(bearing) * Math.sin(angle) * Math.cos(lat1), Math.cos(angle) - Math.sin(lat1) * Math.sin(lat));
174
+ return { lon: wrap180(lon / RAD), lat: lat / RAD };
175
+ };
176
+ const sphericalBearings = (from, to) => {
177
+ const y1 = Math.sin((to.lon - from.lon) * RAD) * Math.cos(to.lat * RAD);
178
+ const x1 = Math.cos(from.lat * RAD) * Math.sin(to.lat * RAD) - Math.sin(from.lat * RAD) * Math.cos(to.lat * RAD) * Math.cos((to.lon - from.lon) * RAD);
179
+ const y2 = Math.sin((from.lon - to.lon) * RAD) * Math.cos(from.lat * RAD);
180
+ const x2 = Math.cos(to.lat * RAD) * Math.sin(from.lat * RAD) - Math.sin(to.lat * RAD) * Math.cos(from.lat * RAD) * Math.cos((from.lon - to.lon) * RAD);
181
+ return { initial: wrap360(Math.atan2(y1, x1) / RAD), final: wrap360((Math.atan2(y2, x2) / RAD + 180) % 360) };
182
+ };
183
+ /**
184
+ * Distance, bearings, midpoint and a flight-time estimate between two places. The distance quoted is the WGS84 geodesic.
185
+ * The flight time is that distance at {@link FLIGHT_SPEED_KM_H}, an estimate with no wind.
186
+ *
187
+ * @param from - The start, degrees.
188
+ * @param to - The end, degrees.
189
+ * @returns The leg, or null when a place is not on the Earth.
190
+ *
191
+ * @example
192
+ * ```ts
193
+ * import { routeLeg } from "@johnmorrisdotca/chikyu-earth";
194
+ *
195
+ * const leg = routeLeg({ lon: 0, lat: 0 }, { lon: 1, lat: 0 });
196
+ * console.log(leg?.kilometres.toFixed(3), leg?.initialBearing);
197
+ * ```
198
+ */
199
+ export function routeLeg(from, to) {
200
+ const sphere = greatCircleKm(from, to);
201
+ if (sphere === null)
202
+ return null;
203
+ const solved = vincentyInverse(from, to);
204
+ const exact = solved !== null;
205
+ const kilometres = exact ? solved.metres / 1000 : sphere;
206
+ const bearings = exact ? { initial: solved.initial, final: solved.final } : sphericalBearings(from, to);
207
+ const midpoint = exact ? vincentyDirect(from, bearings.initial, solved.metres / 2) : trackSteps(from, to, Math.max(sphere / MEAN_RADIUS_KM / RAD / 2, 0.01))[1] ?? from;
208
+ return { kilometres, greatCircleKm: sphere, initialBearing: bearings.initial, finalBearing: bearings.final, flightHours: kilometres / FLIGHT_SPEED_KM_H, midpoint, exact };
209
+ }
210
+ /**
211
+ * Points along the geodesic from `from` to `to`, including both ends. `count` is how many (2 when they are the same place, 64 when left out).
212
+ * When the geodesic does not converge, the points are the great circle (`trackSteps`).
213
+ *
214
+ * @param from - The start.
215
+ * @param to - The end.
216
+ * @param count - How many points, at least 2.
217
+ * @returns The points, or an empty list when a place is not on the Earth.
218
+ *
219
+ * @example
220
+ * ```ts
221
+ * import { routePoints } from "@johnmorrisdotca/chikyu-earth";
222
+ *
223
+ * console.log(routePoints({ lon: 0, lat: 0 }, { lon: 1, lat: 0 }, 3).length);
224
+ * ```
225
+ */
226
+ export function routePoints(from, to, count = 64) {
227
+ const leg = routeLeg(from, to);
228
+ if (!leg)
229
+ return [];
230
+ const n = Math.max(2, Math.floor(count));
231
+ if (leg.kilometres === 0)
232
+ return [from, to];
233
+ if (!leg.exact) {
234
+ // nearly opposite places: the great circle, by the spherical direct problem from the initial bearing, so there are exactly `n` points, evenly spaced
235
+ const angle = leg.greatCircleKm / MEAN_RADIUS_KM;
236
+ return Array.from({ length: n }, (_, i) => (i === 0 ? { lon: from.lon, lat: from.lat } : i === n - 1 ? { lon: to.lon, lat: to.lat } : sphereDirect(from, leg.initialBearing, (angle * i) / (n - 1))));
237
+ }
238
+ const out = [];
239
+ for (let i = 0; i < n; i += 1)
240
+ out.push(i === n - 1 ? { lon: to.lon, lat: to.lat } : vincentyDirect(from, leg.initialBearing, (leg.kilometres * 1000 * i) / (n - 1)));
241
+ return out;
242
+ }
243
+ /**
244
+ * The runs of a route after it is split at the date line, so a flat map draws two pieces instead of a line across the world.
245
+ * A run that does not cross is one piece. Uses `splitDateLine`.
246
+ *
247
+ * @param points - Points in order along the route, including the ends.
248
+ * @returns One list of points per piece.
249
+ *
250
+ * @example
251
+ * ```ts
252
+ * import { routeRuns } from "@johnmorrisdotca/chikyu-earth";
253
+ *
254
+ * console.log(routeRuns([{ lon: 179, lat: 0 }, { lon: -179, lat: 0 }]).length);
255
+ * ```
256
+ */
257
+ export function routeRuns(points) {
258
+ if (points.length === 0)
259
+ return [];
260
+ const runs = [[{ lon: points[0].lon, lat: points[0].lat }]];
261
+ for (let i = 1; i < points.length; i += 1) {
262
+ const next = points[i];
263
+ const parts = splitDateLine(points[i - 1], next);
264
+ const run = runs[runs.length - 1];
265
+ if (parts.length === 1)
266
+ run.push({ lon: next.lon, lat: next.lat });
267
+ else {
268
+ run.push(parts[0][1]);
269
+ runs.push([parts[1][0], { lon: next.lon, lat: next.lat }]);
270
+ }
271
+ }
272
+ return runs;
273
+ }
274
+ /**
275
+ * Read the `route` attribute: `lon,lat,lon,lat`. Anything else is no route.
276
+ *
277
+ * @param value - The attribute's text, or null.
278
+ * @returns The two places, or null.
279
+ *
280
+ * @example
281
+ * ```ts
282
+ * import { readRoute } from "@johnmorrisdotca/chikyu-earth";
283
+ *
284
+ * console.log(readRoute("139.69,35.69,-0.13,51.51")?.from.lat);
285
+ * console.log(readRoute("no"));
286
+ * ```
287
+ */
288
+ export function readRoute(value) {
289
+ if (!value)
290
+ return null;
291
+ const parts = value.split(",").map((part) => Number(part.trim()));
292
+ if (parts.length !== 4 || parts.some((n) => !Number.isFinite(n)))
293
+ return null;
294
+ const route = { from: { lon: parts[0], lat: parts[1] }, to: { lon: parts[2], lat: parts[3] } };
295
+ if (!isPlace(route.from) || !isPlace(route.to))
296
+ return null;
297
+ return route;
298
+ }
299
+ /**
300
+ * The `route` attribute for two places, four decimals.
301
+ *
302
+ * @param route - The two places.
303
+ * @returns `lon,lat,lon,lat`.
304
+ *
305
+ * @example
306
+ * ```ts
307
+ * import { routeText } from "@johnmorrisdotca/chikyu-earth";
308
+ *
309
+ * console.log(routeText({ from: { lon: 1, lat: 2 }, to: { lon: 3, lat: 4 } }));
310
+ * ```
311
+ */
312
+ export function routeText(route) {
313
+ const four = (n) => String(Math.round(n * 10_000) / 10_000);
314
+ return `${four(route.from.lon)},${four(route.from.lat)},${four(route.to.lon)},${four(route.to.lat)}`;
315
+ }
@@ -1,4 +1,5 @@
1
1
  export { createSatellites, type SatellitesOptions } from "./track.ts";
2
+ export { NEAREST_COUNT, closeApproach, nearest, overheadKm, within, type Neighbour, type NearestOptions, type NearestTarget } from "./nearest.ts";
2
3
  export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } from "./limits.ts";
3
4
  export type { ElementSet, RefusedElementSet, SatelliteInfo, SatelliteRef, Satellites } from "./types.ts";
4
5
  export { SHORT_NAMES } from "./names.ts";
@@ -1,5 +1,6 @@
1
1
  // The satellites' pure parts: orbital elements in, positions out (SGP4 by satellite.js, an optional peer). They import on a server. The space view takes a group with `setSatellites`.
2
2
  export { createSatellites } from "./track.js";
3
+ export { NEAREST_COUNT, closeApproach, nearest, overheadKm, within } from "./nearest.js";
3
4
  export { DEFAULT_DIM_AFTER_DAYS, DEFAULT_MAX_AGE_DAYS, MAX_SATELLITE_SPEED } from "./limits.js";
4
5
  export { SHORT_NAMES } from "./names.js";
5
6
  export { OWNER_COUNTRIES, ownerCountry } from "./owners.js";
@@ -0,0 +1,162 @@
1
+ import type { GeoPoint } from "../types.ts";
2
+ import type { Satellites } from "./types.ts";
3
+ /**
4
+ * How many neighbours `nearest` returns when `count` is left out.
5
+ *
6
+ * @example
7
+ * ```ts
8
+ * import { NEAREST_COUNT } from "@johnmorrisdotca/chikyu-earth/satellites";
9
+ *
10
+ * console.log(NEAREST_COUNT);
11
+ * ```
12
+ */
13
+ export declare const NEAREST_COUNT = 5;
14
+ /**
15
+ * One satellite and how near it is. `angle` is degrees, for `by: "sky"`. `distance` is kilometres, for `by: "space"`.
16
+ *
17
+ * @example
18
+ * ```ts
19
+ * import type { Neighbour } from "@johnmorrisdotca/chikyu-earth/satellites";
20
+ *
21
+ * const one: Neighbour = { index: 0, name: "ISS (ZARYA)", noradId: 25544, distance: { kilometres: 420, uncertaintyKm: 1, unit: "km" } };
22
+ * console.log(one.distance?.unit);
23
+ * ```
24
+ */
25
+ export interface Neighbour {
26
+ index: number;
27
+ name: string;
28
+ noradId: number;
29
+ /** Degrees, when asked `by: "sky"`. */
30
+ angle?: number;
31
+ /** Kilometres, when asked `by: "space"`. */
32
+ distance?: {
33
+ kilometres: number;
34
+ uncertaintyKm: number;
35
+ unit: "km";
36
+ };
37
+ }
38
+ /**
39
+ * A place on the ground, a direction in the sky from an observer, or another satellite of the same group.
40
+ *
41
+ * @example
42
+ * ```ts
43
+ * import type { NearestTarget } from "@johnmorrisdotca/chikyu-earth/satellites";
44
+ *
45
+ * const target: NearestTarget = { place: { lon: -0.13, lat: 51.51 } };
46
+ * console.log("place" in target);
47
+ * ```
48
+ */
49
+ export type NearestTarget = {
50
+ place: GeoPoint;
51
+ heightM?: number;
52
+ } | {
53
+ azimuth: number;
54
+ elevation: number;
55
+ observer: GeoPoint & {
56
+ heightM?: number;
57
+ };
58
+ } | {
59
+ satellite: number;
60
+ };
61
+ /**
62
+ * What `nearest` may be asked. `date` is required.
63
+ *
64
+ * @example
65
+ * ```ts
66
+ * import type { NearestOptions } from "@johnmorrisdotca/chikyu-earth/satellites";
67
+ *
68
+ * const options: NearestOptions = { date: Date.UTC(2026, 10, 2), by: "space", count: 5 };
69
+ * console.log(options.by);
70
+ * ```
71
+ */
72
+ export interface NearestOptions {
73
+ /** How many to return, nearest first. {@link NEAREST_COUNT} when left out. */
74
+ count?: number;
75
+ /** Drop anything further than this: kilometres for `by: "space"`, degrees for `by: "sky"`. No limit when left out. */
76
+ within?: number;
77
+ /** `"space"` (kilometres, the default) or `"sky"` (the angle at the observer). */
78
+ by?: "sky" | "space";
79
+ /** The moment, milliseconds since the Unix epoch. */
80
+ date: number;
81
+ /** Where the sky is seen from, when the target is a satellite and `by` is `"sky"`. */
82
+ observer?: GeoPoint & {
83
+ heightM?: number;
84
+ };
85
+ }
86
+ /**
87
+ * The satellites nearest a place, a direction in the sky, or another satellite, at one moment. One SGP4 call (`exact`) for each satellite that can be placed.
88
+ * It does not run again by itself. `by: "space"` answers with kilometres; `by: "sky"` with the angle in degrees. Nearest first.
89
+ *
90
+ * @param group - The satellites, from `createSatellites`.
91
+ * @param target - A place, a direction (`azimuth`, `elevation`, `observer`), or `{ satellite: index }`.
92
+ * @param options - `date` (required), `count` (5), `within`, `by` (`"space"`), and `observer` for a satellite seen in the sky.
93
+ * @returns The neighbours, nearest first.
94
+ *
95
+ * @example
96
+ * ```ts
97
+ * import { createSatellites, nearest } from "@johnmorrisdotca/chikyu-earth/satellites";
98
+ *
99
+ * const group = createSatellites([]);
100
+ * console.log(nearest(group, { place: { lon: 0, lat: 0 } }, { date: Date.UTC(2026, 10, 2) }).length);
101
+ * ```
102
+ */
103
+ export declare function nearest(group: Satellites, target: NearestTarget, options: NearestOptions): Neighbour[];
104
+ /**
105
+ * Every satellite within `radius` of a target, nearest first. Kilometres for `by: "space"`, degrees for `by: "sky"`. The same one pass as `nearest`.
106
+ *
107
+ * @param group - The satellites.
108
+ * @param target - A place, a direction, or a satellite.
109
+ * @param radius - The limit, in the unit `by` uses.
110
+ * @param options - As `nearest`, without `count` and `within`.
111
+ * @returns The neighbours inside the limit.
112
+ *
113
+ * @example
114
+ * ```ts
115
+ * import { createSatellites, within } from "@johnmorrisdotca/chikyu-earth/satellites";
116
+ *
117
+ * const group = createSatellites([]);
118
+ * console.log(within(group, { place: { lon: 0, lat: 0 } }, 1000, { date: Date.UTC(2026, 10, 2) }).length);
119
+ * ```
120
+ */
121
+ export declare function within(group: Satellites, target: NearestTarget, radius: number, options: Omit<NearestOptions, "count" | "within">): Neighbour[];
122
+ /**
123
+ * The closest the two satellites come between `from` and `to`. The span is sampled every `stepMs` (60 seconds when left out), and the moment of the closest sample is then refined between its two neighbours,
124
+ * to a tenth of a second, because two satellites close in at several kilometres a second and the closest sample alone can be hundreds of kilometres too far. The uncertainty is the elements' own: the
125
+ * square root of the sum of the squares of the two satellites' rough uncertainties (1 km, and 1 km more for each day the elements have aged at that moment), not a published covariance.
126
+ * Two SGP4 calls a sample, and about 20 more for the refinement. It does not run again by itself.
127
+ *
128
+ * @param group - The satellites.
129
+ * @param a - The index of one satellite.
130
+ * @param b - The index of the other.
131
+ * @param from - The start, milliseconds since the Unix epoch.
132
+ * @param to - The end.
133
+ * @param stepMs - The sample step, at least 1000.
134
+ * @returns The closest moment, or null when either satellite cannot be placed.
135
+ *
136
+ * @example
137
+ * ```ts
138
+ * import { closeApproach, createSatellites } from "@johnmorrisdotca/chikyu-earth/satellites";
139
+ *
140
+ * const group = createSatellites([]);
141
+ * console.log(closeApproach(group, 0, 1, 0, 60_000));
142
+ * ```
143
+ */
144
+ export declare function closeApproach(group: Satellites, a: number, b: number, from: number, to: number, stepMs?: number): {
145
+ at: number;
146
+ kilometres: number;
147
+ uncertaintyKm: number;
148
+ } | null;
149
+ /**
150
+ * The slant range from a place to a satellite that is straight overhead, for a test or a page: it is the satellite's height. Positions are kilometres in one frame.
151
+ *
152
+ * @param heightKm - How far above the ground the satellite is.
153
+ * @returns The distance, which is `heightKm`.
154
+ *
155
+ * @example
156
+ * ```ts
157
+ * import { overheadKm } from "@johnmorrisdotca/chikyu-earth/satellites";
158
+ *
159
+ * console.log(overheadKm(420));
160
+ * ```
161
+ */
162
+ export declare function overheadKm(heightKm: number): number;