|
16 | 16 | export let stationLon: number | null = null; |
17 | 17 | export let stationName = 'Tide station'; |
18 | 18 | 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; |
19 | 22 | export let mode: 'overlay' | 'inline' = 'overlay'; |
20 | 23 | export let hideSearch = false; |
21 | 24 |
|
|
24 | 27 | let map: maplibregl.Map | undefined; |
25 | 28 | let currentMarker: maplibregl.Marker | undefined; |
26 | 29 | let stationMarker: maplibregl.Marker | undefined; |
| 30 | + let marineMarker: maplibregl.Marker | undefined; |
27 | 31 | let pinMarker: maplibregl.Marker | undefined; |
28 | 32 | let picked: { lat: number; lon: number } | null = null; |
29 | 33 | let busy = false; |
|
175 | 179 | return el; |
176 | 180 | } |
177 | 181 |
|
| 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 | +
|
178 | 241 | function makePendingMarker() { |
179 | 242 | const el = document.createElement('div'); |
180 | 243 | el.className = 'pending-location-marker'; |
|
280 | 343 | const attribEl = container.querySelector('.maplibregl-ctrl-attrib.maplibregl-compact'); |
281 | 344 | if (attribEl) attribEl.classList.remove('maplibregl-compact-show'); |
282 | 345 |
|
| 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 | +
|
283 | 364 | map.addSource('stations', { type: 'geojson', data: data as never }); |
284 | 365 | map.addLayer({ |
285 | 366 | id: 'stations-c', |
|
316 | 397 | }, |
317 | 398 | paint: { 'text-color': '#0b3a5a', 'text-halo-color': '#ffffff', 'text-halo-width': 1.2 }, |
318 | 399 | }); |
| 400 | +
|
| 401 | + // Apply any marine sample that arrived while the style was still loading. |
| 402 | + updateMarine(marineLat, marineLon, lat, lon); |
319 | 403 | }); |
320 | 404 |
|
321 | 405 | map.on('mouseenter', 'stations-c', () => map && (map.getCanvas().style.cursor = 'pointer')); |
|
336 | 420 | }); |
337 | 421 |
|
338 | 422 | $: if (map) fitSelection(map, lat, lon, stationLat, stationLon, mode); |
| 423 | + $: if (map) updateMarine(marineLat, marineLon, lat, lon); |
339 | 424 | $: if (stationMarker && stationType) { |
340 | 425 | const el = stationMarker.getElement(); |
341 | 426 | if (el) { |
|
549 | 634 | :global(.selected-station-marker:hover) { |
550 | 635 | transform: scale(1.1); |
551 | 636 | } |
| 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 | + } |
552 | 648 | :global(.pending-location-marker) { |
553 | 649 | width: 1.4rem; |
554 | 650 | height: 1.4rem; |
|
0 commit comments