souplesse/src/Track.elm

37 lines
946 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
, ele : Maybe Float
2024-10-24 17:28:40 +00:00
, time : Maybe 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")
(XD.maybe (path ["ele"] (single XD.float)))
2024-10-24 17:28:40 +00:00
(XD.maybe (path ["time"] (single timeDecoder)))
2024-10-24 16:14:35 +00:00
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