-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
585 lines (546 loc) · 23.6 KB
/
Copy pathindex.js
File metadata and controls
585 lines (546 loc) · 23.6 KB
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
import React, { useState, useRef, useEffect } from "react";
import Button from "react-bootstrap/Button";
import Form from "react-bootstrap/Form";
import "../../styles/BufferAnalysisWindow.scss";
const BufferAnalysisWindow = ({
show,
onClose,
bufferResults,
bufferRadius,
onRadiusChange,
isBufferActive,
onActivateBuffer,
bufferCenter,
selectedMunicipality,
onBlueBikeStationHover,
onCommuterRailStationHover,
onSubwayStationHover,
onClearBuffer,
onZoomToBuffer,
// Layer visibility states
showCommuterRail,
showBlueBikeStations,
showSubwayStations,
onToggleCommuterRail,
onToggleBlueBikeStations,
onToggleSubwayStations
}) => {
const [position, setPosition] = useState({ x: 20, y: 100 });
const [isDragging, setIsDragging] = useState(false);
const [dragOffset, setDragOffset] = useState({ x: 0, y: 0 });
const [isCollapsed, setIsCollapsed] = useState(false);
const windowRef = useRef(null);
const MAX_RADIUS_MILES = 1.5; // Maximum radius in miles
const MIN_RADIUS_MILES = 0.1; // Minimum radius in miles
const STEP_RADIUS_MILES = 0.1; // Step size in miles
// Convert miles to meters for internal calculations
const milesToMeters = (miles) => miles * 1609.34;
// Convert meters to miles for display
const metersToMiles = (meters) => meters / 1609.34;
// Reset position when municipality changes
useEffect(() => {
if (selectedMunicipality) {
setPosition({ x: 20, y: 100 });
setIsCollapsed(false);
}
}, [selectedMunicipality]);
// Ensure window is expanded when it's shown
useEffect(() => {
if (show) {
setIsCollapsed(false);
}
}, [show]);
const handleMouseDown = (e) => {
if (e.target.closest('.BufferAnalysisWindow__header') &&
!e.target.closest('.BufferAnalysisWindow__close') &&
!e.target.closest('.BufferAnalysisWindow__collapse')) {
setIsDragging(true);
setDragOffset({
x: e.clientX - position.x,
y: e.clientY - position.y
});
}
};
useEffect(() => {
const handleMouseMove = (e) => {
if (isDragging && windowRef.current) {
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
// Get actual window dimensions from the DOM element
const rect = windowRef.current.getBoundingClientRect();
const bufferWindowWidth = rect.width;
const bufferWindowHeight = rect.height;
// Calculate new position with boundary constraints
let newX = e.clientX - dragOffset.x;
let newY = e.clientY - dragOffset.y;
// Constrain to viewport boundaries
newX = Math.max(0, Math.min(newX, windowWidth - bufferWindowWidth));
newY = Math.max(0, Math.min(newY, windowHeight - bufferWindowHeight));
setPosition({
x: newX,
y: newY
});
}
};
const handleMouseUp = () => {
setIsDragging(false);
};
if (isDragging) {
document.addEventListener('mousemove', handleMouseMove);
document.addEventListener('mouseup', handleMouseUp);
}
return () => {
document.removeEventListener('mousemove', handleMouseMove);
document.removeEventListener('mouseup', handleMouseUp);
};
}, [isDragging, dragOffset]);
const formatDistance = (meters) => {
if (!meters && meters !== 0) return "N/A";
const feet = meters * 3.28084;
if (feet < 5280) {
return `${Math.round(feet).toLocaleString()} ft`;
} else {
const miles = feet / 5280;
return `${miles.toFixed(2)} mi`;
}
};
const formatDistanceMeters = (meters) => {
if (!meters && meters !== 0) return "N/A";
// Convert meters to miles
const miles = meters / 1609.34;
return `${miles.toFixed(2)} mi`;
};
const handleClose = () => {
// Clear buffer if there is one
if (bufferCenter || bufferResults) {
if (onClearBuffer) {
onClearBuffer();
}
}
// Close the window
if (onClose) {
onClose();
}
};
if (!show) return null;
return (
<div
ref={windowRef}
className={`BufferAnalysisWindow ${isCollapsed ? 'BufferAnalysisWindow--collapsed' : ''}`}
style={{
left: `${position.x}px`,
top: `${position.y}px`,
cursor: isDragging ? 'grabbing' : 'default'
}}
>
{/* Header */}
<div
className="BufferAnalysisWindow__header"
onMouseDown={handleMouseDown}
style={{ cursor: isDragging ? 'grabbing' : 'grab' }}
>
<div className="d-flex align-items-center justify-content-between w-100">
<div className="d-flex align-items-center">
<span className="fw-bold">Buffer Analysis</span>
</div>
<div className="d-flex align-items-center gap-1">
{bufferCenter && onZoomToBuffer && (
<button
className="BufferAnalysisWindow__zoom btn btn-sm p-1"
onClick={onZoomToBuffer}
title="Zoom to Buffer"
style={{ fontSize: '12px' }}
>
<i className="fas fa-search"></i>
</button>
)}
<button
className="BufferAnalysisWindow__collapse btn btn-sm p-0"
onClick={() => setIsCollapsed(!isCollapsed)}
title={isCollapsed ? "Expand" : "Collapse"}
>
{isCollapsed ? '▲' : '▼'}
</button>
<button
className="BufferAnalysisWindow__close btn btn-sm p-0"
onClick={handleClose}
title="Close"
>
×
</button>
</div>
</div>
</div>
{/* Body */}
{!isCollapsed && (
<div className="BufferAnalysisWindow__body">
{/* Controls Section */}
<div className="BufferAnalysisWindow__controls mb-3">
<div className="small fw-semibold mb-2">Create Buffer Zone</div>
<Form.Group className="mb-2">
<div className="d-flex justify-content-between align-items-center mb-1">
<Form.Label className="small mb-0">
Radius: <strong>{metersToMiles(bufferRadius).toFixed(2)} mi</strong>
</Form.Label>
</div>
<Form.Range
min={MIN_RADIUS_MILES * 10}
max={MAX_RADIUS_MILES * 10}
step={STEP_RADIUS_MILES * 10}
value={metersToMiles(bufferRadius) * 10}
onChange={(e) => {
const miles = parseFloat(e.target.value) / 10;
onRadiusChange(Math.round(milesToMeters(miles)));
}}
size="sm"
/>
<div className="d-flex justify-content-between small text-muted" style={{ fontSize: '0.7rem' }}>
<span>{MIN_RADIUS_MILES} mi</span>
<span>{MAX_RADIUS_MILES} mi</span>
</div>
</Form.Group>
<div className="d-flex gap-2">
<Button
variant={isBufferActive ? "danger" : "success"}
size="sm"
className="flex-grow-1"
onClick={() => {
console.log('🖱️ Buffer button clicked. Current state:', { isBufferActive, bufferCenter });
onActivateBuffer();
}}
>
{isBufferActive ? "Cancel Drawing" : bufferCenter ? "Draw New Buffer" : "Click to Start Drawing"}
</Button>
{bufferCenter && !isBufferActive && (
<Button
variant="outline-danger"
size="sm"
onClick={() => onActivateBuffer(true)} // Clear
title="Clear buffer"
>
✕
</Button>
)}
</div>
{isBufferActive && (
<div className="alert alert-success mt-2 mb-0 p-2 small">
<div className="d-flex align-items-center">
<div>
<strong>Drawing Mode Active!</strong><br/>
Click anywhere on the map to draw a {metersToMiles(bufferRadius).toFixed(2)} mi radius circle
</div>
</div>
</div>
)}
{!isBufferActive && !bufferCenter && (
<div className="alert alert-light mt-2 mb-0 p-2 small border">
<div className="text-muted small">
<i className="fas fa-lightbulb me-1"></i>Adjust the radius above, then click "Start Drawing" to create a buffer zone
</div>
</div>
)}
</div>
{/* Results Section */}
{bufferResults && bufferCenter && (
<div className="BufferAnalysisWindow__results">
<div className="small fw-semibold mb-2">Analysis Results</div>
{/* Buffer Center */}
<div className="BufferAnalysisWindow__center mb-2 p-2 border rounded bg-light">
<div className="small text-muted">Buffer Center</div>
<code className="small">
{bufferCenter.lat.toFixed(6)}, {bufferCenter.lng.toFixed(6)}
</code>
</div>
{/* Summary Stats */}
<div className="BufferAnalysisWindow__summary mb-3 p-2 border rounded">
<div className="row g-2 small">
<div className="col-2 text-center">
<div className="text-muted" style={{ fontSize: '0.7rem' }}>Trails</div>
<div className="fw-bold fs-5">
{bufferResults.trails.length}
</div>
</div>
<div className="col-2 text-center">
<div className="text-muted" style={{ fontSize: '0.7rem' }}>Rail Stations</div>
<div className="fw-bold fs-5">
{bufferResults.stations.length}
</div>
</div>
<div className="col-2 text-center">
<div className="text-muted" style={{ fontSize: '0.7rem', lineHeight: '1.0' }}>Blue Bike<br/>Stations</div>
<div className="fw-bold fs-5">
{bufferResults.bikeStations ? bufferResults.bikeStations.length : 0}
</div>
</div>
<div className="col-2 text-center">
<div className="text-muted" style={{ fontSize: '0.7rem' }}>T-stops</div>
<div className="fw-bold fs-5">
{bufferResults.subwayStations ? bufferResults.subwayStations.length : 0}
</div>
</div>
<div className="col-2 text-center">
<div className="text-muted" style={{ fontSize: '0.7rem' }}>Area</div>
<div className="fw-bold" style={{ fontSize: '0.8rem' }}>
{(Math.PI * Math.pow(metersToMiles(bufferRadius), 2)).toFixed(2)} mi²
</div>
</div>
</div>
</div>
{/* Trails by Type */}
<div className="mb-3">
<div className="small fw-semibold mb-2">Trail Types (Counts)</div>
{bufferResults.trails.length === 0 ? (
<div className="small text-muted text-center p-2 border rounded">
No trails found
</div>
) : (
<div className="BufferAnalysisWindow__trail-types">
{(() => {
// Group trails by type
const trailsByType = {};
bufferResults.trails.forEach((trail) => {
if (!trailsByType[trail.type]) {
trailsByType[trail.type] = {
count: 0,
color: trail.color
};
}
trailsByType[trail.type].count++;
});
return Object.entries(trailsByType)
.sort(([, a], [, b]) => b.count - a.count)
.map(([type, data]) => (
<div key={type} className="BufferAnalysisWindow__type-badge">
<div className="type-name small text-muted mb-1" title={type}>
{type}
</div>
<div
className="type-circle d-flex align-items-center justify-content-center fw-bold"
style={{
backgroundColor: data.color || '#6c757d',
color: '#ffffff'
}}
title={`${data.count} ${type} trails`}
>
{data.count}
</div>
</div>
));
})()}
</div>
)}
</div>
{/* Commuter Rail Stations in Buffer */}
<div className="mb-2">
{/* Distance Explanation */}
<div className="alert alert-info mb-2 p-2 small" style={{ fontSize: '0.75rem', lineHeight: '1.3' }}>
<i className="fas fa-info-circle me-1"></i>
<strong>Distance:</strong> Straight-line distance from buffer center to each station, measured in miles.
</div>
<div className="d-flex justify-content-between align-items-center mb-2">
<div className="d-flex align-items-center">
<Form.Check
type="checkbox"
id="commuter-rail-checkbox"
checked={showCommuterRail}
onChange={(e) => {
if (onToggleCommuterRail) {
onToggleCommuterRail(e.target.checked);
}
}}
className="me-2"
style={{ transform: 'scale(0.8)' }}
/>
<span className="small fw-semibold">Commuter Rail Stations ({bufferResults.stations.length})</span>
</div>
</div>
{!showCommuterRail ? (
<div className="small text-muted text-center p-2 border rounded bg-light">
<i className="fas fa-info-circle me-1"></i>
Please open the layer to see calculation result
</div>
) : bufferResults.stations.length === 0 ? (
<div className="small text-muted text-center p-2 border rounded">
No stations found
</div>
) : (
<div className="BufferAnalysisWindow__list">
{bufferResults.stations.slice(0, 5).map((station, idx) => (
<div
key={idx}
className="BufferAnalysisWindow__item p-2 mb-1 border rounded"
onMouseEnter={() => {
if (onCommuterRailStationHover) {
onCommuterRailStationHover(station);
}
}}
onMouseLeave={() => {
if (onCommuterRailStationHover) {
onCommuterRailStationHover(null);
}
}}
>
<div className="d-flex justify-content-between align-items-start">
<div className="flex-grow-1" style={{ minWidth: 0 }}>
<div className="small fw-semibold text-truncate" title={station.name}>
{station.name}
</div>
<div className="small text-muted" style={{ fontSize: '0.7rem' }}>
{station.line}
</div>
</div>
<div className="text-end ms-2" style={{ minWidth: '60px' }}>
<div className="small fw-bold text-primary" style={{ fontSize: '0.75rem' }}>
{formatDistanceMeters(station.distance)}
</div>
</div>
</div>
</div>
))}
{bufferResults.stations.length > 5 && (
<div className="small text-muted text-center">
+{bufferResults.stations.length - 5} more
</div>
)}
</div>
)}
</div>
{/* Blue Bike Stations in Buffer */}
<div className="mb-2">
<div className="d-flex justify-content-between align-items-center mb-2">
<div className="d-flex align-items-center">
<Form.Check
type="checkbox"
id="blue-bike-checkbox"
checked={showBlueBikeStations}
onChange={(e) => {
if (onToggleBlueBikeStations) {
onToggleBlueBikeStations(e.target.checked);
}
}}
className="me-2"
style={{ transform: 'scale(0.8)' }}
/>
<span className="small fw-semibold">Blue Bike Stations ({bufferResults.bikeStations ? bufferResults.bikeStations.length : 0})</span>
</div>
</div>
{!showBlueBikeStations ? (
<div className="small text-muted text-center p-2 border rounded bg-light">
<i className="fas fa-info-circle me-1"></i>
Please open the layer to see calculation result
</div>
) : bufferResults.bikeStations && bufferResults.bikeStations.length > 0 ? (
<div className="BufferAnalysisWindow__list BufferAnalysisWindow__bike-stations">
{bufferResults.bikeStations.map((station, idx) => (
<div
key={idx}
className="BufferAnalysisWindow__item p-2 mb-1 border rounded"
onMouseEnter={() => {
if (onBlueBikeStationHover) {
onBlueBikeStationHover(station);
}
}}
onMouseLeave={() => {
if (onBlueBikeStationHover) {
onBlueBikeStationHover(null);
}
}}
>
<div className="d-flex justify-content-between align-items-start">
<div className="flex-grow-1" style={{ minWidth: 0 }}>
<div className="small fw-semibold text-truncate" title={station.name}>
{station.name}
</div>
<div className="small text-muted" style={{ fontSize: '0.7rem' }}>
{station.district} • {station.totalDocks} docks
</div>
</div>
<div className="text-end ms-2" style={{ minWidth: '60px' }}>
<div className="small fw-bold text-primary" style={{ fontSize: '0.75rem' }}>
{formatDistanceMeters(station.distance)}
</div>
</div>
</div>
</div>
))}
</div>
) : (
<div className="small text-muted text-center p-2 border rounded">
No stations found
</div>
)}
</div>
{/* Subway Stations in Buffer */}
<div className="mb-2">
<div className="d-flex justify-content-between align-items-center mb-2">
<div className="d-flex align-items-center">
<Form.Check
type="checkbox"
id="subway-checkbox"
checked={showSubwayStations}
onChange={(e) => {
if (onToggleSubwayStations) {
onToggleSubwayStations(e.target.checked);
}
}}
className="me-2"
style={{ transform: 'scale(0.8)' }}
/>
<span className="small fw-semibold">T-stops ({bufferResults.subwayStations ? bufferResults.subwayStations.length : 0})</span>
</div>
</div>
{!showSubwayStations ? (
<div className="small text-muted text-center p-2 border rounded bg-light">
<i className="fas fa-info-circle me-1"></i>
Please open the layer to see calculation result
</div>
) : bufferResults.subwayStations && bufferResults.subwayStations.length > 0 ? (
<div className="BufferAnalysisWindow__list BufferAnalysisWindow__subway-stations">
{bufferResults.subwayStations.map((station, idx) => (
<div
key={idx}
className="BufferAnalysisWindow__item p-2 mb-1 border rounded"
onMouseEnter={() => {
if (onSubwayStationHover) {
onSubwayStationHover(station);
}
}}
onMouseLeave={() => {
if (onSubwayStationHover) {
onSubwayStationHover(null);
}
}}
>
<div className="d-flex justify-content-between align-items-start">
<div className="flex-grow-1" style={{ minWidth: 0 }}>
<div className="small fw-semibold text-truncate" title={station.name}>
{station.name}
</div>
<div className="small text-muted" style={{ fontSize: '0.7rem' }}>
{station.line} Line
</div>
</div>
<div className="text-end ms-2" style={{ minWidth: '60px' }}>
<div className="small fw-bold text-primary" style={{ fontSize: '0.75rem' }}>
{formatDistanceMeters(station.distance)}
</div>
</div>
</div>
</div>
))}
</div>
) : (
<div className="small text-muted text-center p-2 border rounded">
No stations found
</div>
)}
</div>
</div>
)}
{/* Empty State - removed as we now show instructions in controls */}
</div>
)}
</div>
);
};
export default BufferAnalysisWindow;