souplesse/src/Track.elm

37 lines
912 B
Elm
Raw Normal View History

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)
import Time
2024-10-24 16:14:35 +00:00
import Iso8601
2024-10-23 22:37:29 +00:00
type alias Point =
{ lat : Float
, lon : Float
2024-10-24 16:14:35 +00:00
, ele : Float
, time : Time.Posix
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
pointDecoder = XD.map4 Point
(floatAttr "lat")
(floatAttr "lon")
2024-10-24 16:14:35 +00:00
(path ["ele"] (single XD.float))
(path ["time"] (single timeDecoder))
2024-10-23 22:37:29 +00:00
gpxDecoder =
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
2024-10-23 22:37:29 +00:00
parse str = XD.run gpxDecoder str