···
(** High-level Unix API for GPX operations *)
3
-
(** Result binding operators *)
4
-
let (let*) = Result.bind
(* Re-export IO module *)
···
| (Error e, _) | (_, Error e) -> Error (Gpx.Error.invalid_coordinate e)
44
-
(** Create simple track from coordinate list *)
45
-
let make_track_from_coords ~name coords =
46
-
let make_trkpt (lat, lon) =
47
-
match (Coordinate.latitude lat, Coordinate.longitude lon) with
48
-
| (Ok lat, Ok lon) -> Ok (Waypoint.make lat lon)
49
-
| (Error e, _) | (_, Error e) -> Error (Gpx.Error.invalid_coordinate e)
51
-
let rec convert_coords acc = function
52
-
| [] -> Ok (List.rev acc)
54
-
match make_trkpt coord with
55
-
| Ok trkpt -> convert_coords (trkpt :: acc) rest
56
-
| Error e -> Error e
58
-
let* _trkpts = convert_coords [] coords in
59
-
Ok (Track.make_from_coords ~name coords)
61
-
(** Create simple route from coordinate list *)
62
-
let make_route_from_coords ~name coords =
63
-
let make_rtept (lat, lon) =
64
-
match (Coordinate.latitude lat, Coordinate.longitude lon) with
65
-
| (Ok lat, Ok lon) -> Ok (Waypoint.make lat lon)
66
-
| (Error e, _) | (_, Error e) -> Error (Gpx.Error.invalid_coordinate e)
68
-
let rec convert_coords acc = function
69
-
| [] -> Ok (List.rev acc)
71
-
match make_rtept coord with
72
-
| Ok rtept -> convert_coords (rtept :: acc) rest
73
-
| Error e -> Error e
75
-
let* _rtepts = convert_coords [] coords in
76
-
Ok (Route.make_from_coords ~name coords)
78
-
(** Extract coordinates from waypoints *)
79
-
let waypoint_coords wpt = Waypoint.to_floats wpt
81
-
(** Extract coordinates from track *)
82
-
let track_coords track = Track.to_coords track
84
-
(** Extract coordinates from route *)
85
-
let route_coords route = Route.to_coords route
87
-
(** Count total points in GPX *)
88
-
let count_points gpx =
89
-
let waypoints = Doc.waypoints gpx in
90
-
let routes = Doc.routes gpx in
91
-
let tracks = Doc.tracks gpx in
92
-
List.length waypoints +
93
-
List.fold_left (fun acc r -> acc + List.length (Route.points r)) 0 routes +
94
-
List.fold_left (fun acc t -> acc + Track.point_count t) 0 tracks
96
-
(** Get GPX statistics *)
98
-
waypoint_count : int;
101
-
total_points : int;
102
-
has_elevation : bool;
107
-
let waypoints = Doc.waypoints gpx in
108
-
let routes = Doc.routes gpx in
109
-
let tracks = Doc.tracks gpx in
111
-
waypoint_count = List.length waypoints;
112
-
route_count = List.length routes;
113
-
track_count = List.length tracks;
114
-
total_points = count_points gpx;
115
-
has_elevation = List.exists (fun w -> Waypoint.elevation w <> None) waypoints;
116
-
has_time = List.exists (fun w -> Waypoint.time w <> None) waypoints;
(** Pretty print GPX statistics *)
121
-
let stats = stats gpx in
122
-
Printf.printf "GPX Statistics:\n";
123
-
Printf.printf " Waypoints: %d\n" stats.waypoint_count;
124
-
Printf.printf " Routes: %d\n" stats.route_count;
125
-
Printf.printf " Tracks: %d\n" stats.track_count;
126
-
Printf.printf " Total points: %d\n" stats.total_points;
127
-
Printf.printf " Has elevation data: %s\n" (if stats.has_elevation then "yes" else "no");
128
-
Printf.printf " Has time data: %s\n" (if stats.has_time then "yes" else "no")
43
+
Format.printf "%a@." Doc.pp_stats gpx