···
(** Convenience functions for common operations *)
(** Read and parse GPX file *)
-
let read ~fs path = IO.read_file ~fs path
-
(** Read and parse GPX file with validation *)
-
let read_validated ~fs path = IO.read_file_validated ~fs path
-
let write ~fs path gpx = IO.write_file ~fs path gpx
-
(** Write GPX to file with validation *)
-
let write_validated ~fs path gpx = IO.write_file_validated ~fs path gpx
(** Write GPX to file with backup *)
-
let write_with_backup ~fs path gpx = IO.write_file_with_backup ~fs path gpx
(** Read GPX from Eio source *)
-
let from_source source = IO.read_source source
(** Write GPX to Eio sink *)
-
let to_sink sink gpx = IO.write_sink sink gpx
-
(** Read GPX from Eio source with validation *)
-
let from_source_validated source = IO.read_source_validated source
-
(** Write GPX to Eio sink with validation *)
-
let to_sink_validated sink gpx = IO.write_sink_validated sink gpx
(** Create simple waypoint *)
-
let make_waypoint ~fs:_ ~lat ~lon ?name ?desc () =
-
match Gpx.latitude lat, Gpx.longitude lon with
-
let wpt = Gpx.make_waypoint_data lat lon in
-
{ wpt with name; desc }
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
(** Create simple track from coordinate list *)
-
let make_track_from_coords ~fs:_ ~name coords =
-
let make_trkpt (lat_f, lon_f) =
-
match Gpx.latitude lat_f, Gpx.longitude lon_f with
-
| Ok lat, Ok lon -> Gpx.make_waypoint_data lat lon
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
-
let trkpts = List.map make_trkpt coords in
-
let trkseg : Gpx.track_segment = { trkpts; extensions = [] } in
-
cmt = None; desc = None; src = None; links = [];
-
number = None; type_ = None; extensions = [];
(** Create simple route from coordinate list *)
-
let make_route_from_coords ~fs:_ ~name coords =
-
let make_rtept (lat_f, lon_f) =
-
match Gpx.latitude lat_f, Gpx.longitude lon_f with
-
| Ok lat, Ok lon -> Gpx.make_waypoint_data lat lon
-
| Error e, _ | _, Error e -> raise (Gpx.Gpx_error (Gpx.Invalid_coordinate e))
-
let rtepts = List.map make_rtept coords in
-
cmt = None; desc = None; src = None; links = [];
-
number = None; type_ = None; extensions = [];
(** Extract coordinates from waypoints *)
-
let waypoint_coords (wpt : Gpx.waypoint_data) =
-
(Gpx.latitude_to_float wpt.lat, Gpx.longitude_to_float wpt.lon)
(** Extract coordinates from track *)
-
let track_coords (track : Gpx.track) =
-
List.fold_left (fun acc (trkseg : Gpx.track_segment) ->
-
List.fold_left (fun acc trkpt ->
-
waypoint_coords trkpt :: acc
(** Extract coordinates from route *)
-
let route_coords (route : Gpx.route) =
-
List.map waypoint_coords route.rtepts
(** Count total points in GPX *)
-
let count_points (gpx : Gpx.gpx) =
-
let waypoint_count = List.length gpx.waypoints in
-
let route_count = List.fold_left (fun acc (route : Gpx.route) ->
-
acc + List.length route.rtepts
-
let track_count = List.fold_left (fun acc (track : Gpx.track) ->
-
List.fold_left (fun acc (trkseg : Gpx.track_segment) ->
-
acc + List.length trkseg.trkpts
-
waypoint_count + route_count + track_count
(** Get GPX statistics *)
···
-
let get_stats (gpx : Gpx.gpx) =
-
let waypoint_count = List.length gpx.waypoints in
-
let route_count = List.length gpx.routes in
-
let track_count = List.length gpx.tracks in
-
let total_points = count_points gpx in
-
List.exists (fun (wpt : Gpx.waypoint_data) -> wpt.ele <> None) gpx.waypoints ||
-
List.exists (fun (route : Gpx.route) ->
-
List.exists (fun (rtept : Gpx.waypoint_data) -> rtept.ele <> None) route.rtepts
-
List.exists (fun (track : Gpx.track) ->
-
List.exists (fun (trkseg : Gpx.track_segment) ->
-
List.exists (fun (trkpt : Gpx.waypoint_data) -> trkpt.ele <> None) trkseg.trkpts
-
List.exists (fun (wpt : Gpx.waypoint_data) -> wpt.time <> None) gpx.waypoints ||
-
List.exists (fun (route : Gpx.route) ->
-
List.exists (fun (rtept : Gpx.waypoint_data) -> rtept.time <> None) route.rtepts
-
List.exists (fun (track : Gpx.track) ->
-
List.exists (fun (trkseg : Gpx.track_segment) ->
-
List.exists (fun (trkpt : Gpx.waypoint_data) -> trkpt.time <> None) trkseg.trkpts
-
{ waypoint_count; route_count; track_count; total_points; has_elevation; has_time }
(** Pretty print GPX statistics *)
-
let print_stats (gpx : Gpx.gpx) =
-
let stats = get_stats gpx in
-
Printf.printf "GPX Statistics:\n";
-
Printf.printf " Waypoints: %d\n" stats.waypoint_count;
-
Printf.printf " Routes: %d\n" stats.route_count;
-
Printf.printf " Tracks: %d\n" stats.track_count;
-
Printf.printf " Total points: %d\n" stats.total_points;
-
Printf.printf " Has elevation data: %s\n" (if stats.has_elevation then "yes" else "no");
-
Printf.printf " Has time data: %s\n" (if stats.has_time then "yes" else "no")