2024-10-23 22:49:19 +00:00
|
|
|
module Track exposing (Track, Point, parse)
|
2024-10-23 22:37:29 +00:00
|
|
|
|
|
|
|
import Result
|
2024-10-24 16:14:35 +00:00
|
|
|
import Xml.Decode as XD exposing (path, list, single, floatAttr, stringAttr)
|
2024-10-24 16:59:59 +00:00
|
|
|
import Time
|
2024-10-24 16:14:35 +00:00
|
|
|
import Iso8601
|
2024-10-23 22:37:29 +00:00
|
|
|
|
2024-10-24 22:06:00 +00:00
|
|
|
type alias Position = (Float, Float, Maybe Float) -- lat, lon, ele
|
|
|
|
|
2024-10-23 22:37:29 +00:00
|
|
|
type alias Point =
|
2024-10-24 22:06:00 +00:00
|
|
|
{ loc : Position
|
2024-10-24 17:28:40 +00:00
|
|
|
, time : Maybe Time.Posix
|
2024-10-24 23:00:37 +00:00
|
|
|
, power : Maybe Int
|
2024-10-24 23:12:19 +00:00
|
|
|
, cadence : Maybe Int
|
2024-10-25 20:25:20 +00:00
|
|
|
, hr : Maybe Int
|
2024-10-23 22:37:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type alias Track = List Point
|
|
|
|
|
2024-10-24 16:14:35 +00:00
|
|
|
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
|
|
|
|
|
2024-10-24 22:06:00 +00:00
|
|
|
triple x y z = (x,y,z)
|
|
|
|
|
2024-10-25 20:25:20 +00:00
|
|
|
pointDecoder = XD.map5 Point
|
2024-10-24 22:06:00 +00:00
|
|
|
(XD.map3 triple (floatAttr "lat") (floatAttr "lon")
|
|
|
|
(XD.maybe (path ["ele"] (single XD.float))))
|
2024-10-24 17:28:40 +00:00
|
|
|
(XD.maybe (path ["time"] (single timeDecoder)))
|
2024-10-24 23:00:37 +00:00
|
|
|
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "pwr:PowerInWatts"] (single XD.int)))
|
2024-10-24 23:12:19 +00:00
|
|
|
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "gpxtpx:cad"] (single XD.int)))
|
2024-10-25 20:25:20 +00:00
|
|
|
(XD.maybe (path ["extensions", "gpxtpx:TrackPointExtension", "gpxtpx:hr"] (single XD.int)))
|
2024-10-24 16:14:35 +00:00
|
|
|
|
2024-10-23 22:37:29 +00:00
|
|
|
|
|
|
|
gpxDecoder =
|
2024-10-23 22:49:19 +00:00
|
|
|
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
|
2024-10-23 22:37:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
parse str = XD.run gpxDecoder str
|