Feature: Calculate track length automatically
Title
Calculate track length automatically
Id
0054
Requestor
Ian
Status
Closed, 8 Aug 2009 04:49
Progress




Description
It would be very helpful for Tracks to calculate the track length automatically, rather than it being entered manually by the user.
Calculating the track length automatically would also help to ensure consistency between the displayed length and the distance shown on altitude charts.
The following snippet of Javascript is the basis for a track length function - it calculates the distance between two pairs of coordinates. The calculation should be done on the server side rather than by the client, so this code needs to be translated into some other language.
// Calculate length, in km, along Great Circle between two points
// on the Earth's surface, using the 'Spherical law of cosines'.
// Derived from http://www.movable-type.co.uk/scripts/latlong.html
var EarthRadius = 6369; // Earth's radius (at NZ), in km
var lat1R = lat1.toRad(); // Convert degree coordinates to radians
var lon1R = lon1.toRad();
var lat2R = lat2.toRad();
var lon2R = lon2.toRad();
var Distance = Math.acos(Math.sin(lat1) * Math.sin(lat2)
+ Math.cos(lat1) * Math.cos(lat2)
* Math.cos(lon2 - lon1)) * EarthRadius;
The kml is already parsed in the map data encoding process, including dealing with multiple track sections, so it should be fairly straightforward to iterate over all the coordinates and then update the length field in the database.
Given that a path consisting of line segments is always shorter than the real track, it would also be good to include a field for adjusting the calculated length. A typical adjustment would be in the range 1% to 5%, depending on how convoluted the real track is, how hilly it is (which could add 1% to the length), and how much detail is included in the kml data.
Created
7 Jul 2009 12:26
Updated
8 Aug 2009 04:49


Comments
Nice. I have seen this calculation before. I like the "adjustment factor" idea too.
Jonny, 6 Aug 2009 16:26