(** Example using GPX with real Eio effects-based API This demonstrates the real Eio-based API with structured concurrency and proper resource management. **) open Gpx_eio let main env = try let fs = Eio.Stdenv.fs env in (* Create some GPS coordinates *) let lat1 = Gpx.latitude 37.7749 |> Result.get_ok in let lon1 = Gpx.longitude (-122.4194) |> Result.get_ok in let lat2 = Gpx.latitude 37.7849 |> Result.get_ok in let lon2 = Gpx.longitude (-122.4094) |> Result.get_ok in (* Create waypoints *) let waypoint1 = make_waypoint ~fs ~lat:(Gpx.latitude_to_float lat1) ~lon:(Gpx.longitude_to_float lon1) ~name:"San Francisco" () in let waypoint2 = make_waypoint ~fs ~lat:(Gpx.latitude_to_float lat2) ~lon:(Gpx.longitude_to_float lon2) ~name:"Near SF" () in (* Create a simple track from coordinates *) let track = make_track_from_coords ~fs ~name:"SF Walk" [ (37.7749, -122.4194); (37.7759, -122.4184); (37.7769, -122.4174); (37.7779, -122.4164); ] in (* Create a route *) let route = make_route_from_coords ~fs ~name:"SF Route" [ (37.7749, -122.4194); (37.7849, -122.4094); ] in (* Create GPX document with all elements *) let gpx = Gpx.make_gpx ~creator:"eio-example" in let gpx = { gpx with waypoints = [waypoint1; waypoint2]; tracks = [track]; routes = [route]; } in Printf.printf "Created GPX document with:\\n"; print_stats gpx; Printf.printf "\\n"; (* Write to file with validation *) write_validated ~fs "example_output.gpx" gpx; Printf.printf "Wrote GPX to example_output.gpx\\n"; (* Read it back and verify *) let gpx2 = read_validated ~fs "example_output.gpx" in Printf.printf "Read back GPX document with %d waypoints, %d tracks, %d routes\\n" (List.length gpx2.waypoints) (List.length gpx2.tracks) (List.length gpx2.routes); (* Extract coordinates from track *) match gpx2.tracks with | track :: _ -> let coords = track_coords track in Printf.printf "Track coordinates: %d points\\n" (List.length coords); List.iteri (fun i (lat, lon) -> Printf.printf " Point %d: %.4f, %.4f\\n" i lat lon ) coords | [] -> Printf.printf "No tracks found\\n"; Printf.printf "\\nEio example completed successfully!\\n" with | Gpx.Gpx_error err -> let error_msg = match err with | Gpx.Invalid_xml s -> "Invalid XML: " ^ s | Gpx.Invalid_coordinate s -> "Invalid coordinate: " ^ s | Gpx.Missing_required_attribute (elem, attr) -> Printf.sprintf "Missing required attribute '%s' in element '%s'" attr elem | Gpx.Missing_required_element s -> "Missing required element: " ^ s | Gpx.Validation_error s -> "Validation error: " ^ s | Gpx.Xml_error s -> "XML error: " ^ s | Gpx.IO_error s -> "I/O error: " ^ s in Printf.eprintf "GPX Error: %s\\n" error_msg; exit 1 | exn -> Printf.eprintf "Unexpected error: %s\\n" (Printexc.to_string exn); exit 1 let () = Eio_main.run main