GPS Exchange Format library/CLI in OCaml
1(** Example using GPX with real Eio effects-based API 2 3 This demonstrates the real Eio-based API with structured concurrency 4 and proper resource management. 5**) 6 7open Gpx_eio 8 9let main env = 10 try 11 let fs = Eio.Stdenv.fs env in 12 13 (* Create some GPS coordinates *) 14 let lat1 = Gpx.latitude 37.7749 |> Result.get_ok in 15 let lon1 = Gpx.longitude (-122.4194) |> Result.get_ok in 16 let lat2 = Gpx.latitude 37.7849 |> Result.get_ok in 17 let lon2 = Gpx.longitude (-122.4094) |> Result.get_ok in 18 19 (* Create waypoints *) 20 let waypoint1 = make_waypoint ~fs ~lat:(Gpx.latitude_to_float lat1) ~lon:(Gpx.longitude_to_float lon1) ~name:"San Francisco" () in 21 let waypoint2 = make_waypoint ~fs ~lat:(Gpx.latitude_to_float lat2) ~lon:(Gpx.longitude_to_float lon2) ~name:"Near SF" () in 22 23 (* Create a simple track from coordinates *) 24 let track = make_track_from_coords ~fs ~name:"SF Walk" [ 25 (37.7749, -122.4194); 26 (37.7759, -122.4184); 27 (37.7769, -122.4174); 28 (37.7779, -122.4164); 29 ] in 30 31 (* Create a route *) 32 let route = make_route_from_coords ~fs ~name:"SF Route" [ 33 (37.7749, -122.4194); 34 (37.7849, -122.4094); 35 ] in 36 37 (* Create GPX document with all elements *) 38 let gpx = Gpx.make_gpx ~creator:"eio-example" in 39 let gpx = { gpx with 40 waypoints = [waypoint1; waypoint2]; 41 tracks = [track]; 42 routes = [route]; 43 } in 44 45 Printf.printf "Created GPX document with:\\n"; 46 print_stats gpx; 47 Printf.printf "\\n"; 48 49 (* Write to file with validation *) 50 write_validated ~fs "example_output.gpx" gpx; 51 Printf.printf "Wrote GPX to example_output.gpx\\n"; 52 53 (* Read it back and verify *) 54 let gpx2 = read_validated ~fs "example_output.gpx" in 55 Printf.printf "Read back GPX document with %d waypoints, %d tracks, %d routes\\n" 56 (List.length gpx2.waypoints) (List.length gpx2.tracks) (List.length gpx2.routes); 57 58 (* Extract coordinates from track *) 59 match gpx2.tracks with 60 | track :: _ -> 61 let coords = track_coords track in 62 Printf.printf "Track coordinates: %d points\\n" (List.length coords); 63 List.iteri (fun i (lat, lon) -> 64 Printf.printf " Point %d: %.4f, %.4f\\n" i lat lon 65 ) coords 66 | [] -> Printf.printf "No tracks found\\n"; 67 68 Printf.printf "\\nEio example completed successfully!\\n" 69 70 with 71 | Gpx.Gpx_error err -> 72 let error_msg = match err with 73 | Gpx.Invalid_xml s -> "Invalid XML: " ^ s 74 | Gpx.Invalid_coordinate s -> "Invalid coordinate: " ^ s 75 | Gpx.Missing_required_attribute (elem, attr) -> 76 Printf.sprintf "Missing required attribute '%s' in element '%s'" attr elem 77 | Gpx.Missing_required_element s -> "Missing required element: " ^ s 78 | Gpx.Validation_error s -> "Validation error: " ^ s 79 | Gpx.Xml_error s -> "XML error: " ^ s 80 | Gpx.IO_error s -> "I/O error: " ^ s 81 in 82 Printf.eprintf "GPX Error: %s\\n" error_msg; 83 exit 1 84 | exn -> 85 Printf.eprintf "Unexpected error: %s\\n" (Printexc.to_string exn); 86 exit 1 87 88let () = Eio_main.run main