souplesse/lib/Track.hs

90 lines
1.8 KiB
Haskell
Raw Normal View History

2024-10-28 23:35:36 +00:00
{-# LANGUAGE OverloadedStrings #-}
2024-10-27 23:13:39 +00:00
module Track where
import Data.Time
import qualified Data.List
2024-10-28 23:35:36 +00:00
-- import qualified Text.XML as X
import Text.XML
import Text.XML.Cursor as Cursor
import qualified Data.Text
-- import qualified Data.Text.Lazy
import Data.Text.Lazy as T
import Debug.Trace (trace, traceShow)
import Data.List as List
import Data.Map as Map
import Control.Exception
data Pos = Pos Float Float Float deriving (Show)
2024-10-27 23:13:39 +00:00
2024-10-28 23:35:36 +00:00
type Power = Maybe Int
type Cadence = Maybe Int
type HeartRate = Maybe Int
data Point = Point {
pos :: Pos,
time :: UTCTime,
power :: Power,
cadence :: Cadence,
heartRate :: HeartRate
} deriving (Show)
2024-10-27 23:13:39 +00:00
type Track = [Point]
2024-10-28 23:35:36 +00:00
mkPoint pos =
Point
pos
(UTCTime (toEnum 60631) 43200)
(Just 0)
(Just 0)
(Just 0)
parse :: String -> Either SomeException [Point]
parse' str = [
Point
(Pos 51.6 0 0)
(UTCTime (toEnum 60631) 43200)
(Just 0)
(Just 0)
(Just 0)
2024-10-27 23:13:39 +00:00
]
2024-10-28 23:35:36 +00:00
elToPoint :: Cursor -> Point
elToPoint n =
trace "el" $
case traceShow (node n) (node n) of
NodeElement (Element _ attrs _) ->
let
lat = traceShow (getAttr "lat") (getAttr "lat")
lon = getAttr "lon"
in mkPoint (Pos lat lon 0)
where getAttr name =
case (Map.lookup name attrs) of
Just v -> (read (Data.Text.unpack v) :: Float)
_ -> 0
_ -> mkPoint (Pos 1 2 3)
getPoints :: Cursor -> [Point]
getPoints c =
let
trkpts =
child c >>=
-- element "gpx" >>= child >>=
element "trk" >>= descendant >>=
element "trkpt"
in
List.map elToPoint trkpts
parse str =
case parseText def (T.pack str) of
Right gpx ->
let
points = getPoints $ fromDocument gpx
in traceShow "(gpx)" (Right points)
Left err ->
Left err
2024-10-27 23:13:39 +00:00
length :: Track -> Int
length trk = Data.List.length trk