1(** Example demonstrating basic GPX operations using the direct API *)
2
3open Gpx
4
5let () =
6 Printf.printf "=== mlgpx Library Example ===\n\n";
7
8 (* Create coordinates using direct API *)
9 let create_coordinate_pair lat_f lon_f =
10 match latitude lat_f, longitude lon_f with
11 | Ok lat, Ok lon -> Ok (lat, lon)
12 | Error e, _ | _, Error e -> Error (Invalid_coordinate e)
13 in
14
15 (* Create a simple waypoint *)
16 (match create_coordinate_pair 37.7749 (-122.4194) with
17 | Ok (lat, lon) ->
18 let wpt = make_waypoint_data lat lon in
19 let wpt = { wpt with name = Some "San Francisco"; desc = Some "Golden Gate Bridge area" } in
20 Printf.printf "✓ Created waypoint: %s\n" (Option.value wpt.name ~default:"<unnamed>");
21
22 (* Create GPX document *)
23 let gpx = make_gpx ~creator:"mlgpx direct API example" in
24 let gpx = { gpx with waypoints = [wpt] } in
25
26 (* Add metadata *)
27 let metadata = { empty_metadata with
28 name = Some "Example GPX File";
29 desc = Some "Demonstration of mlgpx library capabilities";
30 time = None (* Ptime_clock not available in this context *)
31 } in
32 let gpx = { gpx with metadata = Some metadata } in
33
34 (* Create a simple track *)
35 let track_points = [
36 (37.7749, -122.4194, Some "Start");
37 (37.7849, -122.4094, Some "Mid Point");
38 (37.7949, -122.3994, Some "End");
39 ] in
40
41 let create_track_points acc (lat_f, lon_f, name) =
42 match create_coordinate_pair lat_f lon_f with
43 | Ok (lat, lon) ->
44 let trkpt = make_waypoint_data lat lon in
45 let trkpt = { trkpt with name } in
46 trkpt :: acc
47 | Error _ -> acc
48 in
49
50 let trkpts = List.fold_left create_track_points [] track_points |> List.rev in
51 let trkseg = { trkpts; extensions = [] } in
52 let track = {
53 name = Some "Example Track";
54 cmt = Some "Sample GPS track";
55 desc = Some "Demonstrates track creation";
56 src = None; links = []; number = None; type_ = None; extensions = [];
57 trksegs = [trkseg];
58 } in
59 let gpx = { gpx with tracks = [track] } in
60
61 Printf.printf "✓ Created track with %d points\n" (List.length trkpts);
62
63 (* Validate the document *)
64 let validation = validate_gpx gpx in
65 Printf.printf "✓ GPX validation: %s\n" (if validation.is_valid then "PASSED" else "FAILED");
66
67 if not validation.is_valid then (
68 Printf.printf "Validation issues:\n";
69 List.iter (fun issue ->
70 Printf.printf " %s: %s\n"
71 (match issue.level with `Error -> "ERROR" | `Warning -> "WARNING")
72 issue.message
73 ) validation.issues
74 );
75
76 (* Convert to XML string *)
77 (match write_string gpx with
78 | Ok xml_string ->
79 Printf.printf "✓ Generated XML (%d characters)\n" (String.length xml_string);
80
81 (* Save to file using Unix layer for convenience *)
82 (match Gpx_unix.write_validated "example_direct.gpx" gpx with
83 | Ok () ->
84 Printf.printf "✓ Saved to example_direct.gpx\n";
85
86 (* Read it back to verify round-trip *)
87 (match Gpx_unix.read_validated "example_direct.gpx" with
88 | Ok gpx2 ->
89 Printf.printf "✓ Successfully read back GPX\n";
90 let validation2 = validate_gpx gpx2 in
91 Printf.printf "✓ Round-trip validation: %s\n"
92 (if validation2.is_valid then "PASSED" else "FAILED");
93 Printf.printf " Waypoints: %d, Tracks: %d\n"
94 (List.length gpx2.waypoints) (List.length gpx2.tracks)
95 | Error e ->
96 Printf.printf "✗ Error reading back: %s\n"
97 (match e with
98 | Invalid_xml s -> "Invalid XML: " ^ s
99 | Validation_error s -> "Validation: " ^ s
100 | IO_error s -> "I/O: " ^ s
101 | _ -> "Unknown error"))
102 | Error e ->
103 Printf.printf "✗ Error saving file: %s\n"
104 (match e with
105 | IO_error s -> s
106 | Validation_error s -> s
107 | _ -> "Unknown error"))
108 | Error e ->
109 Printf.printf "✗ Error generating XML: %s\n"
110 (match e with
111 | Invalid_xml s -> s
112 | Xml_error s -> s
113 | _ -> "Unknown error"))
114 | Error e ->
115 Printf.printf "✗ Error creating coordinates: %s\n"
116 (match e with Invalid_coordinate s -> s | _ -> "Unknown error"));
117
118 Printf.printf "\n=== Example Complete ===\n"