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
26 #define DEGREE_SYMBOL "\302\260"
29 * @param pos_c char for positive value
30 * @param neg_c char for negative value
32 static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c)
49 result = g_strdup_printf ( "%c%f" DEGREE_SYMBOL, 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 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'",
91 sign_c, val_d, val_m );
95 gchar *convert_lat_dec_to_dmm(gdouble lat)
97 return convert_dec_to_dmm(lat, 'N', 'S');
100 gchar *convert_lon_dec_to_dmm(gdouble lon)
102 return convert_dec_to_dmm(lon, 'E', 'W');
106 * @param pos_c char for positive value
107 * @param neg_c char for negative value
109 static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
115 gchar *result = NULL;
129 tmp = (tmp - val_d) * 60;
133 val_s = (tmp - val_m) * 60;
136 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%f\"",
137 sign_c, val_d, val_m, val_s );
141 gchar *convert_lat_dec_to_dms(gdouble lat)
143 return convert_dec_to_dms(lat, 'N', 'S');
146 gchar *convert_lon_dec_to_dms(gdouble lon)
148 return convert_dec_to_dms(lon, 'E', 'W');
151 gdouble convert_dms_to_dec(const gchar *dms)
153 gdouble d = 0.0; /* Degree */
154 gdouble m = 0.0; /* Minutes */
155 gdouble s = 0.0; /* Seconds */
161 const gchar *ptr, *endptr;
164 // It is negative if:
165 // - the '-' sign occurs
166 // - it is a west longitude or south latitude
167 if (strpbrk (dms, "-wWsS") != NULL)
170 // Peek the différent components
174 ptr = strpbrk (endptr, "0123456789,.");
176 value = g_strtod(ptr, &endptr);
190 } while (ptr != NULL && endptr != NULL);
193 // Compute the result
194 result = d + m/60 + s/3600;
196 if (neg) result = - result;