Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
3041 lines (2604 loc) Β· 120 KB
/
Copy pathscript.js
File metadata and controls
3041 lines (2604 loc) Β· 120 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
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
class DisasterMap {
constructor() {
this.map = null;
this.markers = [];
this.affectedAreas = [];
this.impactZones = [];
this.impactZoneLayer = null;
this.disasterEvents = [];
this.filteredEvents = [];
this.selectedEventId = null;
this.showAffectedAreas = true;
this.showImpactZones = false;
this.healthFacilityMarkers = [];
this.healthFacilities = [];
this.showHealthFacilities = false;
this.currentCountryFilter = null; // Track current country filter
this.currentTypeFilter = null; // Track current facility type filter
this.healthFacilitiesCluster = null; // Cluster group for health facilities
this.csvHealthFacilityMarkers = [];
this.csvHealthFacilities = [];
this.shapefileHealthFacilities = [];
this.showOtherHealthFacilities = false;
this.selectedHealthCountries = []; // Health facilities country filter (multi-select)
this.selectedHealthFunctionality = ''; // Health facilities functionality filter
this.selectedImpactFacilityCountry = ''; // Impact facilities country filter
this.isFullscreen = false; // Fullscreen mode state
this.baseLayer = null; // OpenStreetMap base layer
this.satelliteLayer = null; // Satellite imagery layer
this.showSatelliteLayer = false; // Satellite layer visibility
this.satelliteDateCaption = null; // Date caption control for satellite imagery
this.isSatelliteDateCaptionVisible = false; // Track if caption is currently displayed
this.clusteringEnabled = true; // Control clustering state
this.disableClusteringOnCountryFilter = true; // Disable clustering when filtering by country
this.facilityTypeVisibility = {
'Primary Health Care Centres': true,
'Primary Health Care Center': true,
'Ambulance Stations': true,
'Ambulance Station': true,
'Blood Centres': true,
'Blood Center': true,
'Hospitals': true,
'Hospital': true,
'Pharmacies': true,
'Pharmacy': true,
'Training Facilities': true,
'Training Facility': true,
'Specialized Services': true,
'Residential Facilities': true,
'Residential Facility': true,
'Other': true
};
this.disasterTypeVisibility = {
'Earthquake': true,
'Flood': true,
'Cyclone': true,
'Wildfire': true,
'Volcanic Activity': true,
'Other': true
};
this.activeERUs = [];
this.eruMarkers = [];
this.showActiveERUs = false;
this.init();
}
init() {
this.initMap();
this.initEventListeners();
this.loadDisasterData();
this.loadImpactZones();
this.loadHealthFacilities();
this.loadActiveERUs();
// Removed: CSV and shapefile loading - now using IFRC API only
// this.loadCsvHealthFacilities();
// this.loadShapefileHealthFacilities();
// Create fallback satellite date caption
this.createSatelliteDateCaption('ArcGIS World Imagery');
}
getRegionForCountry(country) {
const regionMap = {
// North America
'United States': 'North America',
'Canada': 'North America',
'Mexico': 'North America',
// Central America
'Guatemala': 'Central America',
'Belize': 'Central America',
'Honduras': 'Central America',
'El Salvador': 'Central America',
'Nicaragua': 'Central America',
'Costa Rica': 'Central America',
'Panama': 'Central America',
// South America
'Brazil': 'South America',
'Argentina': 'South America',
'Chile': 'South America',
'Peru': 'South America',
'Colombia': 'South America',
'Venezuela': 'South America',
'Ecuador': 'South America',
'Bolivia': 'South America',
'Paraguay': 'South America',
'Uruguay': 'South America',
'Guyana': 'South America',
'Suriname': 'South America',
'French Guiana': 'South America',
// Africa
'Nigeria': 'Africa',
'South Africa': 'Africa',
'Kenya': 'Africa',
'Egypt': 'Africa',
'Morocco': 'Africa',
'Algeria': 'Africa',
'Tunisia': 'Africa',
'Libya': 'Africa',
'Sudan': 'Africa',
'South Sudan': 'Africa',
'Ethiopia': 'Africa',
'Somalia': 'Africa',
'Uganda': 'Africa',
'Tanzania': 'Africa',
'Rwanda': 'Africa',
'Burundi': 'Africa',
'Democratic Republic of the Congo': 'Africa',
'Republic of the Congo': 'Africa',
'Central African Republic': 'Africa',
'Chad': 'Africa',
'Niger': 'Africa',
'Mali': 'Africa',
'Burkina Faso': 'Africa',
'Ghana': 'Africa',
'Ivory Coast': 'Africa',
'Liberia': 'Africa',
'Sierra Leone': 'Africa',
'Guinea': 'Africa',
'Senegal': 'Africa',
'Mauritania': 'Africa',
'Gambia': 'Africa',
'Guinea-Bissau': 'Africa',
'Cape Verde': 'Africa',
'Cameroon': 'Africa',
'Equatorial Guinea': 'Africa',
'Gabon': 'Africa',
'SΓ£o TomΓ© and PrΓncipe': 'Africa',
'Angola': 'Africa',
'Zambia': 'Africa',
'Zimbabwe': 'Africa',
'Botswana': 'Africa',
'Namibia': 'Africa',
'Lesotho': 'Africa',
'Eswatini': 'Africa',
'Madagascar': 'Africa',
'Mauritius': 'Africa',
'Seychelles': 'Africa',
'Comoros': 'Africa',
'Djibouti': 'Africa',
'Eritrea': 'Africa',
'Mozambique': 'Africa',
'Malawi': 'Africa',
// Middle East
'Saudi Arabia': 'Middle East',
'Iran': 'Middle East',
'Iraq': 'Middle East',
'Israel': 'Middle East',
'Palestine': 'Middle East',
'Jordan': 'Middle East',
'Lebanon': 'Middle East',
'Syria': 'Middle East',
'Turkey': 'Middle East',
'United Arab Emirates': 'Middle East',
'Kuwait': 'Middle East',
'Qatar': 'Middle East',
'Bahrain': 'Middle East',
'Oman': 'Middle East',
'Yemen': 'Middle East',
'Cyprus': 'Middle East',
// Europe
'Germany': 'Europe',
'France': 'Europe',
'Italy': 'Europe',
'Spain': 'Europe',
'Poland': 'Europe',
'Romania': 'Europe',
'Netherlands': 'Europe',
'Belgium': 'Europe',
'Greece': 'Europe',
'Portugal': 'Europe',
'Czech Republic': 'Europe',
'Hungary': 'Europe',
'Sweden': 'Europe',
'Austria': 'Europe',
'Belarus': 'Europe',
'Switzerland': 'Europe',
'Bulgaria': 'Europe',
'Serbia': 'Europe',
'Denmark': 'Europe',
'Finland': 'Europe',
'Slovakia': 'Europe',
'Norway': 'Europe',
'Ireland': 'Europe',
'Croatia': 'Europe',
'Bosnia and Herzegovina': 'Europe',
'Albania': 'Europe',
'Lithuania': 'Europe',
'Slovenia': 'Europe',
'Latvia': 'Europe',
'Estonia': 'Europe',
'Moldova': 'Europe',
'Macedonia': 'Europe',
'North Macedonia': 'Europe',
'Luxembourg': 'Europe',
'Malta': 'Europe',
'Iceland': 'Europe',
'Monaco': 'Europe',
'Liechtenstein': 'Europe',
'San Marino': 'Europe',
'Vatican': 'Europe',
'Montenegro': 'Europe',
'Kosovo': 'Europe',
'United Kingdom': 'Europe',
'Ukraine': 'Europe',
'Russia': 'Europe',
// Central Asia
'Kazakhstan': 'Central Asia',
'Uzbekistan': 'Central Asia',
'Turkmenistan': 'Central Asia',
'Tajikistan': 'Central Asia',
'Kyrgyzstan': 'Central Asia',
'Afghanistan': 'Central Asia',
'Mongolia': 'Central Asia',
// Asia
'China': 'Asia',
'India': 'Asia',
'Indonesia': 'Asia',
'Pakistan': 'Asia',
'Bangladesh': 'Asia',
'Japan': 'Asia',
'Philippines': 'Asia',
'Vietnam': 'Asia',
'Turkey': 'Asia', // Could be Middle East or Asia - keeping Middle East above
'Thailand': 'Asia',
'Myanmar': 'Asia',
'South Korea': 'Asia',
'Malaysia': 'Asia',
'Nepal': 'Asia',
'Sri Lanka': 'Asia',
'Cambodia': 'Asia',
'Jordan': 'Asia', // Could be Middle East - keeping Middle East above
'Laos': 'Asia',
'Singapore': 'Asia',
'Oman': 'Asia', // Could be Middle East - keeping Middle East above
'Lebanon': 'Asia', // Could be Middle East - keeping Middle East above
'Bhutan': 'Asia',
'Brunei': 'Asia',
'Maldives': 'Asia',
'North Korea': 'Asia',
'East Timor': 'Asia',
// Oceania
'Australia': 'Oceania',
'Papua New Guinea': 'Oceania',
'New Zealand': 'Oceania',
'Fiji': 'Oceania',
'Solomon Islands': 'Oceania',
'Vanuatu': 'Oceania',
'Samoa': 'Oceania',
'Micronesia': 'Oceania',
'Tonga': 'Oceania',
'Kiribati': 'Oceania',
'Palau': 'Oceania',
'Marshall Islands': 'Oceania',
'Tuvalu': 'Oceania',
'Nauru': 'Oceania'
};
return regionMap[country] || 'Other';
}
getCountryCoordinates(country) {
const countryCoords = {
// Major countries with approximate center coordinates and zoom levels
'Afghanistan': { lat: 33.0, lng: 65.0, zoom: 6 },
'Algeria': { lat: 28.0, lng: 3.0, zoom: 5 },
'Angola': { lat: -12.5, lng: 18.5, zoom: 6 },
'Argentina': { lat: -38.4, lng: -63.6, zoom: 5 },
'Australia': { lat: -25.3, lng: 133.8, zoom: 5 },
'Austria': { lat: 47.5, lng: 14.6, zoom: 7 },
'Bangladesh': { lat: 23.7, lng: 90.4, zoom: 7 },
'Belgium': { lat: 50.5, lng: 4.5, zoom: 8 },
'Bolivia': { lat: -16.3, lng: -63.6, zoom: 6 },
'Brazil': { lat: -14.2, lng: -51.9, zoom: 5 },
'Canada': { lat: 56.1, lng: -106.3, zoom: 4 },
'Chad': { lat: 15.5, lng: 19.0, zoom: 6 },
'Chile': { lat: -35.7, lng: -71.5, zoom: 5 },
'China': { lat: 35.9, lng: 104.2, zoom: 4 },
'Colombia': { lat: 4.6, lng: -74.1, zoom: 6 },
'Czech Republic': { lat: 49.8, lng: 15.5, zoom: 7 },
'Democratic Republic of Congo': { lat: -4.0, lng: 21.8, zoom: 5 },
'Ecuador': { lat: -1.8, lng: -78.2, zoom: 7 },
'Egypt': { lat: 26.8, lng: 30.8, zoom: 6 },
'Ethiopia': { lat: 9.1, lng: 40.5, zoom: 6 },
'Finland': { lat: 61.9, lng: 25.7, zoom: 6 },
'France': { lat: 46.6, lng: 2.2, zoom: 6 },
'Germany': { lat: 51.2, lng: 10.4, zoom: 6 },
'Ghana': { lat: 7.9, lng: -1.0, zoom: 7 },
'Greece': { lat: 39.1, lng: 21.8, zoom: 7 },
'Guatemala': { lat: 15.8, lng: -90.2, zoom: 7 },
'Haiti': { lat: 18.9, lng: -72.3, zoom: 8 },
'Honduras': { lat: 15.2, lng: -86.2, zoom: 7 },
'India': { lat: 20.6, lng: 78.9, zoom: 5 },
'Indonesia': { lat: -0.8, lng: 113.9, zoom: 5 },
'Iran, Islamic Republic of': { lat: 32.4, lng: 53.7, zoom: 6 },
'Iraq': { lat: 33.2, lng: 43.7, zoom: 6 },
'Italy': { lat: 41.9, lng: 12.6, zoom: 6 },
'Japan': { lat: 36.2, lng: 138.3, zoom: 6 },
'Jordan': { lat: 30.6, lng: 36.2, zoom: 7 },
'Kenya': { lat: -0.0, lng: 37.9, zoom: 6 },
'Libya': { lat: 26.3, lng: 17.2, zoom: 6 },
'Madagascar': { lat: -18.8, lng: 47.0, zoom: 6 },
'Mali': { lat: 17.6, lng: -4.0, zoom: 6 },
'Mexico': { lat: 23.6, lng: -102.6, zoom: 5 },
'Morocco': { lat: 31.8, lng: -7.1, zoom: 6 },
'Myanmar': { lat: 21.9, lng: 95.9, zoom: 6 },
'Nepal': { lat: 28.4, lng: 84.1, zoom: 7 },
'Niger': { lat: 17.6, lng: 8.1, zoom: 6 },
'Nigeria': { lat: 9.1, lng: 8.7, zoom: 6 },
'Norway': { lat: 60.5, lng: 8.5, zoom: 5 },
'Pakistan': { lat: 30.4, lng: 69.3, zoom: 6 },
'Palestine': { lat: 31.9, lng: 35.2, zoom: 8 },
'Paraguay': { lat: -23.4, lng: -58.4, zoom: 6 },
'Peru': { lat: -9.2, lng: -75.0, zoom: 6 },
'Philippines': { lat: 12.9, lng: 121.8, zoom: 6 },
'Poland': { lat: 51.9, lng: 19.1, zoom: 6 },
'Portugal': { lat: 39.4, lng: -8.2, zoom: 7 },
'Romania': { lat: 45.9, lng: 24.9, zoom: 6 },
'Russian Federation': { lat: 61.5, lng: 105.3, zoom: 3 },
'Rwanda': { lat: -1.9, lng: 29.9, zoom: 8 },
'Saudi Arabia': { lat: 23.9, lng: 45.1, zoom: 6 },
'Somalia': { lat: 5.2, lng: 46.2, zoom: 6 },
'South Africa': { lat: -30.6, lng: 22.9, zoom: 6 },
'South Sudan': { lat: 6.9, lng: 31.3, zoom: 6 },
'Spain': { lat: 40.5, lng: -3.7, zoom: 6 },
'Sudan': { lat: 12.9, lng: 30.4, zoom: 6 },
'Sweden': { lat: 60.1, lng: 18.6, zoom: 5 },
'Turkey': { lat: 38.9, lng: 35.2, zoom: 6 },
'Uganda': { lat: 1.4, lng: 32.3, zoom: 7 },
'Ukraine': { lat: 48.4, lng: 31.2, zoom: 6 },
'United Kingdom': { lat: 55.4, lng: -3.4, zoom: 6 },
'United States': { lat: 39.8, lng: -98.6, zoom: 4 },
'Venezuela': { lat: 6.4, lng: -66.6, zoom: 6 },
'Yemen': { lat: 15.6, lng: 48.5, zoom: 7 },
'Zimbabwe': { lat: -19.0, lng: 29.2, zoom: 7 }
};
return countryCoords[country] || null;
}
initMap() {
this.map = L.map('map').setView([20, 0], 2);
// Create base layer (OpenStreetMap)
this.baseLayer = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Β© OpenStreetMap contributors',
maxZoom: 18,
});
// Create satellite layer (ArcGIS World Imagery)
this.satelliteLayer = L.tileLayer('https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', {
attribution: 'Tiles Β© Esri β Source: Esri, i-cubed, USDA, USGS, AEX, GeoEye, Getmapping, Aerogrid, IGN, IGP, UPR-EGP, and the GIS User Community',
maxZoom: 18,
});
// Add base layer by default
this.baseLayer.addTo(this.map);
// Initialize health facilities cluster group with dynamic clustering
this.initializeClusterGroup();
}
initializeClusterGroup() {
// Create cluster group with dynamic settings based on current state
const shouldDisableClustering = this.shouldDisableClustering();
this.healthFacilitiesCluster = L.markerClusterGroup({
maxClusterRadius: 50, // Reduce clustering distance for better separation
disableClusteringAtZoom: shouldDisableClustering ? 1 : 8, // Disable at zoom 1 (always) if clustering should be disabled, otherwise at zoom 8
spiderfyOnMaxZoom: true, // Spread markers when clicking cluster at max zoom
showCoverageOnHover: false, // Don't show cluster coverage area
zoomToBoundsOnClick: true, // Zoom to cluster bounds when clicked
iconCreateFunction: function(cluster) {
const count = cluster.getChildCount();
let className = 'marker-cluster-small';
if (count > 100) className = 'marker-cluster-large';
else if (count > 10) className = 'marker-cluster-medium';
return new L.DivIcon({
html: '<div><span>' + count + '</span></div>',
className: 'marker-cluster ' + className,
iconSize: new L.Point(40, 40)
});
}
});
// Add zoom event listener for dynamic clustering control
this.map.on('zoomend', () => {
this.handleZoomBasedClustering();
});
}
shouldDisableClustering() {
// Disable clustering if:
// 1. User has manually disabled clustering
// 2. User has selected specific countries and the setting is enabled
// 3. Zoom level is high enough
if (!this.clusteringEnabled) {
return true;
}
if (this.disableClusteringOnCountryFilter && this.selectedHealthCountries.length > 0) {
return true;
}
// Check zoom level (disable clustering at zoom 8+ for better detail)
const currentZoom = this.map ? this.map.getZoom() : 0;
if (currentZoom >= 8) {
return true;
}
return false;
}
handleZoomBasedClustering() {
// Only recreate cluster group if clustering state has changed
const shouldDisable = this.shouldDisableClustering();
const currentDisableZoom = this.healthFacilitiesCluster.options.disableClusteringAtZoom;
// If we need to change clustering behavior, recreate the cluster group
if ((shouldDisable && currentDisableZoom !== 1) || (!shouldDisable && currentDisableZoom === 1)) {
this.recreateClusterGroup();
}
}
recreateClusterGroup() {
// Store current markers
const currentMarkers = [];
this.healthFacilitiesCluster.eachLayer(layer => {
currentMarkers.push(layer);
});
// Remove old cluster group from map
if (this.map.hasLayer(this.healthFacilitiesCluster)) {
this.map.removeLayer(this.healthFacilitiesCluster);
}
// Reinitialize cluster group with new settings
this.initializeClusterGroup();
// Re-add all markers to new cluster group
currentMarkers.forEach(marker => {
this.healthFacilitiesCluster.addLayer(marker);
});
// Add cluster group back to map if health facilities are visible
if (this.showHealthFacilities) {
this.map.addLayer(this.healthFacilitiesCluster);
}
console.log(`π Recreated cluster group. Clustering ${this.shouldDisableClustering() ? 'DISABLED' : 'ENABLED'}`);
}
toggleClustering() {
this.clusteringEnabled = !this.clusteringEnabled;
this.recreateClusterGroup();
// Update UI button if it exists
const clusterBtn = document.getElementById('toggle-clustering-btn');
if (clusterBtn) {
clusterBtn.textContent = this.clusteringEnabled ? 'π Disable Clustering' : 'π Enable Clustering';
clusterBtn.title = this.clusteringEnabled ? 'Disable facility clustering' : 'Enable facility clustering';
}
console.log(`Clustering ${this.clusteringEnabled ? 'enabled' : 'disabled'} by user`);
}
createSatelliteDateCaption(dateText) {
// Remove existing caption if it exists
if (this.satelliteDateCaption && this.isSatelliteDateCaptionVisible) {
this.map.removeControl(this.satelliteDateCaption);
this.isSatelliteDateCaptionVisible = false;
}
// Create a custom Leaflet control for the date caption
const DateCaption = L.Control.extend({
onAdd: function(map) {
const div = L.DomUtil.create('div', 'satellite-date-caption');
div.innerHTML = dateText;
div.style.cssText = `
background: rgba(255, 255, 255, 0.9);
padding: 5px 10px;
border-radius: 4px;
font-size: 12px;
font-weight: bold;
color: #333;
box-shadow: 0 1px 5px rgba(0,0,0,0.4);
border: 1px solid #ccc;
pointer-events: none;
margin-bottom: 70px;
margin-right: 10px;
`;
return div;
},
onRemove: function(map) {
// Nothing to do here
}
});
this.satelliteDateCaption = new DateCaption({ position: 'bottomright' });
}
initEventListeners() {
document.getElementById('refreshData').addEventListener('click', () => {
this.loadDisasterData();
});
document.getElementById('alertLevel').addEventListener('change', (e) => {
this.filterByAlertLevel(e.target.value);
});
document.getElementById('showAffectedAreas').addEventListener('change', (e) => {
this.showAffectedAreas = e.target.checked;
this.toggleDisasterEvents();
// Hide/show the disaster events control box
const disasterControlsBox = document.getElementById('disasterEventsControls');
if (disasterControlsBox) {
disasterControlsBox.style.display = e.target.checked ? 'block' : 'none';
}
});
document.getElementById('showImpactZones').addEventListener('change', (e) => {
this.showImpactZones = e.target.checked;
this.toggleImpactZones();
});
document.getElementById('showHealthFacilities').addEventListener('change', (e) => {
this.showHealthFacilities = e.target.checked;
this.toggleHealthFacilities();
});
document.getElementById('showOtherHealthFacilities').addEventListener('change', (e) => {
this.showOtherHealthFacilities = e.target.checked;
this.toggleOtherHealthFacilities();
});
// Clustering toggle button
const clusteringBtn = document.getElementById('toggle-clustering-btn');
if (clusteringBtn) {
clusteringBtn.addEventListener('click', () => {
this.toggleClustering();
});
}
// Event search functionality
document.getElementById('eventSearch').addEventListener('input', (e) => {
this.searchEvents(e.target.value);
});
document.getElementById('clearEventSearch').addEventListener('click', () => {
this.clearEventSearch();
});
document.getElementById('clearEventSelection').addEventListener('click', () => {
this.clearEventSelection();
});
// Fullscreen toggle
document.getElementById('fullscreenToggle').addEventListener('click', () => {
this.toggleFullscreen();
});
// ESC key to exit fullscreen
document.addEventListener('keydown', (e) => {
if (e.key === 'Escape' && this.isFullscreen) {
this.toggleFullscreen();
}
});
// Satellite layer toggle
document.getElementById('showSatelliteLayer').addEventListener('change', (e) => {
this.showSatelliteLayer = e.target.checked;
this.toggleSatelliteLayer();
});
document.getElementById('showActiveERUs').addEventListener('change', (e) => {
this.showActiveERUs = e.target.checked;
this.toggleActiveERUs();
});
// Sidebar drawer toggle
const sidebarToggle = document.getElementById('toggleSidebar');
if (sidebarToggle) {
sidebarToggle.addEventListener('click', (e) => {
this.toggleSidebar();
});
}
// Drawer close button
const drawerCloseBtn = document.getElementById('drawerCloseBtn');
if (drawerCloseBtn) {
drawerCloseBtn.addEventListener('click', (e) => {
e.preventDefault();
e.stopPropagation();
this.toggleSidebar();
});
}
// Health controls collapse/expand toggle
const healthToggle = document.getElementById('toggleHealthControls');
if (healthToggle) {
healthToggle.addEventListener('click', (e) => {
this.toggleHealthControlsCollapse();
});
}
// Disaster controls collapse/expand toggle
const disasterToggle = document.getElementById('toggleDisasterControls');
if (disasterToggle) {
disasterToggle.addEventListener('click', (e) => {
this.toggleDisasterControlsCollapse();
});
}
// Map controls collapse/expand toggle
const mapControlsToggle = document.getElementById('toggleMapControls');
if (mapControlsToggle) {
mapControlsToggle.addEventListener('click', (e) => {
this.toggleMapControlsCollapse();
});
}
// Health facilities country filter (multi-select)
const healthCountryFilter = document.getElementById('healthCountryFilter');
if (healthCountryFilter) {
healthCountryFilter.addEventListener('change', (e) => {
this.selectedHealthCountries = Array.from(e.target.selectedOptions).map(option => option.value);
this.filterHealthFacilities();
});
}
// Health facilities functionality filter
const healthFunctionalityFilter = document.getElementById('healthFunctionalityFilter');
if (healthFunctionalityFilter) {
healthFunctionalityFilter.addEventListener('change', (e) => {
this.selectedHealthFunctionality = e.target.value;
this.filterHealthFacilities();
});
}
// Impact facilities country filter
const facilityCountryFilter = document.getElementById('facilityCountryFilter');
if (facilityCountryFilter) {
facilityCountryFilter.addEventListener('change', (e) => {
this.selectedImpactFacilityCountry = e.target.value;
console.log(`π Impact facility country filter changed to: "${this.selectedImpactFacilityCountry}"`);
this.updateImpactFacilitiesList();
this.updateCountryStats(this.selectedImpactFacilityCountry || null);
});
console.log('β
Impact facility country filter event listener added');
} else {
console.error('β facilityCountryFilter element not found!');
}
// Add event listeners for individual facility type checkboxes
this.initFacilityTypeListeners();
// Add event listeners for disaster type checkboxes
this.initDisasterTypeListeners();
// Note: initHealthFacilityCountryFilter() is called after health facilities are loaded
}
async loadDisasterData() {
try {
document.getElementById('eventList').innerHTML = '<div class="loading">Loading real disaster events from GDACS...</div>';
this.clearMarkers();
this.clearAffectedAreas();
const data = await this.fetchGDACSData();
this.disasterEvents = data;
this.displayEvents(data);
this.addMarkersToMap(data);
if (this.showAffectedAreas) {
this.addAffectedAreasToMap(data);
}
} catch (error) {
console.error('Error loading disaster data:', error);
document.getElementById('eventList').innerHTML = '<div class="loading">Error loading data from backend. Using sample data...</div>';
const sampleData = this.getSampleData();
this.disasterEvents = sampleData;
this.displayEvents(sampleData);
this.addMarkersToMap(sampleData);
if (this.showAffectedAreas) {
this.addAffectedAreasToMap(sampleData);
}
}
}
async loadDisasterDataWithDates(fromDate, toDate) {
try {
document.getElementById('eventList').innerHTML = '<div class="loading">Loading disaster events from GDACS...</div>';
this.clearMarkers();
this.clearAffectedAreas();
const events = await this.fetchGDACSDataWithDates(fromDate, toDate);
this.disasterEvents = events;
this.filteredEvents = [...events];
this.addMarkersToMap(events);
this.displayEvents(this.filteredEvents);
this.addAffectedAreasToMap(events);
console.log(`Loaded ${events.length} disaster events for date range ${fromDate} to ${toDate}`);
} catch (error) {
console.error('Error loading disaster data with dates:', error);
document.getElementById('eventList').innerHTML = '<div class="error">Error loading events. Please try again.</div>';
}
}
async fetchGDACSData() {
try {
// Use backend proxy to fetch GDACS data
const alertLevel = document.getElementById('alertLevel').value;
// Default to last 6 months of data for better disaster coverage
const toDate = new Date();
const fromDate = new Date();
fromDate.setMonth(fromDate.getMonth() - 6);
const params = new URLSearchParams({
source: 'ALL',
from: fromDate.toISOString().split('T')[0], // YYYY-MM-DD format
to: toDate.toISOString().split('T')[0],
...(alertLevel && { alertLevel })
});
const response = await fetch(`/api/disasters?${params}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.success && result.events) {
console.log(`Loaded ${result.count} disaster events from backend`);
return result.events;
} else {
throw new Error(result.error || 'No events received from backend');
}
} catch (error) {
console.error('Backend proxy fetch failed:', error);
throw error;
}
}
async fetchGDACSDataWithDates(fromDate, toDate) {
try {
// Use backend proxy to fetch GDACS data with custom dates
const alertLevel = document.getElementById('alertLevel').value;
const params = new URLSearchParams({
source: 'ALL',
from: fromDate,
to: toDate,
...(alertLevel && { alertLevel })
});
const response = await fetch(`/api/disasters?${params}`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const result = await response.json();
if (result.success && result.events) {
console.log(`Loaded ${result.count} disaster events from backend for date range ${fromDate} to ${toDate}`);
return result.events;
} else {
throw new Error(result.error || 'No events received');
}
} catch (error) {
console.error('Error fetching GDACS data with dates:', error);
throw error;
}
}
getSampleData() {
return [
{
id: 'sample_1',
title: 'Flood Alert - Bangladesh',
type: 'Flood',
alertLevel: 'RED',
latitude: 23.6850,
longitude: 90.3563,
date: '2024-12-01',
description: 'Severe flooding in central Bangladesh affecting multiple districts.',
affectedRadius: 75,
affectedPopulation: 2500000,
impactDescription: '2.5M people affected'
},
{
id: 'sample_2',
title: 'Earthquake - Turkey',
type: 'Earthquake',
alertLevel: 'ORANGE',
latitude: 39.9334,
longitude: 32.8597,
date: '2024-11-30',
description: 'Moderate earthquake detected in central Turkey region.',
magnitude: 5.8,
affectedRadius: 50,
affectedPopulation: 850000,
impactDescription: '850K people affected, Magnitude 5.8'
},
{
id: 'sample_3',
title: 'Cyclone Watch - Philippines',
type: 'Cyclone',
alertLevel: 'ORANGE',
latitude: 14.5995,
longitude: 120.9842,
date: '2024-12-02',
description: 'Tropical cyclone approaching the Philippines archipelago.',
affectedRadius: 150,
affectedPopulation: 3200000,
impactDescription: '3.2M people affected, Intensity 3'
},
{
id: 'sample_4',
title: 'Wildfire - California',
type: 'Wildfire',
alertLevel: 'RED',
latitude: 34.0522,
longitude: -118.2437,
date: '2024-11-29',
description: 'Large wildfire burning in Southern California.',
affectedRadius: 20,
affectedPopulation: 125000,
impactDescription: '125K people affected'
},
{
id: 'sample_5',
title: 'Volcano Alert - Indonesia',
type: 'Volcano',
alertLevel: 'GREEN',
latitude: -7.5360,
longitude: 110.4978,
date: '2024-11-28',
description: 'Volcanic activity monitoring in Java island.',
affectedRadius: 25,
affectedPopulation: 45000,
impactDescription: '45K people affected'
}
];
}
sortEventsByAlertLevel(events) {
const alertPriority = { 'RED': 0, 'ORANGE': 1, 'GREEN': 2 };
return events.slice().sort((a, b) => {
const priorityA = alertPriority[a.alertLevel] !== undefined ? alertPriority[a.alertLevel] : 3;
const priorityB = alertPriority[b.alertLevel] !== undefined ? alertPriority[b.alertLevel] : 3;
return priorityA - priorityB;
});
}
displayEvents(events) {
const eventList = document.getElementById('eventList');
if (events.length === 0) {
eventList.innerHTML = '<div class="loading">No disaster events found.</div>';
return;
}
// Sort events by alert level: RED first, then ORANGE, then GREEN
const sortedEvents = this.sortEventsByAlertLevel(events);
const eventsHTML = sortedEvents.map(event => `
<div class="event-item ${event.alertLevel.toLowerCase()}" onclick="app.focusOnEvent('${event.id}')">
<div class="event-title">${event.title}</div>
<div class="event-details">
Type: ${event.type}<br>
Date: ${event.date}<br>
Location: ${event.latitude.toFixed(4)}, ${event.longitude.toFixed(4)}<br>
${event.source ? `Source: ${event.source}` : ''}
${event.magnitude ? `<br>Magnitude: ${event.magnitude}` : ''}
</div>
<span class="alert-badge alert-${event.alertLevel.toLowerCase()}">${event.alertLevel}</span>
</div>
`).join('');
eventList.innerHTML = eventsHTML;
}
addMarkersToMap(events) {
events.forEach(event => {
const color = this.getAlertColor(event.alertLevel);
const icon = this.createCustomIcon(color, event.type);
const marker = L.marker([event.latitude, event.longitude], {
icon,
zIndexOffset: 2000
})
.addTo(this.map)
.bindPopup(`
<div>
<h4>${event.title}</h4>
<p><strong>Type:</strong> ${event.type}</p>
<p><strong>Alert Level:</strong> ${event.alertLevel}</p>
<p><strong>Date:</strong> ${event.date}</p>
<p><strong>Description:</strong> ${event.description}</p>
</div>
`);
marker.eventId = event.id;
this.markers.push(marker);
});
}
createCustomIcon(color, type) {
const iconSymbol = this.getDisasterSymbol(type);
return L.divIcon({
className: 'custom-div-icon',
html: `
<div style="
background-color: ${color};
color: white;
width: 30px;
height: 30px;
display: flex;
align-items: center;
justify-content: center;
font-weight: bold;
font-size: 16px;
border: 2px solid #006400;
box-shadow: 0 2px 4px rgba(0,0,0,0.3);
clip-path: polygon(30% 0%, 70% 0%, 100% 30%, 100% 70%, 70% 100%, 30% 100%, 0% 70%, 0% 30%);
z-index: 2000;
position: relative;
">${iconSymbol}</div>
`,
iconSize: [30, 30],
iconAnchor: [15, 15]
});
}
getDisasterSymbol(type) {
const symbols = {
'Flood': 'π',
'Earthquake': 'π',
'Cyclone': 'π',
'Volcano': 'π',
'Wildfire': 'π₯',
'Precipitation': 'π§οΈ'
};
return symbols[type] || 'β οΈ';
}
getAlertColor(alertLevel) {
const colors = {
'GREEN': '#27ae60',
'ORANGE': '#f39c12',
'RED': '#e74c3c'
};
return colors[alertLevel] || '#3498db';
}
clearMarkers() {