commit a54f903326c13b49131f647d7f35512188604a13
parent 6b3c456c41a9661cf16b26c60be534424dcd3252
Author: Daniel Barlow <dan@telent.net>
Date: Thu, 24 Oct 2024 17:14:35 +0100
add time and elevation to gpx parsing
Diffstat:
3 files changed, 23 insertions(+), 6 deletions(-)
diff --git a/elm.json b/elm.json
@@ -9,7 +9,9 @@
"elm/browser": "1.0.2",
"elm/core": "1.0.5",
"elm/html": "1.0.0",
+ "elm/time": "1.0.0",
"elm-explorations/test": "2.2.0",
+ "rtfeldman/elm-iso8601-date-strings": "1.1.4",
"ymtszw/elm-xml-decode": "3.2.2"
},
"indirect": {
@@ -17,7 +19,6 @@
"elm/json": "1.1.3",
"elm/parser": "1.1.0",
"elm/random": "1.0.0",
- "elm/time": "1.0.0",
"elm/url": "1.0.0",
"elm/virtual-dom": "1.0.3",
"miniBill/elm-xml-parser": "1.0.1",
diff --git a/src/Track.elm b/src/Track.elm
@@ -1,18 +1,33 @@
module Track exposing (Track, Point, parse)
import Result
-import Xml.Decode as XD exposing (path, list, floatAttr)
+import Xml.Decode as XD exposing (path, list, single, floatAttr, stringAttr)
+import Time -- exposing (utc, toHour, toMinute, toSecond)
+import Iso8601
type alias Point =
{ lat : Float
, lon : Float
+ , ele : Float
+ , time : Time.Posix
}
type alias Track = List Point
-pointDecoder = XD.map2 Point
+timeDecoder : XD.Decoder Time.Posix
+timeDecoder = XD.andThen (\ts ->
+ case Iso8601.toTime ts of
+ Result.Ok time -> XD.succeed time
+ Result.Err f -> XD.fail "err"
+ )
+ XD.string
+
+pointDecoder = XD.map4 Point
(floatAttr "lat")
(floatAttr "lon")
+ (path ["ele"] (single XD.float))
+ (path ["time"] (single timeDecoder))
+
gpxDecoder =
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
diff --git 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,9 +14,9 @@ specs =
let
r = Track.parse Fixtures.threepoints
expected =
- [ (Point 51.600643 -0.01856)
- , (Point 51.600679 -0.018179)
- , (Point 51.600697 -0.018064)
+ [ (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))
]
in