|
1 | | -import { getDistance } from "geolib"; |
2 | | -import { stations, type Station } from "@neaps/tide-database"; |
| 1 | +import { |
| 2 | + stations, |
| 3 | + near, |
| 4 | + nearest, |
| 5 | + type Station, |
| 6 | + type Position, |
| 7 | + type NearOptions, |
| 8 | + type NearestOptions, |
| 9 | +} from "@neaps/tide-database"; |
3 | 10 | import tidePredictor, { type TimeSpan, type ExtremesInput } from "@neaps/tide-predictor"; |
4 | | -import type { GeolibInputCoordinates } from "geolib/es/types"; |
5 | 11 |
|
6 | 12 | type Units = "meters" | "feet"; |
7 | 13 | type PredictionOptions = { |
@@ -34,41 +40,39 @@ const defaultUnits: Units = "meters"; |
34 | 40 | * datum: 'MLLW', // optional, defaults to MLLW if available |
35 | 41 | * }) |
36 | 42 | */ |
37 | | -export function getExtremesPrediction(options: GeolibInputCoordinates & ExtremesOptions) { |
| 43 | +export function getExtremesPrediction(options: NearestOptions & ExtremesOptions) { |
38 | 44 | return nearestStation(options).getExtremesPrediction(options); |
39 | 45 | } |
40 | 46 |
|
41 | 47 | /** |
42 | 48 | * Get timeline prediction using the nearest station to the given position. |
43 | 49 | */ |
44 | | -export function getTimelinePrediction(options: GeolibInputCoordinates & TimelineOptions) { |
| 50 | +export function getTimelinePrediction(options: NearestOptions & TimelineOptions) { |
45 | 51 | return nearestStation(options).getTimelinePrediction(options); |
46 | 52 | } |
47 | 53 |
|
48 | 54 | /** |
49 | 55 | * Get water level at a specific time using the nearest station to the given position. |
50 | 56 | */ |
51 | | -export function getWaterLevelAtTime(options: GeolibInputCoordinates & WaterLevelOptions) { |
| 57 | +export function getWaterLevelAtTime(options: NearestOptions & WaterLevelOptions) { |
52 | 58 | return nearestStation(options).getWaterLevelAtTime(options); |
53 | 59 | } |
54 | 60 |
|
55 | 61 | /** |
56 | 62 | * Find the nearest station to the given position. |
57 | 63 | */ |
58 | | -export function nearestStation(position: GeolibInputCoordinates) { |
59 | | - return stationsNear(position, 1)[0]; |
| 64 | +export function nearestStation(options: NearestOptions) { |
| 65 | + const data = nearest(options); |
| 66 | + if (!data) throw new Error(`No stations found with options: ${JSON.stringify(options)}`); |
| 67 | + return useStation(...data); |
60 | 68 | } |
61 | 69 |
|
62 | 70 | /** |
63 | 71 | * Find stations near the given position. |
64 | 72 | * @param limit Maximum number of stations to return (default: 10) |
65 | 73 | */ |
66 | | -export function stationsNear(position: GeolibInputCoordinates, limit = 10) { |
67 | | - return stations |
68 | | - .map((station) => ({ station, distance: getDistance(position, station) })) |
69 | | - .sort((a, b) => a.distance - b.distance) |
70 | | - .slice(0, limit) |
71 | | - .map(({ station, distance }) => useStation(station, distance)); |
| 74 | +export function stationsNear(options: NearOptions) { |
| 75 | + return near(options).map(([station, distance]) => useStation(station, distance)); |
72 | 76 | } |
73 | 77 |
|
74 | 78 | /** |
|
0 commit comments