2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2006-2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 * @param pos_c char for positive value
29 * @param neg_c char for negative value
31 static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c)
48 /* TODO : replace "°" as UTF-8 */
49 result = g_strdup_printf ( "%c%f°", sign_c, val_d );
53 gchar *convert_lat_dec_to_ddd(gdouble lat)
55 return convert_dec_to_ddd(lat, 'N', 'S');
58 gchar *convert_lon_dec_to_ddd(gdouble lon)
60 return convert_dec_to_ddd(lon, 'E', 'W');
64 * @param pos_c char for positive value
65 * @param neg_c char for negative value
67 static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
87 val_m = (tmp - val_d) * 60;
90 /* TODO : replace "°" as UTF-8 */
91 result = g_strdup_printf ( "%c%d°%f'",
92 sign_c, val_d, val_m );
96 gchar *convert_lat_dec_to_dmm(gdouble lat)
98 return convert_dec_to_dmm(lat, 'N', 'S');
101 gchar *convert_lon_dec_to_dmm(gdouble lon)
103 return convert_dec_to_dmm(lon, 'E', 'W');
107 * @param pos_c char for positive value
108 * @param neg_c char for negative value
110 static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
116 gchar *result = NULL;
130 tmp = (tmp - val_d) * 60;
134 val_s = (tmp - val_m) * 60;
137 /* TODO : replace "°" as UTF-8 */
138 result = g_strdup_printf ( "%c%d°%d'%f\"",
139 sign_c, val_d, val_m, val_s );
143 gchar *convert_lat_dec_to_dms(gdouble lat)
145 return convert_dec_to_dms(lat, 'N', 'S');
148 gchar *convert_lon_dec_to_dms(gdouble lon)
150 return convert_dec_to_dms(lon, 'E', 'W');
153 gdouble convert_dms_to_dec(const gchar *dms)
155 gdouble d = 0.0; /* Degree */
156 gdouble m = 0.0; /* Minutes */
157 gdouble s = 0.0; /* Seconds */
163 const gchar *ptr, *endptr;
166 // It is negative if:
167 // - the '-' sign occurs
168 // - it is a west longitude or south latitude
169 if (strpbrk (dms, "-wWsS") != NULL)
172 // Peek the différent components
176 ptr = strpbrk (endptr, "0123456789,.");
178 value = g_strtod(ptr, &endptr);
192 } while (ptr != NULL && endptr != NULL);
195 // Compute the result
196 result = d + m/60 + s/3600;
198 if (neg) result = - result;