2 * Different Lat/Lon related function (conversion, distances, etc)
11 /* Int ranges for integerized lat/lon */
12 #define LATLON_MAX 2147483646
13 #define LATLON_MIN -2147483646
15 #define EARTH_RADIUS (3440.06479f)
17 /* Convert latitude to integerized+mercator projected value */
19 lat2mp_int(gdouble lat)
21 return lat > 85.051128779 ? LATLON_MAX : lat < -85.051128779 ? LATLON_MIN :
22 lrint(log(tan(M_PI_4l+lat*M_PIl/360))/M_PIl*LATLON_MAX);
25 /* Convert longitude to integerized+mercator projected value */
27 lon2mp_int(gdouble lon)
29 return lrint(lon/180*LATLON_MAX);
33 calculate_idistance(gint lat1, gint lon1, gint lat2, gint lon2)
35 return lrint(sqrt((double)((lat1-lat2)*(lat1-lat2)+(lon1-lon2)*(lon1-lon2))));
39 * Calculate the distance between two lat/lon pairs. The distance is returned
43 calculate_distance(gfloat lat1, gfloat lon1, gfloat lat2, gfloat lon2)
45 gdouble dlat, dlon, slat, slon, a;
47 /* Convert to radians. */
48 lat1 *= (M_PI_4l / 180.f);
49 lon1 *= (M_PI_4l / 180.f);
50 lat2 *= (M_PI_4l / 180.f);
51 lon2 *= (M_PI_4l / 180.f);
56 slat = sinf(dlat / 2.f);
57 slon = sinf(dlon / 2.f);
58 a = (slat * slat) + (cosf(lat1) * cosf(lat2) * slon * slon);
60 return ((2.f * atan2f(sqrtf(a), sqrtf(1.f - a))) * EARTH_RADIUS);