Compare commits
2 Commits
2befb534a1
...
e15c42ff4a
Author | SHA1 | Date | |
---|---|---|---|
e15c42ff4a | |||
a3d48100ae |
63
lib/Track.hs
63
lib/Track.hs
@ -17,6 +17,7 @@ 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
|
||||
@ -52,16 +53,12 @@ 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
|
||||
|
||||
elToPoint :: Cursor -> Point
|
||||
data BadFile = BadFile deriving (Show)
|
||||
|
||||
instance Exception BadFile
|
||||
|
||||
elToPoint :: Cursor -> Either SomeException Point
|
||||
elToPoint c =
|
||||
case node c of
|
||||
NodeElement (Element _ attrs _) ->
|
||||
@ -85,37 +82,27 @@ elToPoint c =
|
||||
>>= element (Name "PowerInWatts" (Just "http://www.garmin.com/xmlschemas/PowerExtension/v1") Nothing)
|
||||
>>= child
|
||||
>>= content
|
||||
in Point
|
||||
(Pos lat lon)
|
||||
( 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
|
||||
parsedTime =
|
||||
listToMaybe ts
|
||||
>>= (Data.Time.ISO8601.parseISO8601 . Data.Text.unpack)
|
||||
in case parsedTime of
|
||||
Nothing -> Left (toException BadFile)
|
||||
Just utime ->
|
||||
Right $
|
||||
Point
|
||||
(Pos lat lon)
|
||||
(listToMaybe ele >>= return . asFloat)
|
||||
utime
|
||||
(listToMaybe cadence >>= return . asInt)
|
||||
(listToMaybe power >>= return . asInt)
|
||||
Nothing
|
||||
where
|
||||
asFloat v = (read (Data.Text.unpack v) :: Float)
|
||||
asInt v = (read (Data.Text.unpack v) :: Int)
|
||||
getAttr name =
|
||||
case Map.lookup name attrs of
|
||||
Just v -> asFloat v
|
||||
_ -> 0
|
||||
_ -> mkPoint (Pos 0 0)
|
||||
getAttr name = maybe 0 asFloat (Map.lookup name attrs)
|
||||
_ -> Left (toException BadFile)
|
||||
|
||||
getPoints :: Cursor -> [Point]
|
||||
getPoints :: Cursor -> Either SomeException [Point]
|
||||
getPoints c =
|
||||
let trkpts =
|
||||
element (gpxNS "gpx") c
|
||||
@ -123,12 +110,12 @@ getPoints c =
|
||||
>>= element (gpxNS "trk")
|
||||
>>= descendant
|
||||
>>= element (gpxNS "trkpt")
|
||||
in List.map elToPoint trkpts
|
||||
in traverse elToPoint trkpts
|
||||
|
||||
parse :: String -> Either SomeException [Point]
|
||||
parse str = do
|
||||
gpx <- parseText def (T.pack str)
|
||||
return (getPoints (fromDocument gpx))
|
||||
getPoints (fromDocument gpx)
|
||||
|
||||
length :: Track -> Int
|
||||
length = Data.List.length
|
||||
|
@ -34,7 +34,9 @@ onepoint =
|
||||
wrap
|
||||
[r|
|
||||
<trk> <trkseg>
|
||||
<trkpt lat="51" lon="-0.1"> </trkpt>
|
||||
<trkpt lat="51" lon="-0.1">
|
||||
<time>2024-10-23T08:34:59.779+01:00</time>
|
||||
</trkpt>
|
||||
</trkseg> </trk>
|
||||
|]
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user