fastgeotoolkit 0.1.7

A novel high-performance geospatial analysis framework with advanced route density mapping algorithms
Documentation
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
#[cfg(target_arch = "wasm32")]
use gpx::read;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
#[cfg(target_arch = "wasm32")]
use std::io::Cursor;

#[cfg(target_arch = "wasm32")]
use wasm_bindgen::prelude::*;

// DATA STRUCTURES
#[derive(Serialize)]
pub struct HeatmapTrack {
    pub coordinates: Vec<[f64; 2]>,
    pub frequency: u32,
}

#[derive(Serialize)]
pub struct HeatmapResult {
    pub tracks: Vec<HeatmapTrack>,
    pub max_frequency: u32,
}

#[derive(Serialize, Deserialize)]
pub struct ValidationResult {
    valid_count: u32,
    total_count: u32,
    issues: Vec<String>,
}

#[derive(Serialize, Deserialize)]
pub struct TrackStatistics {
    distance_km: f64,
    point_count: u32,
    bounding_box: [f64; 4], // [min_lat, min_lng, max_lat, max_lng]
    elevation_gain: Option<f64>,
    average_speed: Option<f64>,
}

#[derive(Serialize, Deserialize)]
pub struct FileInfo {
    format: String,
    track_count: u32,
    point_count: u32,
    valid: bool,
    file_size: u32,
}

#[derive(Serialize, Deserialize)]
pub struct IntersectionPoint {
    coordinate: [f64; 2],
    track_indices: Vec<u32>,
}

#[derive(Serialize, Deserialize)]
pub struct TrackCluster {
    representative_track: Vec<[f64; 2]>,
    member_indices: Vec<u32>,
    similarity_score: f64,
}

// debugging
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
extern "C" {
    #[wasm_bindgen(js_namespace = console)]
    fn log(s: &str);
}

// decode Google polyline format
pub fn decode_polyline(encoded: &str) -> Vec<[f64; 2]> {
    let mut coords = Vec::new();
    let mut lat = 0i32;
    let mut lng = 0i32;
    let mut index = 0;
    let bytes = encoded.as_bytes();

    while index < bytes.len() {
        // Decode latitude
        let mut shift = 0;
        let mut result = 0i32;
        loop {
            if index >= bytes.len() || shift >= 30 {
                // Prevent overflow
                break;
            }
            let b = bytes[index] as i32 - 63;
            index += 1;
            result |= (b & 0x1f) << shift;
            shift += 5;
            if b < 0x20 {
                break;
            }
        }
        let dlat = if (result & 1) != 0 {
            !(result >> 1)
        } else {
            result >> 1
        };
        lat += dlat;

        // Decode longitude
        shift = 0;
        result = 0;
        loop {
            if index >= bytes.len() || shift >= 30 {
                // Prevent overflow
                break;
            }
            let b = bytes[index] as i32 - 63;
            index += 1;
            result |= (b & 0x1f) << shift;
            shift += 5;
            if b < 0x20 {
                break;
            }
        }
        let dlng = if (result & 1) != 0 {
            !(result >> 1)
        } else {
            result >> 1
        };
        lng += dlng;

        // Convert to lat/lng and add to coordinates
        let lat_f64 = lat as f64 * 1e-5;
        let lng_f64 = lng as f64 * 1e-5;

        if is_valid_coordinate(lat_f64, lng_f64) {
            coords.push([lat_f64, lng_f64]);
        }
    }

    coords
}

// wasm export for polyline decoding
#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]

pub fn decode_polyline_string(encoded: &str) -> JsValue {
    let coords = decode_polyline(encoded);
    serde_wasm_bindgen::to_value(&coords).unwrap()
}

// polyline processingstep
pub fn process_polyline(polyline_str: &str) -> Vec<[f64; 2]> {
    // First try to parse as JSON (RideWithGPS format)
    if let Ok(json_coords) = serde_json::from_str::<Vec<[f64; 2]>>(polyline_str) {
        // It's a JSON array of coordinates
        return if !json_coords.is_empty() {
            filter_unrealistic_jumps(&json_coords)
        } else {
            Vec::new()
        };
    }

    // If JSON parsing fails, treat as encoded polyline (Strava format)
    let coords = decode_polyline(polyline_str);
    if !coords.is_empty() {
        filter_unrealistic_jumps(&coords)
    } else {
        Vec::new()
    }
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn process_polylines(polylines: js_sys::Array) -> JsValue {
    let mut all_tracks: Vec<Vec<[f64; 2]>> = Vec::new();

    for i in 0..polylines.length() {
        if let Some(polyline_str) = polylines.get(i).as_string() {
            let coords = process_polyline(&polyline_str);
            if coords.len() > 1 {
                let simplified = simplify_track(&coords, 0.00005);
                if simplified.len() > 1 {
                    all_tracks.push(simplified);
                }
            }
        }
    }

    // Apply the same processing logic as GPX files
    let result = create_heatmap_from_tracks(all_tracks);

    serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
}

// Helper function to create heatmap from coordinate arrays
pub fn create_heatmap_from_tracks(all_tracks: Vec<Vec<[f64; 2]>>) -> HeatmapResult {
    // Create a segment usage map to count overlapping segments
    let mut segment_usage: HashMap<String, u32> = HashMap::new();

    // Break each track into segments and count usage
    for track in &all_tracks {
        for window in track.windows(2) {
            if let [start, end] = window {
                let segment_key = create_segment_key(*start, *end);
                *segment_usage.entry(segment_key).or_insert(0) += 1;
            }
        }
    }

    // Calculate frequency for each track based on its segments
    let mut heatmap_tracks = Vec::new();

    for track in all_tracks {
        if track.len() < 2 {
            continue;
        }

        // Calculate track frequency as the average frequency of its segments
        let mut total_usage = 0;
        let mut segment_count = 0;

        for window in track.windows(2) {
            if let [start, end] = window {
                let segment_key = create_segment_key(*start, *end);
                if let Some(&usage) = segment_usage.get(&segment_key) {
                    total_usage += usage;
                    segment_count += 1;
                }
            }
        }

        // Use average usage, with minimum of 1
        let track_frequency = if segment_count > 0 {
            (total_usage as f64 / segment_count as f64).round() as u32
        } else {
            1
        };

        heatmap_tracks.push(HeatmapTrack {
            coordinates: track,
            frequency: track_frequency,
        });
    }

    // Find the maximum frequency for normalization
    let max_frequency = heatmap_tracks
        .iter()
        .map(|track| track.frequency)
        .max()
        .unwrap_or(1);

    HeatmapResult {
        tracks: heatmap_tracks,
        max_frequency,
    }
}

pub fn round(value: f64) -> f64 {
    (value * 100000.0).round() / 100000.0
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn process_gpx_files(files: js_sys::Array) -> JsValue {
    let mut all_tracks: Vec<Vec<[f64; 2]>> = Vec::new();

    // Parse all GPX and FIT files and extract tracks
    for file_bytes in files.iter() {
        let array = js_sys::Uint8Array::new(&file_bytes);
        let bytes = array.to_vec();

        // Try to parse as GPX first
        if let Ok(gpx) = read(Cursor::new(&bytes)) {
            for track in gpx.tracks {
                for segment in track.segments {
                    let mut track_coords = Vec::new();

                    for point in segment.points {
                        let lat = round(point.point().y());
                        let lon = round(point.point().x());

                        // Validate coordinates to prevent globe-spanning lines
                        if is_valid_coordinate(lat, lon) {
                            track_coords.push([lat, lon]);
                        }
                    }

                    if track_coords.len() > 1 {
                        // Filter out tracks with unrealistic jumps
                        let filtered_coords = filter_unrealistic_jumps(&track_coords);

                        if filtered_coords.len() > 1 {
                            // Less aggressive simplification to preserve track shape
                            let simplified = simplify_track(&filtered_coords, 0.00005);
                            if simplified.len() > 1 {
                                all_tracks.push(simplified);
                            }
                        }
                    }
                }
            }
        }
        // Try to parse as FIT file if GPX parsing fails
        else if is_fit_file(&bytes) {
            // Custom FIT file parser for extracting GPS coordinates
            let mut fit_parser = FitParser::new(bytes);
            let fit_coordinates = fit_parser.parse_gps_coordinates();

            // Apply the same validation and filtering as GPX
            if fit_coordinates.len() > 1 {
                let filtered_coords = filter_unrealistic_jumps(&fit_coordinates);

                if filtered_coords.len() > 1 {
                    let simplified = simplify_track(&filtered_coords, 0.00005);
                    if simplified.len() > 1 {
                        all_tracks.push(simplified);
                    }
                }
            }
        }
        // Skip files that aren't GPX or FIT
        else {
            continue;
        }
    }

    // create segment usage map to count overlapping segments
    let mut segment_usage: HashMap<String, u32> = HashMap::new();

    // break each track into segments and count usage
    for track in &all_tracks {
        for window in track.windows(2) {
            if let [start, end] = window {
                let segment_key = create_segment_key(*start, *end);
                *segment_usage.entry(segment_key).or_insert(0) += 1;
            }
        }
    }

    // calculate frequency for each track based on its segments
    let mut heatmap_tracks = Vec::new();
    let mut max_frequency = 0;

    for track in all_tracks {
        if track.len() < 2 {
            continue;
        }

        // calculate track frequency as the average frequency of its segments
        let mut total_usage = 0;
        let mut segment_count = 0;

        for window in track.windows(2) {
            if let [start, end] = window {
                let segment_key = create_segment_key(*start, *end);
                if let Some(&usage) = segment_usage.get(&segment_key) {
                    total_usage += usage;
                    segment_count += 1;
                }
            }
        }

        // Use average usage, with minimum of 1
        let track_frequency = if segment_count > 0 {
            (total_usage as f64 / segment_count as f64).round() as u32
        } else {
            1
        };

        max_frequency = max_frequency.max(track_frequency);

        heatmap_tracks.push(HeatmapTrack {
            coordinates: track,
            frequency: track_frequency,
        });
    }

    let result = HeatmapResult {
        tracks: heatmap_tracks,
        max_frequency,
    };

    serde_wasm_bindgen::to_value(&result).unwrap()
}

pub fn create_segment_key(start: [f64; 2], end: [f64; 2]) -> String {
    // change to a larger tolerance for less aggressive matching
    let tolerance = 0.001; // About 100 meters
    let snap_start = snap_to_grid(start, tolerance);
    let snap_end = snap_to_grid(end, tolerance);

    // Normalize direction (smaller coordinate first)
    let (p1, p2) = if (snap_start[0], snap_start[1]) < (snap_end[0], snap_end[1]) {
        (snap_start, snap_end)
    } else {
        (snap_end, snap_start)
    };

    format!("{:.4},{:.4}-{:.4},{:.4}", p1[0], p1[1], p2[0], p2[1])
}

pub fn snap_to_grid(point: [f64; 2], tolerance: f64) -> [f64; 2] {
    [
        (point[0] / tolerance).round() * tolerance,
        (point[1] / tolerance).round() * tolerance,
    ]
}

fn simplify_track(points: &[[f64; 2]], tolerance: f64) -> Vec<[f64; 2]> {
    if points.len() <= 2 {
        return points.to_vec();
    }

    let mut result = vec![points[0]];
    let mut last_added = 0;

    for i in 1..points.len() {
        let distance = distance(points[last_added], points[i]);

        // Add point if it's far enough from the last added point
        // or if it's the last point in the track
        if distance > tolerance || i == points.len() - 1 {
            result.push(points[i]);
            last_added = i;
        }
    }

    result
}

fn distance(p1: [f64; 2], p2: [f64; 2]) -> f64 {
    let dx = p1[0] - p2[0];
    let dy = p1[1] - p2[1];
    (dx * dx + dy * dy).sqrt()
}

pub fn is_valid_coordinate(lat: f64, lon: f64) -> bool {
    // Check for valid latitude and longitude ranges
    if !(-90.0..=90.0).contains(&lat) || !(-180.0..=180.0).contains(&lon) {
        return false;
    }

    // Check for obviously invalid coordinates
    if (lat == 0.0 && lon == 0.0)
        || lat.is_nan()
        || lon.is_nan()
        || lat.is_infinite()
        || lon.is_infinite()
    {
        return false;
    }

    true
}

pub fn filter_unrealistic_jumps(coords: &[[f64; 2]]) -> Vec<[f64; 2]> {
    if coords.len() <= 1 {
        return coords.to_vec();
    }

    let mut filtered = vec![coords[0]];
    let max_jump_km = 100.0;
    let mut consecutive_bad_points = 0;
    const MAX_CONSECUTIVE_BAD: usize = 10; // Allow up to 10 consecutive bad points

    for i in 1..coords.len() {
        let prev = filtered.last().unwrap();
        let curr = coords[i];

        // Calculate approximate distance in kilometers using Haversine formula
        let distance_km = haversine_distance(prev[0], prev[1], curr[0], curr[1]);

        // Only add point if it's within reasonable distance from previous point
        if distance_km <= max_jump_km {
            filtered.push(curr);
            consecutive_bad_points = 0; // Reset bad point counter
        } else {
            consecutive_bad_points += 1;

            // If we've seen too many consecutive bad points, try to find good data ahead
            if consecutive_bad_points <= MAX_CONSECUTIVE_BAD {
                // Look ahead up to 20 points to see if we can find a reasonable continuation
                let mut found_good_continuation = false;
                #[allow(clippy::needless_range_loop)]
                for j in (i + 1)..(i + 21).min(coords.len()) {
                    let future_point = coords[j];
                    let future_distance =
                        haversine_distance(prev[0], prev[1], future_point[0], future_point[1]);

                    // If we find a reasonable point ahead, it suggests this is just a GPS glitch
                    if future_distance <= max_jump_km * 1.5 {
                        // Allow 1.5x distance for bridging
                        found_good_continuation = true;
                        break;
                    }
                }

                // If no good continuation found, we might be at the end of good data
                if !found_good_continuation {
                    // Try to find any remaining good segments by continuing to filter the rest
                    for k in (i + 1)..coords.len() {
                        let remaining_point = coords[k];
                        let remaining_distance = haversine_distance(
                            prev[0],
                            prev[1],
                            remaining_point[0],
                            remaining_point[1],
                        );

                        // If we find a reasonable point, start a new segment from there
                        if remaining_distance <= max_jump_km {
                            filtered.push(remaining_point);
                            // Continue filtering from this new point
                            #[allow(clippy::needless_range_loop)]
                            for m in (k + 1)..coords.len() {
                                let next_prev = filtered.last().unwrap();
                                let next_curr = coords[m];
                                let next_distance = haversine_distance(
                                    next_prev[0],
                                    next_prev[1],
                                    next_curr[0],
                                    next_curr[1],
                                );

                                if next_distance <= max_jump_km {
                                    filtered.push(next_curr);
                                }
                                // Skip points that are too far, but don't break - keep looking
                            }
                            break; // We've processed the rest of the array
                        }
                    }
                    break; // Exit the main loop as we've processed everything
                }
            } else {
                // Too many consecutive bad points - stop processing to avoid bad data
                break;
            }
            // If there is a good continuation, just skip this point and continue
        }
    }

    filtered
}

fn haversine_distance(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
    let r = 6371.0; // Earth's radius in kilometers
    let d_lat = (lat2 - lat1).to_radians();
    let d_lon = (lon2 - lon1).to_radians();
    let lat1_rad = lat1.to_radians();
    let lat2_rad = lat2.to_radians();

    let a =
        (d_lat / 2.0).sin().powi(2) + lat1_rad.cos() * lat2_rad.cos() * (d_lon / 2.0).sin().powi(2);
    let c = 2.0 * a.sqrt().atan2((1.0 - a).sqrt());

    r * c
}

// FIT file format reference: https://developer.garmin.com/fit/protocol/

pub struct FitParser {
    pub data: Vec<u8>,
    pub pos: usize,
    pub message_definitions: HashMap<u8, MessageDefinition>,
}

#[derive(Clone)]
pub struct MessageDefinition {
    pub global_message_number: u16,
    pub fields: Vec<FieldDefinition>,
}

#[derive(Clone)]
pub struct FieldDefinition {
    pub field_def_num: u8,
    pub size: u8,
    pub _base_type: u8,
}

impl FitParser {
    pub fn new(data: Vec<u8>) -> Self {
        Self {
            data,
            pos: 0,
            message_definitions: HashMap::new(),
        }
    }

    pub fn read_u8(&mut self) -> Option<u8> {
        if self.pos < self.data.len() {
            let val = self.data[self.pos];
            self.pos += 1;
            Some(val)
        } else {
            None
        }
    }

    pub fn read_u16_le(&mut self) -> Option<u16> {
        if self.pos + 1 < self.data.len() {
            let val = u16::from_le_bytes([self.data[self.pos], self.data[self.pos + 1]]);
            self.pos += 2;
            Some(val)
        } else {
            None
        }
    }

    pub fn read_u32_le(&mut self) -> Option<u32> {
        if self.pos + 3 < self.data.len() {
            let val = u32::from_le_bytes([
                self.data[self.pos],
                self.data[self.pos + 1],
                self.data[self.pos + 2],
                self.data[self.pos + 3],
            ]);
            self.pos += 4;
            Some(val)
        } else {
            None
        }
    }

    pub fn read_i32_le(&mut self) -> Option<i32> {
        if self.pos + 3 < self.data.len() {
            let val = i32::from_le_bytes([
                self.data[self.pos],
                self.data[self.pos + 1],
                self.data[self.pos + 2],
                self.data[self.pos + 3],
            ]);
            self.pos += 4;
            Some(val)
        } else {
            None
        }
    }

    pub fn skip(&mut self, bytes: usize) {
        self.pos = (self.pos + bytes).min(self.data.len());
    }

    pub fn parse_gps_coordinates(&mut self) -> Vec<[f64; 2]> {
        let mut coordinates = Vec::new();

        // Check FIT file header
        if self.data.len() < 14 {
            return coordinates;
        }

        // FIT file header (14 bytes)
        let header_size = self.read_u8().unwrap_or(0);
        if header_size < 12 {
            return coordinates;
        }

        let _protocol_version = self.read_u8().unwrap_or(0);
        let _profile_version = self.read_u16_le().unwrap_or(0);
        let data_size = self.read_u32_le().unwrap_or(0);

        // Check for ".FIT" signature
        let signature = [
            self.read_u8().unwrap_or(0),
            self.read_u8().unwrap_or(0),
            self.read_u8().unwrap_or(0),
            self.read_u8().unwrap_or(0),
        ];
        if signature != [b'.', b'F', b'I', b'T'] {
            return coordinates;
        }

        // Skip header CRC if present
        if header_size == 14 {
            self.skip(2);
        }

        // Calculate data end position, but also consider that some FIT files
        // might have the data_size field incorrect, so we'll try to parse until
        // we reach the actual end of the file (minus CRC bytes)
        let header_data_end = (self.pos + data_size as usize).min(self.data.len());
        let file_data_end = self.data.len().saturating_sub(2); // Leave 2 bytes for CRC at end
        let data_end = header_data_end.max(file_data_end); // Use the larger of the two

        let mut consecutive_errors = 0;
        const MAX_CONSECUTIVE_ERRORS: usize = 100; // Allow more errors before giving up
        let mut processed_bytes = 0;
        let mut last_progress_pos = self.pos;

        // Parse data records - continue until we reach the end or hit too many errors
        while self.pos < data_end && self.pos < self.data.len() && self.pos + 1 < self.data.len() {
            let start_pos = self.pos;

            // Every 10,000 bytes, check if we're making progress
            if self.pos - last_progress_pos > 10000 {
                processed_bytes += self.pos - last_progress_pos;
                last_progress_pos = self.pos;

                // If we've processed a lot of data and found some coordinates, we're probably doing well
                if coordinates.len() > 100 && processed_bytes > 50000 {
                    consecutive_errors = 0; // Reset error count as we're clearly making progress
                }
            }

            // Ensure we have at least 1 byte to read
            if self.pos >= self.data.len() {
                break;
            }

            let record_header = match self.read_u8() {
                Some(header) => header,
                None => break, // End of data
            };

            let is_definition = (record_header & 0x40) != 0;
            let local_message_type = record_header & 0x0F;

            let parse_success = if is_definition {
                // Parse definition message
                match self.parse_definition_message() {
                    Some(definition) => {
                        self.message_definitions
                            .insert(local_message_type, definition);
                        true
                    }
                    None => {
                        // Definition parsing failed, skip ahead a bit
                        false
                    }
                }
            } else {
                // Parse data message
                if let Some(definition) = self.message_definitions.get(&local_message_type).cloned()
                {
                    // Verify we have enough bytes for this message
                    let total_size: usize = definition.fields.iter().map(|f| f.size as usize).sum();
                    if self.pos + total_size > self.data.len() {
                        // Not enough bytes left, try to parse what we can or skip this message
                        if total_size < 1000 {
                            // Only try if it's a reasonable size
                            self.skip(self.data.len() - self.pos); // Skip to end
                        }
                        break;
                    }

                    // Look for GPS data in multiple message types
                    match definition.global_message_number {
                        20 => {
                            // Record message (primary GPS data)
                            if let Some(coord) = self.parse_record_message(&definition) {
                                if is_valid_coordinate(coord[0], coord[1]) {
                                    coordinates.push(coord);
                                }
                            }
                            true
                        }
                        19 => {
                            // Lap message (might contain GPS data)
                            if let Some(coord) = self.parse_flexible_gps_message(&definition) {
                                if is_valid_coordinate(coord[0], coord[1]) {
                                    coordinates.push(coord);
                                }
                            }
                            true
                        }
                        18 => {
                            // Session message (might contain GPS data)
                            if let Some(coord) = self.parse_flexible_gps_message(&definition) {
                                if is_valid_coordinate(coord[0], coord[1]) {
                                    coordinates.push(coord);
                                }
                            }
                            true
                        }
                        _ => {
                            // Skip other message types but don't count as error
                            let total_size: usize =
                                definition.fields.iter().map(|f| f.size as usize).sum();
                            if total_size < 1000 && self.pos + total_size <= self.data.len() {
                                self.skip(total_size);
                            } else {
                                // Skip to end if message is too large or would overflow
                                self.skip(self.data.len() - self.pos);
                                break;
                            }
                            true
                        }
                    }
                } else {
                    // Unknown message type - this might be an error, but try to continue
                    false
                }
            };

            if parse_success {
                consecutive_errors = 0; // Reset error counter on success
            } else {
                consecutive_errors += 1;

                // If we can't parse this message, try to advance by a small amount and continue
                if self.pos == start_pos {
                    // We didn't advance at all, force advancement to prevent infinite loop
                    self.skip(1);
                }

                // Only give up if we hit way too many consecutive errors AND we haven't found much data
                if consecutive_errors >= MAX_CONSECUTIVE_ERRORS {
                    // If we have a decent amount of coordinates, maybe this is just the end of useful data
                    if coordinates.len() < 100 {
                        break; // Give up if we don't have much data
                    } else {
                        // We have good data, try to continue a bit more
                        consecutive_errors = MAX_CONSECUTIVE_ERRORS / 2; // Reset to half
                    }
                }
            }
        }

        coordinates
    }

    pub fn parse_definition_message(&mut self) -> Option<MessageDefinition> {
        let _start_pos = self.pos;

        // Check we have enough bytes for the basic structure
        if self.pos + 5 > self.data.len() {
            return None;
        }

        self.skip(1); // reserved byte
        self.skip(1); // architecture
        let global_message_number = self.read_u16_le()?;
        let num_fields = self.read_u8()?;

        // Sanity check on number of fields
        if num_fields > 100 {
            // This seems unreasonable, likely a parsing error
            return None;
        }

        // Check we have enough bytes for all field definitions
        if self.pos + (num_fields as usize * 3) > self.data.len() {
            return None;
        }

        let mut fields = Vec::new();
        for _ in 0..num_fields {
            // Check bounds before each field
            if self.pos + 3 > self.data.len() {
                // Not enough bytes for this field definition
                return None;
            }

            let field_def_num = self.read_u8()?;
            let size = self.read_u8()?;
            let base_type = self.read_u8()?;

            // Sanity check on field size
            if size > 100 {
                // Field size seems unreasonable, likely a parsing error
                return None;
            }

            fields.push(FieldDefinition {
                field_def_num,
                size,
                _base_type: base_type,
            });
        }

        Some(MessageDefinition {
            global_message_number,
            fields,
        })
    }

    pub fn parse_record_message(&mut self, definition: &MessageDefinition) -> Option<[f64; 2]> {
        let mut lat: Option<f64> = None;
        let mut lon: Option<f64> = None;

        for field in &definition.fields {
            // More defensive bounds checking
            if field.size == 0
                || self.pos >= self.data.len()
                || self.pos + field.size as usize > self.data.len()
            {
                // Skip this field if we can't read it safely
                let safe_skip = (self.data.len() - self.pos).min(field.size as usize);
                self.skip(safe_skip);
                continue;
            }

            match field.field_def_num {
                0 => {
                    // Latitude field
                    if field.size == 4 {
                        if let Some(lat_raw) = self.read_i32_le() {
                            if lat_raw != 0x7FFFFFFF && lat_raw != 0 {
                                let lat_degrees = lat_raw as f64 * (180.0 / 2147483648.0);
                                if lat_degrees.abs() <= 90.0 {
                                    lat = Some(lat_degrees);
                                }
                            }
                        }
                    } else {
                        self.skip(field.size as usize);
                    }
                }
                1 => {
                    // Longitude field
                    if field.size == 4 {
                        if let Some(lon_raw) = self.read_i32_le() {
                            if lon_raw != 0x7FFFFFFF && lon_raw != 0 {
                                let lon_degrees = lon_raw as f64 * (180.0 / 2147483648.0);
                                if lon_degrees.abs() <= 180.0 {
                                    lon = Some(lon_degrees);
                                }
                            }
                        }
                    } else {
                        self.skip(field.size as usize);
                    }
                }
                _ => {
                    // Skip other fields
                    self.skip(field.size as usize);
                }
            }
        }

        if let (Some(lat_val), Some(lon_val)) = (lat, lon) {
            Some([round(lat_val), round(lon_val)])
        } else {
            None
        }
    }

    pub fn parse_flexible_gps_message(
        &mut self,
        definition: &MessageDefinition,
    ) -> Option<[f64; 2]> {
        let mut lat: Option<f64> = None;
        let mut lon: Option<f64> = None;
        let mut potential_coords = Vec::new();

        // Collect all potential coordinate values
        for field in &definition.fields {
            // More defensive bounds checking
            if field.size == 0
                || self.pos >= self.data.len()
                || self.pos + field.size as usize > self.data.len()
            {
                // Skip this field if we can't read it safely
                let safe_skip = (self.data.len() - self.pos).min(field.size as usize);
                self.skip(safe_skip);
                continue;
            }

            if field.size == 4 {
                if let Some(value) = self.read_i32_le() {
                    if value != 0x7FFFFFFF && value != 0 {
                        let degrees = value as f64 * (180.0 / 2147483648.0);
                        // Only consider reasonable coordinate values
                        if degrees.abs() <= 180.0 {
                            potential_coords.push(degrees);
                        }
                    }
                }
            } else {
                self.skip(field.size as usize);
            }
        }

        // Try to identify lat/lon from potential coordinates
        for coord in &potential_coords {
            if coord.abs() <= 90.0 && lat.is_none() {
                lat = Some(*coord);
            } else if coord.abs() <= 180.0 && lon.is_none() && Some(*coord) != lat {
                lon = Some(*coord);
            }
        }

        if let (Some(lat_val), Some(lon_val)) = (lat, lon) {
            Some([round(lat_val), round(lon_val)])
        } else {
            None
        }
    }
}

pub fn is_fit_file(data: &[u8]) -> bool {
    if data.len() < 12 {
        return false;
    }

    // Check for FIT signature at bytes 8-11
    data[8] == b'.' && data[9] == b'F' && data[10] == b'I' && data[11] == b'T'
}

// #################################################
//
//     DATA PROCESSING & VALIDATION FUNCTIONS
//
// #################################################

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn validate_coordinates(coords: js_sys::Array) -> JsValue {
    let mut issues = Vec::new();
    let mut valid_count = 0;

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            let [lat, lon] = coord_array;

            if !is_valid_coordinate(lat, lon) {
                issues.push(format!(
                    "invalid coordinate at index {}: [{}, {}]",
                    i, lat, lon
                ));
            } else {
                valid_count += 1;
            }
        }
    }

    let result = ValidationResult {
        valid_count,
        total_count: coords.length(),
        issues,
    };

    serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
}

// validation
pub fn validate_coordinates_rust(coords: &[[f64; 2]]) -> (u32, Vec<String>) {
    let mut issues = Vec::new();
    let mut valid_count = 0;

    for (i, &[lat, lon]) in coords.iter().enumerate() {
        if !is_valid_coordinate(lat, lon) {
            issues.push(format!("invalid coordinate at index {i}: [{lat}, {lon}]"));
        } else {
            valid_count += 1;
        }
    }

    (valid_count, issues)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn filter_coordinates_by_bounds(coords: js_sys::Array, bounds: js_sys::Array) -> JsValue {
    // bounds format: [min_lat, min_lon, max_lat, max_lon]
    if bounds.length() != 4 {
        return JsValue::NULL;
    }

    let min_lat = bounds.get(0).as_f64().unwrap_or(-90.0);
    let min_lon = bounds.get(1).as_f64().unwrap_or(-180.0);
    let max_lat = bounds.get(2).as_f64().unwrap_or(90.0);
    let max_lon = bounds.get(3).as_f64().unwrap_or(180.0);

    let mut filtered = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            let [lat, lon] = coord_array;

            if lat >= min_lat && lat <= max_lat && lon >= min_lon && lon <= max_lon {
                filtered.push([lat, lon]);
            }
        }
    }

    serde_wasm_bindgen::to_value(&filtered).unwrap_or(JsValue::NULL)
}

// bounds filtering
pub fn filter_coordinates_by_bounds_rust(coords: &[[f64; 2]], bounds: [f64; 4]) -> Vec<[f64; 2]> {
    let [min_lat, min_lon, max_lat, max_lon] = bounds;

    coords
        .iter()
        .filter(|&&[lat, lon]| lat >= min_lat && lat <= max_lat && lon >= min_lon && lon <= max_lon)
        .copied()
        .collect()
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn calculate_track_statistics(coords: js_sys::Array) -> JsValue {
    if coords.length() == 0 {
        return JsValue::NULL;
    }

    let mut coordinates = Vec::new();
    let mut min_lat = f64::MAX;
    let mut max_lat = f64::MIN;
    let mut min_lng = f64::MAX;
    let mut max_lng = f64::MIN;

    // extract coordinates and calculate bounds
    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            let [lat, lon] = coord_array;
            coordinates.push([lat, lon]);

            min_lat = min_lat.min(lat);
            max_lat = max_lat.max(lat);
            min_lng = min_lng.min(lon);
            max_lng = max_lng.max(lon);
        }
    }

    let mut total_distance = 0.0;
    for window in coordinates.windows(2) {
        if let [start, end] = window {
            total_distance += haversine_distance(start[0], start[1], end[0], end[1]);
        }
    }

    let result = TrackStatistics {
        distance_km: total_distance,
        point_count: coordinates.len() as u32,
        bounding_box: [min_lat, min_lng, max_lat, max_lng],
        elevation_gain: None, // would require elevation data
        average_speed: None,  // would require timestamp data
    };

    serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
}

// track statistics
pub fn calculate_track_statistics_rust(coords: &[[f64; 2]]) -> Option<(f64, u32, [f64; 4])> {
    if coords.is_empty() {
        return None;
    }

    let bbox = calculate_bounding_box(coords);
    let mut total_distance = 0.0;

    for window in coords.windows(2) {
        if let [start, end] = window {
            total_distance += haversine_distance(start[0], start[1], end[0], end[1]);
        }
    }

    Some((total_distance, coords.len() as u32, bbox))
}

// #################################################
//
//          TRACK MANIPULATION FUNCTIONS
//
// #################################################

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn simplify_coordinates(coords: js_sys::Array, tolerance: f64) -> JsValue {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            coordinates.push(coord_array);
        }
    }

    let simplified = simplify_track(&coordinates, tolerance);
    serde_wasm_bindgen::to_value(&simplified).unwrap_or(JsValue::NULL)
}

// simplify coordinates
pub fn simplify_coordinates_rust(coords: &[[f64; 2]], tolerance: f64) -> Vec<[f64; 2]> {
    simplify_track(coords, tolerance)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn merge_nearby_tracks(tracks: js_sys::Array, distance_threshold: f64) -> JsValue {
    let mut track_list = Vec::new();

    for i in 0..tracks.length() {
        if let Ok(track) = serde_wasm_bindgen::from_value::<Vec<[f64; 2]>>(tracks.get(i)) {
            track_list.push(track);
        }
    }

    let merged = merge_similar_tracks(&track_list, distance_threshold);
    serde_wasm_bindgen::to_value(&merged).unwrap_or(JsValue::NULL)
}
// merge nearby tracks
pub fn merge_nearby_tracks_rust(
    tracks: &[Vec<[f64; 2]>],
    distance_threshold: f64,
) -> Vec<Vec<[f64; 2]>> {
    merge_similar_tracks(tracks, distance_threshold)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn split_track_by_gaps(
    coords: js_sys::Array,
    max_gap_km: f64,
    _max_time_gap_seconds: u32,
) -> JsValue {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            coordinates.push(coord_array);
        }
    }

    let split_tracks = split_by_spatial_gaps(&coordinates, max_gap_km);
    serde_wasm_bindgen::to_value(&split_tracks).unwrap_or(JsValue::NULL)
}

// split track by gaps
pub fn split_track_by_gaps_rust(coords: &[[f64; 2]], max_gap_km: f64) -> Vec<Vec<[f64; 2]>> {
    split_by_spatial_gaps(coords, max_gap_km)
}

// #################################################
//
//          FORMAT CONVERSION FUNCTIONS
//
// #################################################

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn coordinates_to_polyline(coords: js_sys::Array) -> String {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            coordinates.push(coord_array);
        }
    }

    encode_polyline(&coordinates)
}

// coordinates to polyline
pub fn coordinates_to_polyline_rust(coords: &[[f64; 2]]) -> String {
    encode_polyline(coords)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn coordinates_to_geojson(coords: js_sys::Array, properties: JsValue) -> JsValue {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            // geojson uses [lng, lat] order
            coordinates.push([coord_array[1], coord_array[0]]);
        }
    }

    let props = if properties.is_null() || properties.is_undefined() {
        serde_json::json!({})
    } else {
        serde_wasm_bindgen::from_value(properties).unwrap_or(serde_json::json!({}))
    };

    let geojson = serde_json::json!({
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": coordinates
        },
        "properties": props
    });

    serde_wasm_bindgen::to_value(&geojson).unwrap_or(JsValue::NULL)
}

// coordinates to geojson (returns json value)
pub fn coordinates_to_geojson_rust(
    coords: &[[f64; 2]],
    properties: serde_json::Value,
) -> serde_json::Value {
    let coordinates: Vec<[f64; 2]> = coords
        .iter()
        .map(|&[lat, lon]| [lon, lat]) // geojson uses [lng, lat] order
        .collect();

    serde_json::json!({
        "type": "Feature",
        "geometry": {
            "type": "LineString",
            "coordinates": coordinates
        },
        "properties": properties
    })
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn export_to_gpx(tracks: js_sys::Array, _metadata: JsValue) -> String {
    let mut gpx_content = String::from(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="fastgeotoolkit">
"#,
    );

    for i in 0..tracks.length() {
        if let Ok(track) = serde_wasm_bindgen::from_value::<Vec<[f64; 2]>>(tracks.get(i)) {
            gpx_content.push_str(&format!(
                "  <trk>\n    <name>Track {}</name>\n    <trkseg>\n",
                i + 1
            ));

            for coord in track {
                gpx_content.push_str(&format!(
                    "      <trkpt lat=\"{:.6}\" lon=\"{:.6}\"></trkpt>\n",
                    coord[0], coord[1]
                ));
            }

            gpx_content.push_str("    </trkseg>\n  </trk>\n");
        }
    }

    gpx_content.push_str("</gpx>");
    gpx_content
}
// gpx export
pub fn export_to_gpx_rust(tracks: &[Vec<[f64; 2]>]) -> String {
    let mut gpx_content = String::from(
        r#"<?xml version="1.0" encoding="UTF-8"?>
<gpx version="1.1" creator="fastgeotoolkit">
"#,
    );

    for (i, track) in tracks.iter().enumerate() {
        gpx_content.push_str(&format!(
            "  <trk>\n    <name>Track {}</name>\n    <trkseg>\n",
            i + 1
        ));

        for coord in track {
            gpx_content.push_str(&format!(
                "      <trkpt lat=\"{:.6}\" lon=\"{:.6}\"></trkpt>\n",
                coord[0], coord[1]
            ));
        }

        gpx_content.push_str("    </trkseg>\n  </trk>\n");
    }

    gpx_content.push_str("</gpx>");
    gpx_content
}

// #################################################
//
//      c    TRACK ANALYSIS FUNCTIONS
//
// #################################################

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn find_track_intersections(tracks: js_sys::Array, tolerance: f64) -> JsValue {
    let mut track_list = Vec::new();

    for i in 0..tracks.length() {
        if let Ok(track) = serde_wasm_bindgen::from_value::<Vec<[f64; 2]>>(tracks.get(i)) {
            track_list.push(track);
        }
    }

    let intersections = find_intersections(&track_list, tolerance);
    serde_wasm_bindgen::to_value(&intersections).unwrap_or(JsValue::NULL)
}

// track intersections finder
pub fn find_track_intersections_rust(
    tracks: &[Vec<[f64; 2]>],
    tolerance: f64,
) -> Vec<([f64; 2], Vec<u32>)> {
    let intersections = find_intersections(tracks, tolerance);
    intersections
        .into_iter()
        .map(|ip| (ip.coordinate, ip.track_indices))
        .collect()
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn calculate_coverage_area(tracks: js_sys::Array) -> JsValue {
    let mut all_points = Vec::new();

    for i in 0..tracks.length() {
        if let Ok(track) = serde_wasm_bindgen::from_value::<Vec<[f64; 2]>>(tracks.get(i)) {
            all_points.extend(track);
        }
    }

    if all_points.is_empty() {
        return JsValue::NULL;
    }

    let bbox = calculate_bounding_box(&all_points);
    let area_km2 = calculate_area_km2(&bbox);

    let result = serde_json::json!({
        "bounding_box": bbox,
        "area_km2": area_km2,
        "point_count": all_points.len()
    });

    serde_wasm_bindgen::to_value(&result).unwrap_or(JsValue::NULL)
}
// calculate coverage area
pub fn calculate_coverage_area_rust(tracks: &[Vec<[f64; 2]>]) -> Option<([f64; 4], f64, usize)> {
    let all_points: Vec<[f64; 2]> = tracks.iter().flatten().copied().collect();

    if all_points.is_empty() {
        return None;
    }

    let bbox = calculate_bounding_box(&all_points);
    let area_km2 = calculate_area_km2(&bbox);

    Some((bbox, area_km2, all_points.len()))
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn cluster_tracks_by_similarity(tracks: js_sys::Array, similarity_threshold: f64) -> JsValue {
    let mut track_list = Vec::new();

    for i in 0..tracks.length() {
        if let Ok(track) = serde_wasm_bindgen::from_value::<Vec<[f64; 2]>>(tracks.get(i)) {
            track_list.push(track);
        }
    }

    let clusters = cluster_similar_tracks(&track_list, similarity_threshold);
    serde_wasm_bindgen::to_value(&clusters).unwrap_or(JsValue::NULL)
}

// cluster tracks by similarity
pub fn cluster_tracks_by_similarity_rust(
    tracks: &[Vec<[f64; 2]>],
    similarity_threshold: f64,
) -> Vec<(Vec<[f64; 2]>, Vec<u32>, f64)> {
    let clusters = cluster_similar_tracks(tracks, similarity_threshold);
    clusters
        .into_iter()
        .map(|tc| {
            (
                tc.representative_track,
                tc.member_indices,
                tc.similarity_score,
            )
        })
        .collect()
}

// #################################################
//
//     (SIMPLE) UTILITY FUNCTIONS (public)
//
// #################################################

pub fn calculate_distance_between_points(lat1: f64, lon1: f64, lat2: f64, lon2: f64) -> f64 {
    haversine_distance(lat1, lon1, lat2, lon2)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn get_bounding_box(coords: js_sys::Array) -> JsValue {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            coordinates.push(coord_array);
        }
    }

    if coordinates.is_empty() {
        return JsValue::NULL;
    }

    let bbox = calculate_bounding_box(&coordinates);
    serde_wasm_bindgen::to_value(&bbox).unwrap_or(JsValue::NULL)
}

// bounding box
pub fn get_bounding_box_rust(coords: &[[f64; 2]]) -> [f64; 4] {
    calculate_bounding_box(coords)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn resample_track(coords: js_sys::Array, target_point_count: u32) -> JsValue {
    let mut coordinates = Vec::new();

    for i in 0..coords.length() {
        if let Ok(coord_array) = serde_wasm_bindgen::from_value::<[f64; 2]>(coords.get(i)) {
            coordinates.push(coord_array);
        }
    }

    let resampled = resample_coordinates(&coordinates, target_point_count as usize);
    serde_wasm_bindgen::to_value(&resampled).unwrap_or(JsValue::NULL)
}

// resample track
pub fn resample_track_rust(coords: &[[f64; 2]], target_point_count: usize) -> Vec<[f64; 2]> {
    resample_coordinates(coords, target_point_count)
}

// #################################################
//
//        FILE INFO/METADATA FUNCTIONS
//
// #################################################

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn get_file_info(file_bytes: js_sys::Uint8Array) -> JsValue {
    let bytes = file_bytes.to_vec();
    let mut info = FileInfo {
        format: "unknown".to_string(),
        track_count: 0,
        point_count: 0,
        valid: false,
        file_size: bytes.len() as u32,
    };

    // check for gpx
    if let Ok(gpx) = read(Cursor::new(&bytes)) {
        info.format = "gpx".to_string();
        info.valid = true;
        info.track_count = gpx.tracks.len() as u32;

        for track in gpx.tracks {
            for segment in track.segments {
                info.point_count += segment.points.len() as u32;
            }
        }
    }
    // check for fit
    else if is_fit_file(&bytes) {
        info.format = "fit".to_string();
        info.valid = true;
        info.track_count = 1; // assume single track for fit files

        let mut parser = FitParser::new(bytes);
        let coords = parser.parse_gps_coordinates();
        info.point_count = coords.len() as u32;
    }

    serde_wasm_bindgen::to_value(&info).unwrap_or(JsValue::NULL)
}

#[cfg(target_arch = "wasm32")]
#[wasm_bindgen]
pub fn extract_file_metadata(file_bytes: js_sys::Uint8Array) -> JsValue {
    let bytes = file_bytes.to_vec();

    if let Ok(gpx) = read(Cursor::new(&bytes)) {
        let metadata = serde_json::json!({
            "format": "gpx",
            "creator": gpx.creator,
            "version": format!("{:?}", gpx.version),
            "tracks": gpx.tracks.iter().map(|t| serde_json::json!({
                "name": t.name,
                "description": t.description,
                "segment_count": t.segments.len()
            })).collect::<Vec<_>>()
        });

        return serde_wasm_bindgen::to_value(&metadata).unwrap_or(JsValue::NULL);
    }

    if is_fit_file(&bytes) {
        let metadata = serde_json::json!({
            "format": "fit",
            "file_size": bytes.len()
        });

        return serde_wasm_bindgen::to_value(&metadata).unwrap_or(JsValue::NULL);
    }

    JsValue::NULL
}

// #################################################
//
//        HELPER FUNCTIONS (private)
//
// #################################################

fn encode_polyline(coordinates: &[[f64; 2]]) -> String {
    let mut encoded = String::new();
    let mut prev_lat = 0i32;
    let mut prev_lng = 0i32;

    for coord in coordinates {
        let lat = (coord[0] * 1e5).round() as i32;
        let lng = (coord[1] * 1e5).round() as i32;

        let d_lat = lat - prev_lat;
        let d_lng = lng - prev_lng;

        encoded.push_str(&encode_number(d_lat));
        encoded.push_str(&encode_number(d_lng));

        prev_lat = lat;
        prev_lng = lng;
    }

    encoded
}

fn encode_number(num: i32) -> String {
    let mut value = if num < 0 { (!num) << 1 | 1 } else { num << 1 };
    let mut encoded = String::new();

    while value >= 0x20 {
        encoded.push(((0x20 | (value & 0x1f)) + 63) as u8 as char);
        value >>= 5;
    }
    encoded.push((value + 63) as u8 as char);

    encoded
}

fn merge_similar_tracks(tracks: &[Vec<[f64; 2]>], distance_threshold: f64) -> Vec<Vec<[f64; 2]>> {
    let mut merged = Vec::new();
    let mut used = vec![false; tracks.len()];

    for i in 0..tracks.len() {
        if used[i] {
            continue;
        }

        let mut current_track = tracks[i].clone();
        used[i] = true;

        // look for similar tracks to merge
        for j in (i + 1)..tracks.len() {
            if used[j] {
                continue;
            }

            if tracks_are_similar(&current_track, &tracks[j], distance_threshold) {
                // merge tracks by interleaving points
                current_track = merge_two_tracks(&current_track, &tracks[j]);
                used[j] = true;
            }
        }

        merged.push(current_track);
    }

    merged
}

fn tracks_are_similar(track1: &[[f64; 2]], track2: &[[f64; 2]], threshold: f64) -> bool {
    if track1.len() < 2 || track2.len() < 2 {
        return false;
    }

    // simple similarity check: compare start and end points
    let start_dist = haversine_distance(track1[0][0], track1[0][1], track2[0][0], track2[0][1]);
    let end_dist = haversine_distance(
        track1[track1.len() - 1][0],
        track1[track1.len() - 1][1],
        track2[track2.len() - 1][0],
        track2[track2.len() - 1][1],
    );

    start_dist <= threshold && end_dist <= threshold
}

fn merge_two_tracks(track1: &[[f64; 2]], track2: &[[f64; 2]]) -> Vec<[f64; 2]> {
    // simple merge: take the longer track as base
    if track1.len() >= track2.len() {
        track1.to_vec()
    } else {
        track2.to_vec()
    }
}

fn split_by_spatial_gaps(coordinates: &[[f64; 2]], max_gap_km: f64) -> Vec<Vec<[f64; 2]>> {
    if coordinates.len() < 2 {
        return vec![coordinates.to_vec()];
    }

    let mut tracks = Vec::new();
    let mut current_track = vec![coordinates[0]];

    for window in coordinates.windows(2) {
        if let [current, next] = window {
            let distance = haversine_distance(current[0], current[1], next[0], next[1]);

            if distance <= max_gap_km {
                current_track.push(*next);
            } else {
                // gap detected, start new track
                if current_track.len() > 1 {
                    tracks.push(current_track);
                }
                current_track = vec![*next];
            }
        }
    }

    if current_track.len() > 1 {
        tracks.push(current_track);
    }

    tracks
}

fn find_intersections(tracks: &[Vec<[f64; 2]>], tolerance: f64) -> Vec<IntersectionPoint> {
    let mut intersections = Vec::new();
    let mut intersection_map: HashMap<String, Vec<u32>> = HashMap::new();

    // find points that are close to each other across different tracks
    for (i, track1) in tracks.iter().enumerate() {
        for point1 in track1 {
            let grid_key = format!(
                "{:.4},{:.4}",
                (point1[0] / tolerance).round() * tolerance,
                (point1[1] / tolerance).round() * tolerance
            );

            intersection_map.entry(grid_key).or_default().push(i as u32);
        }
    }

    // collect intersections where multiple tracks meet
    for (key_str, track_indices) in intersection_map {
        if track_indices.len() > 1 {
            let coords: Vec<f64> = key_str
                .split(',')
                .map(|s| s.parse().unwrap_or(0.0))
                .collect();
            if coords.len() == 2 {
                let mut unique_tracks = track_indices;
                unique_tracks.sort();
                unique_tracks.dedup();

                if unique_tracks.len() > 1 {
                    intersections.push(IntersectionPoint {
                        coordinate: [coords[0], coords[1]],
                        track_indices: unique_tracks,
                    });
                }
            }
        }
    }

    intersections
}

fn calculate_bounding_box(coordinates: &[[f64; 2]]) -> [f64; 4] {
    let mut min_lat = f64::MAX;
    let mut max_lat = f64::MIN;
    let mut min_lng = f64::MAX;
    let mut max_lng = f64::MIN;

    for coord in coordinates {
        min_lat = min_lat.min(coord[0]);
        max_lat = max_lat.max(coord[0]);
        min_lng = min_lng.min(coord[1]);
        max_lng = max_lng.max(coord[1]);
    }

    [min_lat, min_lng, max_lat, max_lng]
}
fn calculate_area_km2(bbox: &[f64; 4]) -> f64 {
    let [min_lat, min_lng, max_lat, max_lng] = *bbox;

    // approximate area calculation using haversine for edges
    let width_km = haversine_distance(min_lat, min_lng, min_lat, max_lng);
    let height_km = haversine_distance(min_lat, min_lng, max_lat, min_lng);

    width_km * height_km
}

fn cluster_similar_tracks(
    tracks: &[Vec<[f64; 2]>],
    similarity_threshold: f64,
) -> Vec<TrackCluster> {
    let mut clusters = Vec::new();
    let mut assigned = vec![false; tracks.len()];

    for i in 0..tracks.len() {
        if assigned[i] {
            continue;
        }

        let mut cluster_members = vec![i as u32];
        assigned[i] = true;

        // find similar tracks
        for j in (i + 1)..tracks.len() {
            if assigned[j] {
                continue;
            }

            let similarity = calculate_track_similarity(&tracks[i], &tracks[j]);
            if similarity >= similarity_threshold {
                cluster_members.push(j as u32);
                assigned[j] = true;
            }
        }

        clusters.push(TrackCluster {
            representative_track: tracks[i].clone(),
            member_indices: cluster_members,
            similarity_score: 1.0, // placeholder
        });
    }

    clusters
}

fn calculate_track_similarity(track1: &[[f64; 2]], track2: &[[f64; 2]]) -> f64 {
    if track1.len() < 2 || track2.len() < 2 {
        return 0.0;
    }

    // simple similarity based on start/end point proximity
    let start_dist = haversine_distance(track1[0][0], track1[0][1], track2[0][0], track2[0][1]);
    let end_dist = haversine_distance(
        track1[track1.len() - 1][0],
        track1[track1.len() - 1][1],
        track2[track2.len() - 1][0],
        track2[track2.len() - 1][1],
    );

    // convert distance to similarity score (closer = more similar)
    let max_reasonable_distance = 10.0; // 10km
    let start_similarity = (max_reasonable_distance - start_dist.min(max_reasonable_distance))
        / max_reasonable_distance;
    let end_similarity =
        (max_reasonable_distance - end_dist.min(max_reasonable_distance)) / max_reasonable_distance;

    (start_similarity + end_similarity) / 2.0
}
fn resample_coordinates(coordinates: &[[f64; 2]], target_count: usize) -> Vec<[f64; 2]> {
    if coordinates.len() <= target_count {
        return coordinates.to_vec();
    }

    let mut resampled = Vec::new();
    let step = coordinates.len() as f64 / target_count as f64;

    for i in 0..target_count {
        let index = (i as f64 * step) as usize;
        if index < coordinates.len() {
            resampled.push(coordinates[index]);
        }
    }

    // always include the last point
    if let Some(last) = coordinates.last() {
        if resampled.last() != Some(last) {
            resampled.push(*last);
        }
    }

    resampled
}