1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
|
import maplibregl from "maplibre-gl";
import React, { useCallback, useEffect, useMemo, useRef, useState } from "react";
import Map, {
Layer,
Marker,
Source,
type MapRef
} from "react-map-gl/maplibre";
import { Sheet } from "react-modal-sheet";
import { useApp } from "~/AppContext";
import { REGION_DATA } from "~/config/RegionConfig";
import { getLineColour } from "~/data/LineColors";
import type { Stop } from "~/data/StopDataProvider";
import { loadStyle } from "~/maps/styleloader";
import "./StopMapModal.css";
export interface Position {
latitude: number;
longitude: number;
orientationDegrees: number;
shapeIndex?: number;
}
export interface ConsolidatedCirculationForMap {
id: string;
line: string;
route: string;
currentPosition?: Position;
stopShapeIndex?: number;
isPreviousTrip?: boolean;
previousTripShapeId?: string;
schedule?: {
shapeId?: string;
};
}
interface StopMapModalProps {
stop: Stop;
circulations: ConsolidatedCirculationForMap[];
isOpen: boolean;
onClose: () => void;
selectedCirculationId?: string;
}
export const StopMapModal: React.FC<StopMapModalProps> = ({
stop,
circulations,
isOpen,
onClose,
selectedCirculationId,
}) => {
const { theme } = useApp();
const [styleSpec, setStyleSpec] = useState<any | null>(null);
const mapRef = useRef<MapRef | null>(null);
const hasFitBounds = useRef(false);
const userInteracted = useRef(false);
const [shapeData, setShapeData] = useState<any | null>(null);
const [previousShapeData, setPreviousShapeData] = useState<any | null>(null);
// Filter circulations that have GPS coordinates
const busesWithPosition = useMemo(
() => circulations.filter((c) => !!c.currentPosition),
[circulations]
);
// Use selectedCirculationId if provided, otherwise use first bus with position
const selectedBus = useMemo(() => {
if (selectedCirculationId !== undefined) {
const circulation = circulations.find(
(c) => c.id === selectedCirculationId
);
if (circulation?.currentPosition) {
return circulation;
}
}
// Fallback to first bus with position
return busesWithPosition.length > 0 ? busesWithPosition[0] : null;
}, [selectedCirculationId, circulations, busesWithPosition]);
const center = useMemo(() => {
if (selectedBus?.currentPosition) {
return {
latitude: selectedBus.currentPosition.latitude,
longitude: selectedBus.currentPosition.longitude,
};
}
if (stop.latitude && stop.longitude) {
return { latitude: stop.latitude, longitude: stop.longitude };
}
return { latitude: 42.2406, longitude: -8.7207 }; // Vigo approx fallback
}, [selectedBus, stop.latitude, stop.longitude]);
const handleCenter = useCallback(() => {
if (!mapRef.current) return;
if (userInteracted.current) return;
const points: { lat: number; lon: number }[] = [];
const addShapePoints = (data: any) => {
if (
data?.properties?.busPoint &&
data?.properties?.stopPoint &&
data?.geometry?.coordinates
) {
const busIdx = data.properties.busPoint.index;
const stopIdx = data.properties.stopPoint.index;
const coords = data.geometry.coordinates;
const start = Math.min(busIdx, stopIdx);
const end = Math.max(busIdx, stopIdx);
for (let i = start; i <= end; i++) {
points.push({ lat: coords[i][1], lon: coords[i][0] });
}
}
};
addShapePoints(shapeData);
addShapePoints(previousShapeData);
if (points.length === 0) {
if (stop.latitude && stop.longitude) {
points.push({ lat: stop.latitude, lon: stop.longitude });
}
if (selectedBus?.currentPosition) {
points.push({
lat: selectedBus.currentPosition.latitude,
lon: selectedBus.currentPosition.longitude,
});
}
}
if (points.length === 0) return;
let minLat = points[0].lat,
maxLat = points[0].lat,
minLon = points[0].lon,
maxLon = points[0].lon;
for (const p of points) {
if (p.lat < minLat) minLat = p.lat;
if (p.lat > maxLat) maxLat = p.lat;
if (p.lon < minLon) minLon = p.lon;
if (p.lon > maxLon) maxLon = p.lon;
}
const sw = [minLon, minLat] as [number, number];
const ne = [maxLon, maxLat] as [number, number];
const bounds = new maplibregl.LngLatBounds(sw, ne);
try {
if (points.length === 1) {
const only = points[0];
mapRef.current
.getMap()
.easeTo({ center: [only.lon, only.lat], zoom: 16, duration: 450 });
} else {
mapRef.current.fitBounds(bounds, {
padding: 80,
duration: 500,
maxZoom: 17,
} as any);
}
} catch { }
}, [stop, selectedBus, shapeData, previousShapeData]);
// Load style without traffic layers for the stop map
useEffect(() => {
let mounted = true;
loadStyle("openfreemap", theme, { includeTraffic: false })
.then((style) => {
if (mounted) setStyleSpec(style);
})
.catch((err) => console.error("Failed to load map style", err));
return () => {
mounted = false;
};
}, [theme]);
// Resize map and fit bounds when modal opens
useEffect(() => {
if (isOpen && mapRef.current) {
const timer = setTimeout(() => {
const map = mapRef.current?.getMap();
if (map) {
map.resize();
// Trigger fit bounds logic again
hasFitBounds.current = false;
handleCenter();
}
}, 300); // Wait for sheet animation
return () => clearTimeout(timer);
}
}, [isOpen, handleCenter]);
// Fit bounds on initial load
useEffect(() => {
if (!styleSpec || !mapRef.current || hasFitBounds.current || !isOpen)
return;
const map = mapRef.current.getMap();
// Handle missing sprite images to suppress console warnings
const handleStyleImageMissing = (e: any) => {
if (!map || map.hasImage(e.id)) return;
map.addImage(e.id, {
width: 1,
height: 1,
data: new Uint8Array(4),
});
};
map.on("styleimagemissing", handleStyleImageMissing);
handleCenter();
hasFitBounds.current = true;
return () => {
if (mapRef.current) {
const map = mapRef.current.getMap();
if (map) {
map.off("styleimagemissing", handleStyleImageMissing);
}
}
};
}, [styleSpec, stop, selectedBus, isOpen, handleCenter]);
// Reset bounds when modal opens/closes
useEffect(() => {
if (!isOpen) {
hasFitBounds.current = false;
userInteracted.current = false;
setShapeData(null);
setPreviousShapeData(null);
}
}, [isOpen]);
// Fetch shape for selected bus
useEffect(() => {
if (
!isOpen ||
!selectedBus ||
!selectedBus.schedule?.shapeId ||
selectedBus.currentPosition?.shapeIndex === undefined ||
!REGION_DATA.shapeEndpoint
) {
setShapeData(null);
setPreviousShapeData(null);
return;
}
const shapeId = selectedBus.schedule.shapeId;
const shapeIndex = selectedBus.currentPosition.shapeIndex;
const stopShapeIndex = selectedBus.stopShapeIndex;
const stopLat = stop.latitude;
const stopLon = stop.longitude;
const fetchShape = async (
sId: string,
bIndex?: number,
sIndex?: number,
sLat?: number,
sLon?: number
) => {
let url = `${REGION_DATA.shapeEndpoint}?shapeId=${sId}`;
if (bIndex !== undefined) url += `&busShapeIndex=${bIndex}`;
if (sIndex !== undefined) url += `&stopShapeIndex=${sIndex}`;
else if (sLat && sLon) url += `&stopLat=${sLat}&stopLon=${sLon}`;
const res = await fetch(url);
if (res.ok) return res.json();
return null;
};
const loadShapes = async () => {
if (selectedBus.isPreviousTrip && selectedBus.previousTripShapeId) {
// Bus is on previous trip
// 1. Load previous shape (where bus is)
const prevData = await fetchShape(
selectedBus.previousTripShapeId,
shapeIndex,
stopShapeIndex
);
// 2. Load current scheduled shape (where bus is going)
// Bus is not on this shape yet, so no bus index
const currData = await fetchShape(
shapeId,
undefined,
undefined,
stopLat,
stopLon
);
if (
prevData &&
prevData.geometry &&
prevData.geometry.coordinates &&
prevData.properties?.busPoint?.index !== undefined
) {
const busIdx = prevData.properties.busPoint.index;
const coords = prevData.geometry.coordinates;
// Slice from busIdx - 5 (clamped to 0) to end
const startIdx = Math.max(0, busIdx - 5);
const slicedCoords = coords.slice(startIdx);
// Join with the first point of the next shape to close the gap
if (currData?.geometry?.coordinates?.length > 0) {
slicedCoords.push(currData.geometry.coordinates[0]);
}
prevData.geometry.coordinates = slicedCoords;
}
setPreviousShapeData(prevData);
setShapeData(currData);
} else {
// Normal case
const data = await fetchShape(
shapeId,
shapeIndex,
stopShapeIndex,
stopLat,
stopLon
);
setShapeData(data);
setPreviousShapeData(null);
}
handleCenter();
};
loadShapes().catch((err) => console.error("Failed to load shape", err));
}, [isOpen, selectedBus]);
if (busesWithPosition.length === 0) {
return null; // Don't render if no buses with GPS coordinates
}
return (
<Sheet
isOpen={isOpen}
onClose={onClose}
detent="content"
>
<Sheet.Container style={{ backgroundColor: "var(--background-color)" }}>
<Sheet.Header />
<Sheet.Content disableDrag={true}>
<div className="stop-map-modal">
{/* Map Container */}
<div className="stop-map-modal__map-container">
{styleSpec && (
<Map
mapLib={maplibregl as any}
initialViewState={{
latitude: center.latitude,
longitude: center.longitude,
zoom: 16,
}}
style={{ width: "100%", height: "50vh" }}
mapStyle={styleSpec}
attributionControl={{ compact: false, customAttribution: "Concello de Vigo & Viguesa de Transportes SL" }}
ref={mapRef}
interactive={true}
onMove={(e) => {
if (e.originalEvent) {
userInteracted.current = true;
}
}}
onDragStart={() => {
userInteracted.current = true;
}}
onZoomStart={() => {
userInteracted.current = true;
}}
onRotateStart={() => {
userInteracted.current = true;
}}
onPitchStart={() => {
userInteracted.current = true;
}}
>
{/* Previous Shape Layer */}
{previousShapeData && selectedBus && (
<Source
id="prev-route-shape"
type="geojson"
data={previousShapeData}
>
{/* 1. Black border */}
<Layer
id="prev-route-shape-border"
type="line"
paint={{
"line-color": "#000000",
"line-width": 6,
"line-opacity": 0.8,
}}
layout={{
"line-cap": "round",
"line-join": "round",
}}
/>
{/* 2. White background */}
<Layer
id="prev-route-shape-white"
type="line"
paint={{
"line-color": "#FFFFFF",
"line-width": 4,
}}
layout={{
"line-cap": "round",
"line-join": "round",
}}
/>
{/* 3. Colored dashes */}
<Layer
id="prev-route-shape-inner"
type="line"
paint={{
"line-color": getLineColour(selectedBus.line)
.background,
"line-width": 4,
"line-dasharray": [2, 2],
}}
layout={{
"line-cap": "round",
"line-join": "round",
}}
/>
</Source>
)}
{/* Shape Layer */}
{shapeData && selectedBus && (
<Source id="route-shape" type="geojson" data={shapeData}>
<Layer
id="route-shape-border"
type="line"
paint={{
"line-color": "#000000",
"line-width": 5,
"line-opacity": 0.6,
}}
layout={{
"line-cap": "round",
"line-join": "round",
}}
/>
<Layer
id="route-shape-inner"
type="line"
paint={{
"line-color": getLineColour(selectedBus.line)
.background,
"line-width": 3,
"line-opacity": 0.7,
}}
layout={{
"line-cap": "round",
"line-join": "round",
}}
/>
</Source>
)}
{/* Stop marker */}
{stop.latitude && stop.longitude && (
<Marker
longitude={stop.longitude}
latitude={stop.latitude}
anchor="bottom"
>
<div title={`Stop ${stop.stopId}`}>
<svg width="28" height="36" viewBox="0 0 28 36">
<defs>
<filter
id="drop-stop"
x="-20%"
y="-20%"
width="140%"
height="140%"
>
<feDropShadow
dx="0"
dy="1"
stdDeviation="1"
floodOpacity={0.35}
/>
</filter>
</defs>
<path
d="M14 0C6.82 0 1 5.82 1 13c0 8.5 11 23 13 23s13-14.5 13-23C27 5.82 21.18 0 14 0z"
fill="#1976d2"
stroke="#fff"
strokeWidth="2"
filter="url(#drop-stop)"
/>
<circle cx="14" cy="13" r="5" fill="#fff" />
<circle cx="14" cy="13" r="3" fill="#1976d2" />
</svg>
</div>
</Marker>
)}
{/* Selected bus marker */}
{selectedBus?.currentPosition && (
<Marker
longitude={selectedBus.currentPosition.longitude}
latitude={selectedBus.currentPosition.latitude}
anchor="center"
>
<div
style={{
display: "flex",
flexDirection: "column",
alignItems: "center",
gap: 6,
transform: `rotate(${selectedBus.currentPosition.orientationDegrees}deg)`,
transformOrigin: "center center",
}}
>
<svg
width="24"
height="24"
viewBox="0 0 24 24"
style={{
filter: "drop-shadow(0 2px 4px rgba(0,0,0,0.3))",
}}
>
<path
d="M12 2 L22 22 L12 17 L2 22 Z"
fill={getLineColour(selectedBus.line).background}
stroke="#000"
strokeWidth="2"
strokeLinejoin="round"
/>
</svg>
</div>
</Marker>
)}
</Map>
)}
{/* Floating controls */}
<div className="map-modal-controls">
<button
type="button"
aria-label="Center"
className="center-btn"
onClick={() => {
userInteracted.current = false;
handleCenter();
}}
title="Center view"
>
<svg
width="20"
height="20"
viewBox="0 0 24 24"
aria-hidden="true"
>
<circle cx="12" cy="12" r="3" fill="currentColor" />
<path
d="M12 2v3M12 19v3M2 12h3M19 12h3"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
/>
<circle
cx="12"
cy="12"
r="8"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
/>
</svg>
</button>
</div>
</div>
</div>
</Sheet.Content>
</Sheet.Container>
<Sheet.Backdrop onClick={onClose} />
</Sheet>
);
};
|