Permalink
Browse files

cleaned up non-exhaustive functions, and redundant imports

  • Loading branch information...
1 parent fe21d8b commit 3af1637253c02fe96a78d609477fee91d375605b Rob Stewart committed Aug 9, 2011
@@ -6,7 +6,7 @@ module Gps2HtmlReport.HTMLGenerator where
import Text.Html
import Data.GPS hiding (src,link,href)
import Text.Printf
-import Data.Time.Clock
+import Data.Maybe
import Gps2HtmlReport.JourneyStats
@@ -34,18 +34,21 @@ statsTable points =
tblHeader2 = th $ stringToHtml "Elevation"
tblHeader3 = th $ stringToHtml "Speed"
distTravelled = printf "%.2f" $ journeyDistance points
- maxElevation = printf "%.1f" $ snd (findPoint points (head points) ele (>))
- minElevation = printf "%.1f" $ snd (findPoint points (head points) ele (<))
+ maybeMaxElePt = findPoint points (head points) ele (>)
+ maybeMinElePt = findPoint points (head points) ele (<)
+ maxElevation = printf "%.1f" (if isJust maybeMaxElePt then snd $ fromJust maybeMaxElePt else 0.0)
+ minElevation = printf "%.1f" (if isJust maybeMinElePt then snd $ fromJust maybeMinElePt else 0.0)
meanEle = printf "%.1f" $ meanElevation points
journeyMins = show $ round (journeyTime points) `div` 60
journeySecs = show $ round (journeyTime points) `rem` 60
mxSpd = printf "%.1f" $ maxSpeed points
meanSpd = printf "%.1f" $ meanJourneySpeed points
- journeyDate = show $ dateOfJourney points
+ maybeDateOfJourney = dateOfJourney points
+ journeyDate = (if isJust maybeDateOfJourney then show $ fromJust maybeDateOfJourney else "")
li1c1 = li $ stringToHtml ("Journey Date: "++ journeyDate)
li2c1 = li $ stringToHtml ("Distance Travelled: "++ distTravelled++"m")
li3c1 = li $ stringToHtml ("Journey Time: "++journeyMins++"m "++journeySecs++"s")
- li1c2 = li $ stringToHtml ("Maximum Elevation: "++maxElevation++"m")
+ li1c2 = li $ stringToHtml ("Maximum Elevation: "++ maxElevation++"m")
li2c2 = li $ stringToHtml ("Minimum Elevation: "++minElevation++"m")
li3c2 = li $ stringToHtml ("Mean Elevation: "++ meanEle++"m")
li1c3 = li $ stringToHtml ("Maximum speed: "++ mxSpd++"m/s")
@@ -5,21 +5,10 @@ module Gps2HtmlReport.JourneyCharts where
import Data.GPS
import Data.Maybe
-import qualified Graphics.Rendering.Cairo as C
-import Graphics.Rendering.Cairo
import Graphics.Rendering.Chart
-import Graphics.Rendering.Chart.Simple
-import Graphics.Rendering.Chart.Gtk
-import Graphics.Rendering.Chart.Grid
-import System.Environment(getArgs)
-import System.Random
-import Data.Time.LocalTime
import Data.Accessor
-import Data.Accessor.Tuple
import Data.Colour
import Data.Colour.Names
-import Data.Colour.SRGB
-import Text.XML.XSD.DateTime
import Gps2HtmlReport.JourneyStats
@@ -45,7 +34,7 @@ speedAndElevationOverTimeChart points otype = toRenderable layout
Left (toPlot avrSpeedLine)
]
$ setLayout1Foreground fg
- $ defaultLayout1
+ defaultLayout1
lineStyle c = line_width ^= 1 * chooseLineWidth otype
$ line_color ^= c
@@ -80,12 +69,12 @@ accumDistanceAndElevationChart points otype = toRenderable layout
$ layout1_background ^= solidFillStyle (opaque white)
$ layout1_right_axis ^= defaultLayoutAxis { laxis_title_ = "Distance (metres)", laxis_override_ = axisGridHide }
$ layout1_left_axis ^: laxis_title ^= "Elevation (metres)"
- $ layout1_plots ^= [ Right (toPlot accumDistanceArea),
+ $ layout1_plots ^= [ Right (toPlot accumDistanceArea),
Left (toPlot elevationLine),
Left (toPlot spots)
]
$ setLayout1Foreground fg
- $ defaultLayout1
+ defaultLayout1
lineStyle c = line_width ^= 1 * chooseLineWidth otype
$ line_color ^= c
@@ -101,25 +90,25 @@ accumDistanceAndElevationChart points otype = toRenderable layout
$ plot_fillbetween_title ^= "Distance"
$ defaultPlotFillBetween
- spotMaxPoint = let (a,b) = findPoint points (head points) ele (>)
- in (a,b,5::Double)
+ spotMaxPoint = let point = findPoint points (head points) ele (>)
+ in (if isJust point then Just (fst $ fromJust point, snd $ fromJust point, 5 :: Double) else Nothing)
- spotMinPoint = let (a,b) = findPoint points (head points) ele (<)
- in (a,b,5::Double)
+ spotMinPoint = let point = findPoint points (head points) ele (<)
+ in (if isJust point then Just (fst $ fromJust point, snd $ fromJust point, 5 :: Double) else Nothing)
spots = area_spots_title ^= "Altitude"
$ area_spots_max_radius ^= 5
- $ area_spots_values ^= [spotMinPoint,spotMaxPoint]
+ $ area_spots_values ^= (if isJust spotMinPoint && isJust spotMaxPoint then [fromJust spotMinPoint,fromJust spotMaxPoint] else [])
$ defaultAreaSpots
fg = opaque black
renderToPng :: (t, OutputType -> Renderable a) -> FilePath -> IO (PickFn a)
-renderToPng (n,ir) = renderableToPNGFile (ir Window) 384 288
+renderToPng (_,ir) = renderableToPNGFile (ir Window) 384 288
-chart1 :: [WptType] -> ([Char], OutputType -> Renderable ())
+chart1 :: [WptType] -> (String, OutputType -> Renderable ())
chart1 points = ("speedAndElevationOverTimeChart", speedAndElevationOverTimeChart points)
-chart2 :: [WptType] -> ([Char], OutputType -> Renderable ())
+chart2 :: [WptType] -> (String, OutputType -> Renderable ())
chart2 points = ("accumDistanceAndElevationChart", accumDistanceAndElevationChart points)
@@ -4,13 +4,9 @@ module Gps2HtmlReport.JourneyStats where
import Data.GPS hiding (speed)
import Data.Maybe
-import System.Random
import Data.Time.LocalTime
import Data.Time.Clock
import Data.Time.Calendar
-import Data.Colour
-import Data.Colour.Names
-import Data.Colour.SRGB
import Text.XML.XSD.DateTime
-- | Takes all WayPoints, and creates a list of tuples containing (TimeStamp,Elevation)
@@ -19,14 +15,17 @@ ptsElevation = map (\point -> (utcToLocalTime dfltTZ (Text.XML.XSD.DateTime.toUT
-- | Takes all WayPoints, and creates a list of tuples containing (TimeStamp,AvrSpeedAtThisPoint)
avrSpeedOverTime :: [(LocalTime,Speed)] -> Double -> Double -> [(LocalTime,Speed)] -> [(LocalTime,Speed)]
+avrSpeedOverTime [] _ _ _ = []
avrSpeedOverTime [spd] totalSpeed numPoints iteratedAvr = iteratedAvr ++ [(fst spd, (snd spd + totalSpeed) / (numPoints+1))]
avrSpeedOverTime (spd:spds) totalSpeed numPoints iteratedAvr = avrSpeedOverTime [spd] totalSpeed numPoints iteratedAvr ++ avrSpeedOverTime spds (snd spd + totalSpeed) (numPoints+1) iteratedAvr
-- | Takes all WayPoints, and creates a list of tuples containing (TimeStamp,SpeedAtThisPoint)
speedAtPoints :: [WptType] -> [(LocalTime,Speed)]
-speedAtPoints points = speedAtPoints' (head points) (tail points)
+speedAtPoints [] = []
+speedAtPoints (point:points) = (lclTime $ fromJust (time point),0.0) : speedAtPoints' point points
speedAtPoints' :: WptType -> [WptType] -> [(LocalTime, Speed)]
+speedAtPoints' _ [] = []
speedAtPoints' prev [x]
| isJust (time x) = [(lclTime $ fromJust (time x), fromJust $ speed prev x)]
| otherwise = []
@@ -36,22 +35,25 @@ speedAtPoints' prev (x:xs)
-- | Takes all WayPoints, and creates a list of tuples containing (TimeStamp,JourneyDistanceAtPoint)
accumDistance :: [WptType] -> Double -> [(LocalTime,Distance)]
-accumDistance [x] acc = [(lclTime $ fromJust (time x),0.0)]
+accumDistance [] _ = []
+accumDistance [x] _ = [(lclTime $ fromJust (time x),0.0)]
accumDistance (x:xs) acc =
let dist = distance x (head xs)
in (lclTime $ fromJust (time x), dist + acc ) : accumDistance (tail xs) (dist + acc)
-- | Takes all WayPoints, an element in wptType, and an Eq function, returning a single WayPoint
-findPoint :: [WptType] -> WptType -> (WptType -> Maybe Double) -> (Double -> Double -> Bool) -> (LocalTime,Double)
+findPoint :: [WptType] -> WptType -> (WptType -> Maybe Double) -> (Double -> Double -> Bool) -> Maybe (LocalTime,Double)
+findPoint [] _ _ _ = Nothing
findPoint (point:points) currSelected wayPointElement equalityF
| equalityF (fromJust $ wayPointElement point) (fromJust $ wayPointElement currSelected) = findPoint points point wayPointElement equalityF
| otherwise = if null points
- then (lclTime (fromJust $ time currSelected), fromJust $ ele currSelected)
+ then Just (lclTime (fromJust $ time currSelected), fromJust $ ele currSelected)
else findPoint points currSelected wayPointElement equalityF
-- | Calculates the total journey distance
journeyDistance :: (Lat a, Lon a) => [a] -> Distance
-journeyDistance [point] = 0.0
+journeyDistance [] = 0.0
+journeyDistance [_] = 0.0
journeyDistance (point:points) = distance point (head points) + journeyDistance points
-- | Calculates the average speed of the journey
@@ -76,14 +78,17 @@ meanElevation points =
-- | Calculates the total journey time
journeyTime :: Time a => [a] -> NominalDiffTime
-journeyTime points =
- let startTime = toUTCTime (fromJust (time $ head points))
+journeyTime [] = fromInteger 0
+journeyTime [_] = fromInteger 0
+journeyTime (point:points) =
+ let startTime = toUTCTime (fromJust (time point))
endTime = toUTCTime (fromJust (time $ last points))
in diffUTCTime endTime startTime
-- | Extracts the date of the journey (from the first WayPoint)
-dateOfJourney :: Time a => [a] -> Day
-dateOfJourney points = utctDay $ toUTCTime $ fromJust (time $ head points)
+dateOfJourney :: Time a => [a] -> Maybe Day
+dateOfJourney [] = Nothing
+dateOfJourney (point:_) = Just $ utctDay $ toUTCTime $ fromJust (time point)
lclTime :: DateTime -> LocalTime
lclTime dteTime = utcToLocalTime dfltTZ (Text.XML.XSD.DateTime.toUTCTime dteTime)
View
@@ -1,15 +1,8 @@
module Main where
import Data.GPS
-import qualified Graphics.Rendering.Cairo as C
-import Graphics.Rendering.Chart
-import Graphics.Rendering.Chart.Simple
-import Graphics.Rendering.Chart.Gtk
-import Graphics.Rendering.Chart.Grid
import System.FilePath
import System.Directory
-import System.Process
-import System.Environment
import Text.Html
import Gps2HtmlReport.OsmChart
@@ -22,7 +15,7 @@ main = do
curDir <- getCurrentDirectory
allFiles <- getDirectoryContents curDir
let allFilesSplit = map splitExtension allFiles
- let gpxFiles = filter (\(a,b) -> b==".gpx") allFilesSplit
+ let gpxFiles = filter (\(_,b) -> b==".gpx") allFilesSplit
putStr ("Processing "++show (length gpxFiles)++" file(s)...\n")
mapM (\(a,b) -> generateReport (curDir++"/"++a) (a++b)) gpxFiles
@@ -36,10 +29,13 @@ createEmptyDir dir = do
generateReport :: FilePath -> FilePath -> IO ()
generateReport webDir gpxFile = do
points <- readGPX gpxFile
- createEmptyDir webDir
- renderToPng (chart1 points) (webDir++"/chart1.png")
- renderToPng (chart2 points) (webDir++"/chart2.png")
- writeFile (webDir++"/index.html") $ renderHtml $ generateHtmlPage points
- createOsmMap webDir gpxFile
- putStr $ "Processing '"++gpxFile++"' complete. Report saved in: "++webDir++"/index.html\n"
- return ()
+ case length points of
+ 0 -> putStr "Unable to parse GPX file. Skipping..."
+ otherwise -> do
+ createEmptyDir webDir
+ renderToPng (chart1 points) (webDir++"/chart1.png")
+ renderToPng (chart2 points) (webDir++"/chart2.png")
+ writeFile (webDir++"/index.html") $ renderHtml $ generateHtmlPage points
+ createOsmMap webDir gpxFile
+ putStr $ "Processing '"++gpxFile++"' complete. Report saved in: "++webDir++"/index.html\n"
+ return ()
@@ -28,13 +28,13 @@ resizeImg imgPath img dimensions = do
savePngFile imgPath resizedImg
-- | Calls the perl `gpx2png' utility to download the relevant OSM tiles
-createOsmMap :: [Char] -> [Char] -> IO [()]
+createOsmMap :: String -> String -> IO [()]
createOsmMap webDir gpxFile = do
(_,_,_,pid) <- createProcess (shell ("perl gpx2png/gpx2png.pl -q -o "++webDir++"/osm.png "++ gpxFile))
waitForProcess pid
fitToWidth $ webDir++"/osm.png"
curDir <- getCurrentDirectory
allFiles <- getDirectoryContents curDir
let allFilesSplit = map splitExtension allFiles
- let tmpPngFiles = filter (\(a,b) -> b==".png") allFilesSplit
+ let tmpPngFiles = filter (\(_,b) -> b==".png") allFilesSplit
mapM (\(a,b) -> removeFile (a++b) ) tmpPngFiles

0 comments on commit 3af1637

Please sign in to comment.