souplesse/src/Track.elm
2024-10-25 00:00:37 +01:00

40 lines
1.1 KiB
Elm

module Track exposing (Track, Point, parse)
import Result
import Xml.Decode as XD exposing (path, list, single, floatAttr, stringAttr)
import Time
import Iso8601
type alias Position = (Float, Float, Maybe Float) -- lat, lon, ele
type alias Point =
{ loc : Position
, time : Maybe Time.Posix
, power : Maybe Int
}
type alias Track = List 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
triple x y z = (x,y,z)
pointDecoder = XD.map3 Point
(XD.map3 triple (floatAttr "lat") (floatAttr "lon")
(XD.maybe (path ["ele"] (single XD.float))))
(XD.maybe (path ["time"] (single timeDecoder)))
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "pwr:PowerInWatts"] (single XD.int)))
gpxDecoder =
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
parse str = XD.run gpxDecoder str