From: Kaj-Michael Lang Date: Sun, 22 Jul 2007 12:13:28 +0000 (+0300) Subject: Move latlon pairs distance calculation function from utils.c to latlon.c X-Git-Url: https://err.no/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a045875a07878f7e5d6bf0fddcfa66c9f04e84d0;p=mapper Move latlon pairs distance calculation function from utils.c to latlon.c --- diff --git a/src/latlon.c b/src/latlon.c index 472a983..ea4b219 100644 --- a/src/latlon.c +++ b/src/latlon.c @@ -1,3 +1,6 @@ +/* + * Different Lat/Lon related function (conversion, distances, etc) + */ #define _GNU_SOURCE #include @@ -9,6 +12,8 @@ #define LATLON_MAX 2147483646 #define LATLON_MIN -2147483646 +#define EARTH_RADIUS (3440.06479f) + /* Convert latitude to integerized+mercator projected value */ gint32 lat2mp_int(gdouble lat) @@ -24,3 +29,27 @@ lon2mp_int(gdouble lon) return lrint(lon/180*LATLON_MAX); } +/** + * Calculate the distance between two lat/lon pairs. The distance is returned + * in kilometer. + */ +gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2) +{ +gdouble dlat, dlon, slat, slon, a; + +/* Convert to radians. */ +lat1 *= (M_PI_4l / 180.f); +lon1 *= (M_PI_4l / 180.f); +lat2 *= (M_PI_4l / 180.f); +lon2 *= (M_PI_4l / 180.f); + +dlat = lat2 - lat1; +dlon = lon2 - lon1; + +slat = sinf(dlat / 2.f); +slon = sinf(dlon / 2.f); +a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon); + +return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS); +} + diff --git a/src/utils.c b/src/utils.c index 75b5238..43574cf 100644 --- a/src/utils.c +++ b/src/utils.c @@ -118,31 +118,6 @@ deg_format(DegFormat degformat, gfloat coor, gchar * scoor, gchar neg_char, vprintf("%s(): return\n", __PRETTY_FUNCTION__); } -/** - * Calculate the distance between two lat/lon pairs. The distance is returned - * in kilometers and should be converted using UNITS_CONVERT[_units]. - */ -gfloat calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2) -{ - gdouble dlat, dlon, slat, slon, a; - - /* Convert to radians. */ - lat1 *= (PI / 180.f); - lon1 *= (PI / 180.f); - lat2 *= (PI / 180.f); - lon2 *= (PI / 180.f); - - dlat = lat2 - lat1; - dlon = lon2 - lon1; - - slat = sinf(dlat / 2.f); - slon = sinf(dlon / 2.f); - a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon); - - vprintf("%s(): return\n", __PRETTY_FUNCTION__); - return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS); -} - /** * Convert the float lat/lon/speed/heading data into integer units. */