From efacb9795cc2f104f7f4787a517a2351b3f2fe29 Mon Sep 17 00:00:00 2001 From: Marcus Rohrmoser Date: Wed, 26 Jul 2023 13:34:54 +0200 Subject: [PATCH] deg,min,sec++ --- lib/route.ml | 39 +++++++++++++++++++++------------------ test/route_test.ml | 42 ++++++++++++++++++++++++++---------------- 2 files changed, 47 insertions(+), 34 deletions(-) diff --git a/lib/route.ml b/lib/route.ml index 4b139c2..7d92475 100644 --- a/lib/route.ml +++ b/lib/route.ml @@ -16,31 +16,34 @@ module P = struct let deg_min_sec = conv (fun ((d, (m, s)), he) -> - let si = match he with "S" | "W" -> -1. | _ -> 1. - and d' = d - and m' = m - and s' = s in - si *. (d' +. ((m' +. (s' /. 60.)) /. 60.))) + let si = match he with + | "S" | "W" -> -1. + | _ -> 1. + and d' = d + and m' = m + and s' = s in + si *. (d' +. ((m' +. (s' /. 60.)) /. 60.))) (fun v -> - let si = if v < 0. then -1. else 1. in - let d' = v *. si in - let d = d' |> floor in - let dr = d' -. d in - let m' = dr *. 60. in - let m = m' |> floor in - let mr' = m' -. m in - let se = mr' *. 60. in - ((d, (m, se)), if si < 0. then "S" else "N")) + let si = if v < 0. then -1. else 1. in + let d' = v *. si in + let d = d' |> floor in + let dr = d' -. d in + let m' = dr *. 60. in + let m = m' |> floor in + let mr' = m' -. m in + let se = mr' *. 60. in + ((d, (m, se)), if si < 0. then "S" else "N")) (float <* pcre "°|%C2%B0" <* ws - <&> (float <* pcre "′|%E2%80%B2" <* ws - <&> (float <* pcre "″|%E2%80%B3" <* ws)) - <&> pcre "[NSEOW]") + <&> (float <* pcre "'|′|%27|%E2%80%B2" <* ws + <&> (float <* pcre "\"|″|%22|%E2%80%B3" <* ws)) + <&> pcre "[NSEOW]") let dec = float <* opt (str "%C2%B0") let deg = conv - (fun x -> match x with `Left v | `Right v -> v) + (fun x -> match x with + | `Left v | `Right v -> v) (fun y -> (* write decimal by default *) `Left y) (dec <|> deg_min_sec) diff --git a/test/route_test.ml b/test/route_test.ml index 6156a91..79ed108 100644 --- a/test/route_test.ml +++ b/test/route_test.ml @@ -64,24 +64,24 @@ let test_deg_min_sec () = let ws = pcre "( |\t|\\+|%20)*" in conv (fun ((d, (m, s)), he) -> - let si = match he with "S" | "W" -> -1. | _ -> 1. - and d' = d - and m' = m - and s' = s in - si *. (d' +. ((m' +. (s' /. 60.)) /. 60.))) + let si = match he with "S" | "W" -> -1. | _ -> 1. + and d' = d + and m' = m + and s' = s in + si *. (d' +. ((m' +. (s' /. 60.)) /. 60.))) (fun v -> - let si = if v < 0. then -1. else 1. in - let d' = v *. si in - let d = d' |> floor in - let dr = d' -. d in - let m' = dr *. 60. in - let m = m' |> floor in - let mr' = m' -. m in - let se = mr' *. 60. in - ((d, (m, se)), if si < 0. then "S" else "N")) + let si = if v < 0. then -1. else 1. in + let d' = v *. si in + let d = d' |> floor in + let dr = d' -. d in + let m' = dr *. 60. in + let m = m' |> floor in + let mr' = m' -. m in + let se = mr' *. 60. in + ((d, (m, se)), if si < 0. then "S" else "N")) (float <* pcre "°" <* ws - <&> (float <* pcre "′" <* ws <&> (float <* pcre "″" <* ws)) - <&> pcre "[NSEW]") + <&> (float <* pcre "′" <* ws <&> (float <* pcre "″" <* ws)) + <&> pcre "[NSEW]") |> compile in let x = "47° 25′ 37″ N" |> exec dms |> Result.get_ok in @@ -91,8 +91,18 @@ let test_deg_min_sec () = (* Printf.printf "%f" x'; *) assert (47.426944 -. x' < 1.e-16) +let test_2023_07 () = + let la, lo = + "q=53%C2%B0+01%27+48%22+N%2C+13%C2%B0+18%27+27%22+E" + |> coord_from_qs |> Result.get_ok + in + la |> Assert2.equals_float "route test_2023_07" 1e-9 47.427; + lo |> Assert2.equals_float "route test_2023_07" 1e-9 13.059; + assert true + let () = test_qs (); test_deg (); test_deg_min_sec (); + test_2023_07 (); assert true -- 2.45.2