From a762245d865f4e17ffc3a0d00e9519b4647e70d5 Mon Sep 17 00:00:00 2001 From: Daniel Barlow Date: Thu, 24 Oct 2024 18:24:03 +0100 Subject: [PATCH] elevation is optional in gpx (so is nearly everything else, but am doing one thing at a time) --- src/Track.elm | 4 ++-- tests/Fixtures.elm | 7 ++++--- tests/TrackTest.elm | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 5 deletions(-) diff --git a/src/Track.elm b/src/Track.elm index 708ef0d..5857671 100644 --- a/src/Track.elm +++ b/src/Track.elm @@ -8,7 +8,7 @@ import Iso8601 type alias Point = { lat : Float , lon : Float - , ele : Float + , ele : Maybe Float , time : Time.Posix } @@ -25,7 +25,7 @@ timeDecoder = XD.andThen (\ts -> pointDecoder = XD.map4 Point (floatAttr "lat") (floatAttr "lon") - (path ["ele"] (single XD.float)) + (XD.maybe (path ["ele"] (single XD.float))) (path ["time"] (single timeDecoder)) diff --git a/tests/Fixtures.elm b/tests/Fixtures.elm index 84b537d..6ce2d6c 100644 --- a/tests/Fixtures.elm +++ b/tests/Fixtures.elm @@ -1,6 +1,7 @@ module Fixtures exposing (threepoints, threepoints_expected) import Track exposing (Track, Point) import Time +import Maybe threepoints = """ @@ -64,7 +65,7 @@ xsi:schemaLocation="http://www.topografix.com/GPX/1/1 http://www.topografix.com/ """ threepoints_expected = - [ (Point 51.600643 -0.01856 64.8 (Time.millisToPosix 1729669252256)) - , (Point 51.600679 -0.018179 65.5 (Time.millisToPosix 1729669255259)) - , (Point 51.600697 -0.018064 66.2 (Time.millisToPosix 1729669256231)) + [ (Point 51.600643 -0.01856 (Just 64.8) (Time.millisToPosix 1729669252256)) + , (Point 51.600679 -0.018179 (Just 65.5) (Time.millisToPosix 1729669255259)) + , (Point 51.600697 -0.018064 (Just 66.2) (Time.millisToPosix 1729669256231)) ] diff --git a/tests/TrackTest.elm b/tests/TrackTest.elm index 6135e54..dc86e67 100644 --- a/tests/TrackTest.elm +++ b/tests/TrackTest.elm @@ -4,6 +4,7 @@ import Fixtures import Track exposing (Track, Point) import Test exposing (..) import Expect exposing (Expectation) +import Time specs: Test specs = @@ -13,6 +14,19 @@ specs = case Track.parse Fixtures.threepoints of (Ok trk) -> Expect.equalLists Fixtures.threepoints_expected trk _ -> Expect.fail "not ok" + , test "missing child ele node in track point" <| + \_ -> + let + xml = String.replace + "64.8" + "" + Fixtures.threepoints + in + case Track.parse xml of + Ok (p::pts) -> Expect.equal p (Point 51.600643 -0.01856 Nothing (Time.millisToPosix 1729669252256)) + Ok _ -> Expect.fail "empty list" + (Err f) -> Expect.fail f + , test "multiple trksegs are coalesced" <| \_ -> let