parse gpx elevation
This commit is contained in:
parent
7cfe6b4892
commit
1007404a24
22
lib/Track.hs
22
lib/Track.hs
@ -39,23 +39,33 @@ type Track = [Point]
|
|||||||
mkPoint pos =
|
mkPoint pos =
|
||||||
Point
|
Point
|
||||||
pos
|
pos
|
||||||
(Just 0)
|
Nothing
|
||||||
(UTCTime (toEnum 60631) 43200)
|
(UTCTime (toEnum 60631) 43200)
|
||||||
(Just 0)
|
(Just 0)
|
||||||
(Just 0)
|
(Just 0)
|
||||||
(Just 0)
|
(Just 0)
|
||||||
|
|
||||||
elToPoint :: Cursor -> Point
|
elToPoint :: Cursor -> Point
|
||||||
elToPoint n =
|
elToPoint c =
|
||||||
case node n of
|
case node c of
|
||||||
NodeElement (Element _ attrs _) ->
|
NodeElement (Element _ attrs _) ->
|
||||||
let
|
let
|
||||||
lat = getAttr "lat"
|
lat = getAttr "lat"
|
||||||
lon = getAttr "lon"
|
lon = getAttr "lon"
|
||||||
in mkPoint (Pos lat lon)
|
ele = child c >>= element "ele" >>= child >>= content
|
||||||
where getAttr name =
|
in Point (Pos lat lon)
|
||||||
|
(case ele of
|
||||||
|
e:[] -> Just $ asFloat e
|
||||||
|
_ -> Nothing)
|
||||||
|
(UTCTime (toEnum 60631) 43200)
|
||||||
|
(Just 0)
|
||||||
|
(Just 0)
|
||||||
|
(Just 0)
|
||||||
|
where
|
||||||
|
asFloat v = (read (Data.Text.unpack v) :: Float)
|
||||||
|
getAttr name =
|
||||||
case (Map.lookup name attrs) of
|
case (Map.lookup name attrs) of
|
||||||
Just v -> (read (Data.Text.unpack v) :: Float)
|
Just v -> asFloat v
|
||||||
_ -> 0
|
_ -> 0
|
||||||
_ -> mkPoint (Pos 0 0)
|
_ -> mkPoint (Pos 0 0)
|
||||||
|
|
||||||
|
@ -1,11 +1,21 @@
|
|||||||
module Main where
|
module Main where
|
||||||
|
|
||||||
import qualified Track (Track, Pos(..), pos, parse, length )
|
import qualified Track
|
||||||
import Test.HUnit
|
import Test.HUnit
|
||||||
import qualified System.Exit as Exit
|
import qualified System.Exit as Exit
|
||||||
import Control.Exception
|
import Control.Exception
|
||||||
import Debug.Trace (trace, traceShow)
|
import Debug.Trace (trace, traceShow)
|
||||||
|
|
||||||
|
onepoint =
|
||||||
|
"<gpx> <trk> <trkseg> \n\
|
||||||
|
\<trkpt lat=\"51\" lon=\"-0.1\"> </trkpt> \n\
|
||||||
|
\</trkseg> </trk> </gpx>"
|
||||||
|
onepointEle =
|
||||||
|
"<gpx> <trk> <trkseg> \n\
|
||||||
|
\<trkpt lat=\"51\" lon=\"-0.1\"> \n\
|
||||||
|
\ <ele>25.2</ele>\n\
|
||||||
|
\</trkpt> \n\
|
||||||
|
\</trkseg> </trk> </gpx>"
|
||||||
|
|
||||||
test1 :: Test
|
test1 :: Test
|
||||||
test1 = TestCase $
|
test1 = TestCase $
|
||||||
@ -15,10 +25,7 @@ test1 = TestCase $
|
|||||||
0 (Track.length t)
|
0 (Track.length t)
|
||||||
|
|
||||||
test2 = TestCase $
|
test2 = TestCase $
|
||||||
case Track.parse
|
case Track.parse onepoint
|
||||||
"<gpx> <trk> <trkseg> \n\
|
|
||||||
\<trkpt lat=\"51\" lon=\"-0.1\"> </trkpt> \n\
|
|
||||||
\</trkseg> </trk> </gpx>"
|
|
||||||
of
|
of
|
||||||
Left err -> assertFailure (displayException err)
|
Left err -> assertFailure (displayException err)
|
||||||
Right (p:ps) ->
|
Right (p:ps) ->
|
||||||
@ -26,10 +33,28 @@ test2 = TestCase $
|
|||||||
(Track.Pos 51.0 (-0.1))
|
(Track.Pos 51.0 (-0.1))
|
||||||
(Track.pos p)
|
(Track.pos p)
|
||||||
|
|
||||||
|
test3 = TestCase $
|
||||||
|
case Track.parse onepoint
|
||||||
|
of
|
||||||
|
Left err -> assertFailure (displayException err)
|
||||||
|
Right (p:ps) ->
|
||||||
|
assertEqual "matches elevation"
|
||||||
|
Nothing (Track.elevation p)
|
||||||
|
|
||||||
|
test4 = TestCase $
|
||||||
|
case Track.parse onepointEle
|
||||||
|
of
|
||||||
|
Left err -> assertFailure (displayException err)
|
||||||
|
Right (p:ps) ->
|
||||||
|
assertEqual "matches elevation"
|
||||||
|
(Just 25.2) (Track.elevation p)
|
||||||
|
|
||||||
tests :: Test
|
tests :: Test
|
||||||
tests = TestList [
|
tests = TestList [
|
||||||
TestLabel "test1" test1,
|
TestLabel "test1" test1,
|
||||||
TestLabel "test2" test2
|
TestLabel "test2" test2,
|
||||||
|
TestLabel "test3" test3,
|
||||||
|
TestLabel "test4" test4
|
||||||
]
|
]
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
|
Loading…
Reference in New Issue
Block a user