2024-11-13 00:16:50 +00:00
|
|
|
module Point exposing(Pos, Point, decoder, downsample, duration)
|
2024-11-12 18:52:44 +00:00
|
|
|
|
|
|
|
import Json.Decode as D
|
|
|
|
|
|
|
|
type alias Pos =
|
|
|
|
{ lat : Float
|
|
|
|
, lon : Float
|
|
|
|
, ele : Maybe Float
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
type alias Point =
|
|
|
|
{ time : Float
|
|
|
|
, pos : Pos
|
|
|
|
, cadence : Maybe Int
|
|
|
|
, power : Maybe Int
|
|
|
|
, heartRate : Maybe Int
|
|
|
|
}
|
|
|
|
|
|
|
|
posDecoder : D.Decoder Pos
|
|
|
|
posDecoder = D.map3 Pos
|
|
|
|
(D.field "lat" D.float)
|
|
|
|
(D.field "lon" D.float)
|
|
|
|
(D.field "ele" (D.maybe D.float))
|
|
|
|
|
|
|
|
|
|
|
|
decoder : D.Decoder Point
|
|
|
|
decoder = D.map5 Point
|
|
|
|
(D.field "time" D.float)
|
|
|
|
(D.field "pos" posDecoder)
|
|
|
|
(D.field "cadence" (D.maybe D.int))
|
|
|
|
(D.field "power" (D.maybe D.int))
|
|
|
|
(D.field "heartRate" (D.maybe D.int))
|
2024-11-12 21:29:02 +00:00
|
|
|
|
|
|
|
|
2024-11-13 00:16:50 +00:00
|
|
|
last x xs =
|
2024-11-12 21:29:02 +00:00
|
|
|
case xs of
|
|
|
|
[] -> x
|
2024-11-13 00:16:50 +00:00
|
|
|
(x_::xs_) -> last x_ xs_
|
2024-11-12 21:29:02 +00:00
|
|
|
|
|
|
|
tracef x = Debug.log (String.fromFloat x) x
|
|
|
|
|
|
|
|
-- divide the points into n equal time buckets and return the first
|
|
|
|
-- point in each
|
|
|
|
downsample n points =
|
|
|
|
let
|
|
|
|
nextpoint step prev points_ =
|
|
|
|
case points_ of
|
|
|
|
(next::rest) ->
|
|
|
|
if next.time - prev.time > step
|
|
|
|
then prev :: (nextpoint step next rest)
|
|
|
|
else nextpoint step prev rest
|
|
|
|
_ -> [prev]
|
|
|
|
in
|
|
|
|
case points of
|
|
|
|
(first::rest) ->
|
2024-11-13 00:16:50 +00:00
|
|
|
let step = (duration points) / (toFloat n)
|
2024-11-12 21:29:02 +00:00
|
|
|
in nextpoint step first rest
|
|
|
|
[] -> []
|
2024-11-13 00:16:50 +00:00
|
|
|
|
|
|
|
duration points =
|
|
|
|
case points of
|
|
|
|
(p::ps) -> (last p ps).time - p.time
|
|
|
|
_ -> 0
|