Amongst other things, I like to keep a detailed track of where I have been [especially during hiking in the woods, venturing into areas that I have never been before, etc.]. For this purpose, I have configured my Garmin GPSMap 60CSx to record location/date-time data every three seconds and a while ago, I described another API I wrote to store these track points in MySQL. Added advantage of this, as I have mentioned before in previous posts, is that it can be used for geotagging my photographs. For completeness sake, the MySQL table structure that holds track data is given below:
CREATE TABLE IF NOT EXISTS `MyDatabase`.`TRK` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`latitude` float NULL ,
`longitude` float NULL ,
`altitude` float NULL ,
`date` date NULL ,
`time` time NULL
) ENGINE = MYISAM DEFAULT CHARSET latin1 AUTO_INCREMENT=1 ;
CREATE UNIQUE INDEX lldt ON `MyDatabase`.`TRK` (`latitude`,`longitude`,`date`,`time`);
Computing Total Travel Distance
Suppose that my GPS recorded my thanksgiving travel – starting from 2009-11-25 at 08:00:00 am EST ($start_datetime = “2009-11-25 03:00:00” ; GPS units save date & time in UTC) till 2009-11-29 at 06:00:00 pm EST ($end_time = “2009-11-29 13:00:00”) – recording track points every three seconds. While recording track points every three seconds in most cases might ensure that straight line approximation is good enough, the code below – for sake of mathematical rigor – uses Haversine formula.
To keep the test case simple and computationally expenses minimal, I used a subset of my Thanksgiving travel – with $start_datetime = “2009-11-25 23:59:30” and $end_datetime = “2009-11-27 00:00:30”. Remembering [clearly] that the odometer read 100 miles for the duration and that we [Nils (@UPBeaches) and yours truly] hiked about a mile [or two], the computed distance [PHP script took 12+ hours on a Intel(R) Xeon(R) CPU L5420 2.50GHz Linux machine with 360MB RAM running x86 version of CentOS 4.7 operating system] from the tracks came out to be 101.9471 miles – a decent agreement I would say. Wouldn’t you agree?
Any suggestions/recommendations to optimize the SQL query and speed up the computation / execution of the PHP script will be greatly appreciated.
Updates (2009.11.30)
With significant suggestions from Srichand, Peter and Jon, I modified the MySQL table structure as well as the PHP script. With the first set of improvements, date and time were merged into one field. That reduced the overall execution time from 650+ minutes to 45 minutes. Second set of improvements included adding an index on date_time field as well as modifying the definition few fields. That reduced the overall execution time to a whopping 3 seconds.
CREATE TABLE IF NOT EXISTS `MyDatabase`.`TRK` (
`id` int( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
`latitude` float NOT NULL DEFAULT '0' ,
`longitude` float NOT NULL DEFAULT '0' ,
`altitude` float NOT NULL DEFAULT '0' ,
`date_time` float NOT NULL DEFAULT '0000-00-00 00:00:00'
) ENGINE = MYISAM DEFAULT CHARSET latin1 AUTO_INCREMENT=1 ;
CREATE UNIQUE INDEX dt ON `MyDatabase`.`TRK` (`date_time`);
CREATE UNIQUE INDEX lldt ON `MyDatabase`.`TRK` (`latitude`,`longitude`,`date_time`);