Skip to content

Commit 9623fc2

Browse files
vinc3m1claude
andauthored
Clarify location vs. closest station, and surface marine sample point (#2)
The title bar conflated "where you are" with "which station the tide prediction comes from", and the marine card silently used whatever offshore grid cell Open-Meteo snapped to. Make both explicit. Title bar (LocationBar): - Show the reverse-geocoded place as the title and the snapped tide station as a distinct "Closest station: <name> · <dist> · <confidence>" chip, so it's obvious the two are different places. Reverse geocoding (new src/sources/reverse.ts): - Add a key-free, CORS reverse geocoder (BigDataCloud), wired into selectPoint so "use my location" and dropped pins resolve to a real place name instead of falling back to the station's name. Best-effort: runs concurrently with the station load and falls back to the station name when offline or on error. Marine data provenance: - Capture the grid cell Open-Meteo actually samples (it echoes the snapped lat/lon) in parseMarine. The marine card now shows "Open-Meteo · ~N km offshore", and the map marks the sampled cell with a wave marker plus a dashed link from the chosen point. Tests: unit coverage for reverse parsing and the sampled-cell extraction; e2e mocks for BigDataCloud + the snapped marine cell; assertions for the closest-station chip, the offshore label, and the marine map marker. Co-authored-by: Claude <noreply@anthropic.com>
1 parent b29477d commit 9623fc2

12 files changed

Lines changed: 355 additions & 31 deletions

File tree

‎src/components/LocationBar.svelte‎

Lines changed: 54 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,14 @@
1515
export let stationName = '';
1616
1717
$: conf = km !== null ? confidenceForKm(km) : null;
18-
$: showStation = km !== null && !!stationName && stationName !== name;
1918
</script>
2019

2120
<header class="locbar">
22-
<div class="info">
23-
<div class="name">{name}</div>
24-
{#if km !== null && conf}
25-
<div class="meta">
26-
{#if showStation}{stationName} · {/if}{formatDistance(km, distanceUnit)} away ·
27-
<span class={`conf ${conf.level}`}>{conf.label}</span>
28-
</div>
29-
{/if}
30-
</div>
31-
<div class="actions">
21+
<div class="top">
22+
<div class="info">
23+
<div class="name">{name}</div>
24+
</div>
25+
<div class="actions">
3226
<button
3327
class="star"
3428
class:on={isFav}
@@ -52,24 +46,70 @@
5246
<line x1="21" y1="21" x2="16.65" y2="16.65"></line>
5347
</svg>
5448
</button>
49+
</div>
5550
</div>
51+
52+
{#if km !== null && conf}
53+
<div class="station-chip" data-testid="closest-station">
54+
<span class="anchor" aria-hidden="true">⚓</span>
55+
<span class="chip-text">
56+
<span class="chip-label">Closest station:</span>
57+
<span class="chip-station">{stationName}</span>
58+
<span class="chip-sep">·</span>{formatDistance(km, distanceUnit)} away<span class="chip-sep">·</span><span class={`conf ${conf.level}`}>{conf.label}</span>
59+
</span>
60+
</div>
61+
{/if}
5662
</header>
5763

5864
<style>
5965
.locbar {
66+
display: flex;
67+
flex-direction: column;
68+
gap: 0.5rem;
69+
}
70+
.top {
6071
display: flex;
6172
align-items: center;
6273
justify-content: space-between;
6374
gap: 0.5rem;
6475
}
76+
.info {
77+
min-width: 0;
78+
}
6579
.name {
6680
font-size: 1.15rem;
6781
font-weight: 700;
6882
}
69-
.meta {
83+
.station-chip {
84+
display: inline-flex;
85+
align-items: center;
86+
gap: 0.4rem;
87+
align-self: flex-start;
88+
max-width: 100%;
89+
padding: 0.3rem 0.6rem;
90+
border-radius: 999px;
91+
background: color-mix(in srgb, var(--accent) 12%, transparent);
92+
border: 1px solid color-mix(in srgb, var(--accent) 28%, transparent);
93+
color: var(--text);
94+
font-size: 0.82rem;
95+
line-height: 1.25;
96+
}
97+
.station-chip .anchor {
98+
font-size: 0.9rem;
99+
flex: none;
100+
}
101+
.chip-text {
102+
min-width: 0;
103+
}
104+
.chip-label {
105+
color: var(--muted);
106+
}
107+
.chip-station {
108+
font-weight: 700;
109+
}
110+
.chip-sep {
111+
margin: 0 0.35rem;
70112
color: var(--muted);
71-
font-size: 0.85rem;
72-
margin-top: 0.1rem;
73113
}
74114
.conf.good {
75115
color: var(--accent);

‎src/components/MarineCard.svelte‎

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
<script lang="ts">
2-
import type { HeightUnit, TimeFormat } from '../engine/types';
3-
import { formatHeight } from '../engine/units';
2+
import type { DistanceUnit, HeightUnit, TimeFormat } from '../engine/types';
3+
import { formatHeight, formatDistance } from '../engine/units';
4+
import { haversineKm } from '../engine/stations';
45
import { formatTime } from '../engine/time';
56
import { getMarine, type MarineData, type MarinePoint } from '../sources/marine';
67
import { makeScale, buildLinePath } from '../chart/geometry';
@@ -10,9 +11,15 @@
1011
export let dayStart: Date;
1112
export let dayEnd: Date;
1213
export let heightUnit: HeightUnit;
14+
export let distanceUnit: DistanceUnit;
1315
export let timeFormat: TimeFormat;
1416
export let tz: string;
1517
export let scrubMs: number;
18+
/**
19+
* The grid cell the forecast was actually sampled from, surfaced for the parent so it
20+
* can mark it on the map. Bindable — set whenever a fetch resolves with coordinates.
21+
*/
22+
export let sampled: { lat: number; lon: number } | null = null;
1623
1724
type State = 'loading' | 'ready' | 'offline' | 'nodata' | 'error';
1825
let state: State = 'loading';
@@ -65,6 +72,13 @@
6572
$: closestPoint = findClosestPoint(data?.points ?? [], scrubMs);
6673
$: activePoint = closestPoint || data?.peak;
6774
75+
// Surface the sampled grid cell to the parent (for the map marker) and describe how
76+
// far offshore it sits from the chosen point — the wave model can't sample inland.
77+
$: sampled = data?.sampled ?? null;
78+
$: offshoreKm = data?.sampled ? haversineKm(lat, lon, data.sampled.lat, data.sampled.lon) : null;
79+
$: offshoreLabel =
80+
offshoreKm != null && offshoreKm >= 0.5 ? `~${formatDistance(offshoreKm, distanceUnit)} offshore` : '';
81+
6882
// Sparkline/chart geometry setup.
6983
$: chartPoints = data ? data.points.map((p) => ({
7084
time: p.time,
@@ -160,7 +174,9 @@
160174
<span class="active-time" data-testid="marine-active-time">{formatTime(activePoint.time, tz, timeFormat)}</span>
161175
{/if}
162176
</div>
163-
<span class="src">Open-Meteo</span>
177+
<span class="src" data-testid="marine-source"
178+
>Open-Meteo{#if offshoreLabel} · <span class="offshore">{offshoreLabel}</span>{/if}</span
179+
>
164180
</div>
165181

166182
{#if state === 'loading'}
@@ -254,6 +270,9 @@
254270
color: var(--muted);
255271
font-size: 0.75rem;
256272
}
273+
.offshore {
274+
color: var(--text);
275+
}
257276
.muted {
258277
margin: 0;
259278
color: var(--muted);

‎src/components/StationMap.svelte‎

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
export let stationLon: number | null = null;
1717
export let stationName = 'Tide station';
1818
export let stationType: 'reference' | 'subordinate' | null = null;
19+
/** Grid cell the marine forecast was sampled from (offshore); null hides the marker. */
20+
export let marineLat: number | null = null;
21+
export let marineLon: number | null = null;
1922
export let mode: 'overlay' | 'inline' = 'overlay';
2023
export let hideSearch = false;
2124
@@ -24,6 +27,7 @@
2427
let map: maplibregl.Map | undefined;
2528
let currentMarker: maplibregl.Marker | undefined;
2629
let stationMarker: maplibregl.Marker | undefined;
30+
let marineMarker: maplibregl.Marker | undefined;
2731
let pinMarker: maplibregl.Marker | undefined;
2832
let picked: { lat: number; lon: number } | null = null;
2933
let busy = false;
@@ -175,6 +179,65 @@
175179
return el;
176180
}
177181
182+
function makeMarineMarker() {
183+
const el = document.createElement('div');
184+
el.className = 'marine-sample-marker';
185+
el.dataset.testid = 'marine-sample-marker';
186+
el.title = 'Waves & swell sampled here';
187+
el.setAttribute('aria-label', 'Marine forecast sample point');
188+
el.innerHTML = `
189+
<svg width="22" height="22" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
190+
<path d="M2 8c2 0 2.5-2 5-2s2.5 2 5 2 2.5-2 5-2 2.5 2 5 2M2 14c2 0 2.5-2 5-2s2.5 2 5 2 2.5-2 5-2 2.5 2 5 2"
191+
stroke="#ffffff" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/>
192+
</svg>`;
193+
return el;
194+
}
195+
196+
/** Show/move/remove the marine marker and the dashed link from the chosen point to it. */
197+
function updateMarine(mlat: number | null, mlon: number | null, fromLat: number, fromLon: number) {
198+
if (!map) return;
199+
if (mlat == null || mlon == null) {
200+
marineMarker?.remove();
201+
marineMarker = undefined;
202+
setMarineLink(null, null, null, null);
203+
return;
204+
}
205+
if (marineMarker) marineMarker.setLngLat([mlon, mlat]);
206+
else {
207+
marineMarker = new maplibregl.Marker({ element: makeMarineMarker(), anchor: 'center' })
208+
.setLngLat([mlon, mlat])
209+
.addTo(map);
210+
}
211+
setMarineLink(fromLon, fromLat, mlon, mlat);
212+
}
213+
214+
function setMarineLink(
215+
fromLon: number | null,
216+
fromLat: number | null,
217+
toLon: number | null,
218+
toLat: number | null,
219+
) {
220+
const src = map?.getSource('marine-link') as maplibregl.GeoJSONSource | undefined;
221+
if (!src) return;
222+
const features =
223+
fromLon == null || fromLat == null || toLon == null || toLat == null
224+
? []
225+
: [
226+
{
227+
type: 'Feature' as const,
228+
geometry: {
229+
type: 'LineString' as const,
230+
coordinates: [
231+
[fromLon, fromLat],
232+
[toLon, toLat],
233+
],
234+
},
235+
properties: {},
236+
},
237+
];
238+
src.setData({ type: 'FeatureCollection', features } as never);
239+
}
240+
178241
function makePendingMarker() {
179242
const el = document.createElement('div');
180243
el.className = 'pending-location-marker';
@@ -280,6 +343,24 @@
280343
const attribEl = container.querySelector('.maplibregl-ctrl-attrib.maplibregl-compact');
281344
if (attribEl) attribEl.classList.remove('maplibregl-compact-show');
282345
346+
// Dashed link between the chosen point and the offshore cell the waves come from.
347+
map.addSource('marine-link', {
348+
type: 'geojson',
349+
data: { type: 'FeatureCollection', features: [] } as never,
350+
});
351+
map.addLayer({
352+
id: 'marine-link',
353+
type: 'line',
354+
source: 'marine-link',
355+
layout: { 'line-cap': 'round' },
356+
paint: {
357+
'line-color': '#0a7ea4',
358+
'line-width': 2,
359+
'line-opacity': 0.55,
360+
'line-dasharray': [1.5, 1.5],
361+
},
362+
});
363+
283364
map.addSource('stations', { type: 'geojson', data: data as never });
284365
map.addLayer({
285366
id: 'stations-c',
@@ -316,6 +397,9 @@
316397
},
317398
paint: { 'text-color': '#0b3a5a', 'text-halo-color': '#ffffff', 'text-halo-width': 1.2 },
318399
});
400+
401+
// Apply any marine sample that arrived while the style was still loading.
402+
updateMarine(marineLat, marineLon, lat, lon);
319403
});
320404
321405
map.on('mouseenter', 'stations-c', () => map && (map.getCanvas().style.cursor = 'pointer'));
@@ -336,6 +420,7 @@
336420
});
337421
338422
$: if (map) fitSelection(map, lat, lon, stationLat, stationLon, mode);
423+
$: if (map) updateMarine(marineLat, marineLon, lat, lon);
339424
$: if (stationMarker && stationType) {
340425
const el = stationMarker.getElement();
341426
if (el) {
@@ -549,6 +634,17 @@
549634
:global(.selected-station-marker:hover) {
550635
transform: scale(1.1);
551636
}
637+
:global(.marine-sample-marker) {
638+
width: 1.7rem;
639+
height: 1.7rem;
640+
display: flex;
641+
align-items: center;
642+
justify-content: center;
643+
border: 2px solid #ffffff;
644+
border-radius: 999px;
645+
background: #0a7ea4;
646+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
647+
}
552648
:global(.pending-location-marker) {
553649
width: 1.4rem;
554650
height: 1.4rem;

‎src/routes/Home.svelte‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@
1717
let dayOffset = 0;
1818
let scrubMs = now.getTime();
1919
let showMap = false;
20+
// Where the marine forecast was actually sampled (offshore grid cell), surfaced by
21+
// MarineCard so the map can mark it. Reset on every location change so a stale cell
22+
// never lingers on the new map while the next forecast loads.
23+
let marineSampled: { lat: number; lon: number } | null = null;
24+
let lastPointKey = '';
25+
$: if ($selection) {
26+
const k = `${$selection.point.lat},${$selection.point.lon}`;
27+
if (k !== lastPointKey) {
28+
lastPointKey = k;
29+
marineSampled = null;
30+
}
31+
}
32+
$: marineMarker = $settings.showMarine ? marineSampled : null;
2033
// Keep MapLibre split out of the initial bundle; load it after the home shell renders.
2134
let MapComp: typeof import('../components/StationMap.svelte').default | null = null;
2235
let mapInstance: any;
@@ -188,9 +201,11 @@
188201
{dayStart}
189202
{dayEnd}
190203
heightUnit={$settings.heightUnit}
204+
distanceUnit={$settings.distanceUnit}
191205
timeFormat={$settings.timeFormat}
192206
{tz}
193207
bind:scrubMs
208+
bind:sampled={marineSampled}
194209
/>
195210
{/if}
196211
</div>
@@ -224,6 +239,8 @@
224239
stationLon={$selection.station.longitude}
225240
stationName={$selection.station.name}
226241
stationType={$selection.station.type}
242+
marineLat={marineMarker?.lat ?? null}
243+
marineLon={marineMarker?.lon ?? null}
227244
/>
228245
{/key}
229246
{:else}
@@ -260,6 +277,8 @@
260277
stationLon={$selection.station.longitude}
261278
stationName={$selection.station.name}
262279
stationType={$selection.station.type}
280+
marineLat={marineMarker?.lat ?? null}
281+
marineLon={marineMarker?.lon ?? null}
263282
on:close={() => (showMap = false)}
264283
/>
265284
{/if}

‎src/sources/marine.test.ts‎

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,27 @@ describe('marine parsing', () => {
4949
});
5050

5151
it('returns empty for missing or fully-null data', () => {
52-
expect(parseMarine({}, start, end)).toEqual({ points: [], peak: null });
53-
expect(parseMarine(null, start, end)).toEqual({ points: [], peak: null });
52+
expect(parseMarine({}, start, end)).toEqual({ points: [], peak: null, sampled: null });
53+
expect(parseMarine(null, start, end)).toEqual({ points: [], peak: null, sampled: null });
5454
const allNull = parseMarine(
5555
{ hourly: { time: [day(0)], wave_height: [null] } },
5656
start,
5757
end,
5858
);
5959
expect(allNull.peak).toBeNull();
6060
});
61+
62+
it('captures the snapped grid-cell coordinates the API echoes back', () => {
63+
const data = {
64+
latitude: -8.75,
65+
longitude: 115.25,
66+
hourly: { time: [day(0)], wave_height: [1.2], swell_wave_height: [0.6], swell_wave_period: [8] },
67+
};
68+
expect(parseMarine(data, start, end).sampled).toEqual({ lat: -8.75, lon: 115.25 });
69+
});
70+
71+
it('leaves sampled null when the response omits coordinates', () => {
72+
const data = { hourly: { time: [day(0)], wave_height: [1.2] } };
73+
expect(parseMarine(data, start, end).sampled).toBeNull();
74+
});
6175
});

0 commit comments

Comments
 (0)