37 lines
946 B
Elm
37 lines
946 B
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 Point =
|
|
{ lat : Float
|
|
, lon : Float
|
|
, ele : Maybe Float
|
|
, time : Maybe Time.Posix
|
|
}
|
|
|
|
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
|
|
|
|
pointDecoder = XD.map4 Point
|
|
(floatAttr "lat")
|
|
(floatAttr "lon")
|
|
(XD.maybe (path ["ele"] (single XD.float)))
|
|
(XD.maybe (path ["time"] (single timeDecoder)))
|
|
|
|
|
|
gpxDecoder =
|
|
(path [ "trk", "trkseg", "trkpt" ] (list pointDecoder))
|
|
|
|
|
|
parse str = XD.run gpxDecoder str
|