···
(** Convenience functions for common operations *)
(** Read and parse GPX file *)
9
-
let read ~fs path = IO.read_file ~fs path
11
-
(** Read and parse GPX file with validation *)
12
-
let read_validated ~fs path = IO.read_file_validated ~fs path
9
+
let read ?(validate=false) ~fs path = IO.read_file ~validate ~fs path
15
-
let write ~fs path gpx = IO.write_file ~fs path gpx
17
-
(** Write GPX to file with validation *)
18
-
let write_validated ~fs path gpx = IO.write_file_validated ~fs path gpx
12
+
let write ?(validate=false) ~fs path gpx = IO.write_file ~validate ~fs path gpx
(** Write GPX to file with backup *)
21
-
let write_with_backup ~fs path gpx = IO.write_file_with_backup ~fs path gpx
15
+
let write_with_backup ?(validate=false) ~fs path gpx = IO.write_file_with_backup ~validate ~fs path gpx
(** Read GPX from Eio source *)
24
-
let from_source source = IO.read_source source
18
+
let from_source ?(validate=false) source = IO.read_source ~validate source
(** Write GPX to Eio sink *)
27
-
let to_sink sink gpx = IO.write_sink sink gpx
29
-
(** Read GPX from Eio source with validation *)
30
-
let from_source_validated source = IO.read_source_validated source
32
-
(** Write GPX to Eio sink with validation *)
33
-
let to_sink_validated sink gpx = IO.write_sink_validated sink gpx
21
+
let to_sink ?(validate=false) sink gpx = IO.write_sink ~validate sink gpx
(** Create simple waypoint *)
36
-
let make_waypoint ~fs:_ ~lat ~lon ?name ?desc () =
37
-
match Gpx.latitude lat, Gpx.longitude lon with
39
-
let wpt = Gpx.make_waypoint_data lat lon in
40
-
{ wpt with name; desc }
41
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
24
+
let make_waypoint ~fs:_ = Gpx.make_waypoint_from_floats
(** Create simple track from coordinate list *)
44
-
let make_track_from_coords ~fs:_ ~name coords =
45
-
let make_trkpt (lat_f, lon_f) =
46
-
match Gpx.latitude lat_f, Gpx.longitude lon_f with
47
-
| Ok lat, Ok lon -> Gpx.make_waypoint_data lat lon
48
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
50
-
let trkpts = List.map make_trkpt coords in
51
-
let trkseg : Gpx.track_segment = { trkpts; extensions = [] } in
54
-
cmt = None; desc = None; src = None; links = [];
55
-
number = None; type_ = None; extensions = [];
27
+
let make_track_from_coords ~fs:_ = Gpx.make_track_from_coord_list
(** Create simple route from coordinate list *)
60
-
let make_route_from_coords ~fs:_ ~name coords =
61
-
let make_rtept (lat_f, lon_f) =
62
-
match Gpx.latitude lat_f, Gpx.longitude lon_f with
63
-
| Ok lat, Ok lon -> Gpx.make_waypoint_data lat lon
64
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
66
-
let rtepts = List.map make_rtept coords in
69
-
cmt = None; desc = None; src = None; links = [];
70
-
number = None; type_ = None; extensions = [];
30
+
let make_route_from_coords ~fs:_ = Gpx.make_route_from_coord_list
(** Extract coordinates from waypoints *)
75
-
let waypoint_coords (wpt : Gpx.waypoint_data) =
76
-
(Gpx.latitude_to_float wpt.lat, Gpx.longitude_to_float wpt.lon)
33
+
let waypoint_coords = Gpx.waypoint_coords
(** Extract coordinates from track *)
79
-
let track_coords (track : Gpx.track) =
80
-
List.fold_left (fun acc (trkseg : Gpx.track_segment) ->
81
-
List.fold_left (fun acc trkpt ->
82
-
waypoint_coords trkpt :: acc
36
+
let track_coords = Gpx.track_coords
(** Extract coordinates from route *)
88
-
let route_coords (route : Gpx.route) =
89
-
List.map waypoint_coords route.rtepts
39
+
let route_coords = Gpx.route_coords
(** Count total points in GPX *)
92
-
let count_points (gpx : Gpx.gpx) =
93
-
let waypoint_count = List.length gpx.waypoints in
94
-
let route_count = List.fold_left (fun acc (route : Gpx.route) ->
95
-
acc + List.length route.rtepts
97
-
let track_count = List.fold_left (fun acc (track : Gpx.track) ->
98
-
List.fold_left (fun acc (trkseg : Gpx.track_segment) ->
99
-
acc + List.length trkseg.trkpts
100
-
) acc track.trksegs
102
-
waypoint_count + route_count + track_count
42
+
let count_points = Gpx.count_points
(** Get GPX statistics *)
45
+
type gpx_stats = Gpx.gpx_stats = {
···
114
-
let get_stats (gpx : Gpx.gpx) =
115
-
let waypoint_count = List.length gpx.waypoints in
116
-
let route_count = List.length gpx.routes in
117
-
let track_count = List.length gpx.tracks in
118
-
let total_points = count_points gpx in
120
-
let has_elevation =
121
-
List.exists (fun (wpt : Gpx.waypoint_data) -> wpt.ele <> None) gpx.waypoints ||
122
-
List.exists (fun (route : Gpx.route) ->
123
-
List.exists (fun (rtept : Gpx.waypoint_data) -> rtept.ele <> None) route.rtepts
125
-
List.exists (fun (track : Gpx.track) ->
126
-
List.exists (fun (trkseg : Gpx.track_segment) ->
127
-
List.exists (fun (trkpt : Gpx.waypoint_data) -> trkpt.ele <> None) trkseg.trkpts
133
-
List.exists (fun (wpt : Gpx.waypoint_data) -> wpt.time <> None) gpx.waypoints ||
134
-
List.exists (fun (route : Gpx.route) ->
135
-
List.exists (fun (rtept : Gpx.waypoint_data) -> rtept.time <> None) route.rtepts
137
-
List.exists (fun (track : Gpx.track) ->
138
-
List.exists (fun (trkseg : Gpx.track_segment) ->
139
-
List.exists (fun (trkpt : Gpx.waypoint_data) -> trkpt.time <> None) trkseg.trkpts
144
-
{ waypoint_count; route_count; track_count; total_points; has_elevation; has_time }
54
+
let get_stats = Gpx.get_stats
(** Pretty print GPX statistics *)
147
-
let print_stats (gpx : Gpx.gpx) =
148
-
let stats = get_stats gpx in
149
-
Printf.printf "GPX Statistics:\n";
150
-
Printf.printf " Waypoints: %d\n" stats.waypoint_count;
151
-
Printf.printf " Routes: %d\n" stats.route_count;
152
-
Printf.printf " Tracks: %d\n" stats.track_count;
153
-
Printf.printf " Total points: %d\n" stats.total_points;
154
-
Printf.printf " Has elevation data: %s\n" (if stats.has_elevation then "yes" else "no");
155
-
Printf.printf " Has time data: %s\n" (if stats.has_time then "yes" else "no")
57
+
let print_stats = Gpx.print_stats