Compare commits

..

No commits in common. "e15c42ff4a274db387e0af843c226f1e59021bb9" and "2befb534a17c45cc8d1a6541362efe0db421ca25" have entirely different histories.

2 changed files with 39 additions and 28 deletions

View File

@ -17,7 +17,6 @@ import Control.Exception
import Data.List as List
import Data.List qualified
import Data.Map as Map
import Data.Maybe
import Data.Text qualified
import Data.Text.Lazy as T
import Data.Time
@ -53,12 +52,16 @@ gpxNS localName =
tpxNS localName =
Name localName (Just "http://www.garmin.com/xmlschemas/TrackPointExtension/v2") Nothing
mkPoint pos =
Point
pos
Nothing
(UTCTime (toEnum 60631) 43200)
Nothing
Nothing
Nothing
data BadFile = BadFile deriving (Show)
instance Exception BadFile
elToPoint :: Cursor -> Either SomeException Point
elToPoint :: Cursor -> Point
elToPoint c =
case node c of
NodeElement (Element _ attrs _) ->
@ -82,27 +85,37 @@ elToPoint c =
>>= element (Name "PowerInWatts" (Just "http://www.garmin.com/xmlschemas/PowerExtension/v1") Nothing)
>>= child
>>= content
parsedTime =
listToMaybe ts
>>= (Data.Time.ISO8601.parseISO8601 . Data.Text.unpack)
in case parsedTime of
Nothing -> Left (toException BadFile)
Just utime ->
Right $
Point
in Point
(Pos lat lon)
(listToMaybe ele >>= return . asFloat)
utime
(listToMaybe cadence >>= return . asInt)
(listToMaybe power >>= return . asInt)
( case ele of
[e] -> Just $ asFloat e
_ -> Nothing
)
( case ts of
[e] -> case Data.Time.ISO8601.parseISO8601 (Data.Text.unpack e) of
Just utime -> utime
_ -> UTCTime (toEnum 0) 0
_ -> UTCTime (toEnum 0) 0
)
( case cadence of
[e] -> Just (asInt e)
_ -> Nothing
)
( case power of
[e] -> Just (asInt e)
_ -> Nothing
)
Nothing
where
asFloat v = (read (Data.Text.unpack v) :: Float)
asInt v = (read (Data.Text.unpack v) :: Int)
getAttr name = maybe 0 asFloat (Map.lookup name attrs)
_ -> Left (toException BadFile)
getAttr name =
case Map.lookup name attrs of
Just v -> asFloat v
_ -> 0
_ -> mkPoint (Pos 0 0)
getPoints :: Cursor -> Either SomeException [Point]
getPoints :: Cursor -> [Point]
getPoints c =
let trkpts =
element (gpxNS "gpx") c
@ -110,12 +123,12 @@ getPoints c =
>>= element (gpxNS "trk")
>>= descendant
>>= element (gpxNS "trkpt")
in traverse elToPoint trkpts
in List.map elToPoint trkpts
parse :: String -> Either SomeException [Point]
parse str = do
gpx <- parseText def (T.pack str)
getPoints (fromDocument gpx)
return (getPoints (fromDocument gpx))
length :: Track -> Int
length = Data.List.length

View File

@ -34,9 +34,7 @@ onepoint =
wrap
[r|
<trk> <trkseg>
<trkpt lat="51" lon="-0.1">
<time>2024-10-23T08:34:59.779+01:00</time>
</trkpt>
<trkpt lat="51" lon="-0.1"> </trkpt>
</trkseg> </trk>
|]