Fix URL formatting issue
[viking/guyou.git] / src / degrees_converters.c
blob8e674dff576c5cee94f71ba8d756618d9cb23373
1 /*
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
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
25 #ifdef HAVE_MATH_H
26 #include <math.h>
27 #endif
28 #include <glib.h>
29 #include <string.h>
31 #define DEGREE_SYMBOL "\302\260"
33 /**
34 * @param pos_c char for positive value
35 * @param neg_c char for negative value
37 static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c)
39 gchar sign_c = ' ';
40 gdouble val_d;
41 gchar *result = NULL;
43 if ( dec > 0 )
44 sign_c = pos_c;
45 else if ( dec < 0 )
46 sign_c = neg_c;
47 else /* Nul value */
48 sign_c = ' ';
50 /* Degree */
51 val_d = fabs(dec);
53 /* Format */
54 result = g_strdup_printf ( "%c%f" DEGREE_SYMBOL, sign_c, val_d );
55 return result;
58 gchar *convert_lat_dec_to_ddd(gdouble lat)
60 return convert_dec_to_ddd(lat, 'N', 'S');
63 gchar *convert_lon_dec_to_ddd(gdouble lon)
65 return convert_dec_to_ddd(lon, 'E', 'W');
68 /**
69 * @param pos_c char for positive value
70 * @param neg_c char for negative value
72 static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
74 gdouble tmp;
75 gchar sign_c = ' ';
76 gint val_d;
77 gdouble val_m;
78 gchar *result = NULL;
80 if ( dec > 0 )
81 sign_c = pos_c;
82 else if ( dec < 0 )
83 sign_c = neg_c;
84 else /* Nul value */
85 sign_c = ' ';
87 /* Degree */
88 tmp = fabs(dec);
89 val_d = (gint)tmp;
91 /* Minutes */
92 val_m = (tmp - val_d) * 60;
94 /* Format */
95 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'",
96 sign_c, val_d, val_m );
97 return result;
100 gchar *convert_lat_dec_to_dmm(gdouble lat)
102 return convert_dec_to_dmm(lat, 'N', 'S');
105 gchar *convert_lon_dec_to_dmm(gdouble lon)
107 return convert_dec_to_dmm(lon, 'E', 'W');
111 * @param pos_c char for positive value
112 * @param neg_c char for negative value
114 static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
116 gdouble tmp;
117 gchar sign_c = ' ';
118 gint val_d, val_m;
119 gdouble val_s;
120 gchar *result = NULL;
122 if ( dec > 0 )
123 sign_c = pos_c;
124 else if ( dec < 0 )
125 sign_c = neg_c;
126 else /* Nul value */
127 sign_c = ' ';
129 /* Degree */
130 tmp = fabs(dec);
131 val_d = (gint)tmp;
133 /* Minutes */
134 tmp = (tmp - val_d) * 60;
135 val_m = (gint)tmp;
137 /* Minutes */
138 val_s = (tmp - val_m) * 60;
140 /* Format */
141 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%f\"",
142 sign_c, val_d, val_m, val_s );
143 return result;
146 gchar *convert_lat_dec_to_dms(gdouble lat)
148 return convert_dec_to_dms(lat, 'N', 'S');
151 gchar *convert_lon_dec_to_dms(gdouble lon)
153 return convert_dec_to_dms(lon, 'E', 'W');
156 gdouble convert_dms_to_dec(const gchar *dms)
158 gdouble d = 0.0; /* Degree */
159 gdouble m = 0.0; /* Minutes */
160 gdouble s = 0.0; /* Seconds */
161 gint neg = FALSE;
162 gdouble result;
164 if (dms != NULL) {
165 int nbFloat = 0;
166 const gchar *ptr, *endptr;
168 // Compute the sign
169 // It is negative if:
170 // - the '-' sign occurs
171 // - it is a west longitude or south latitude
172 if (strpbrk (dms, "-wWsS") != NULL)
173 neg = TRUE;
175 // Peek the différent components
176 endptr = dms;
177 do {
178 gdouble value;
179 ptr = strpbrk (endptr, "0123456789,.");
180 if (ptr != NULL) {
181 value = g_strtod(ptr, &endptr);
182 nbFloat++;
183 switch(nbFloat) {
184 case 1:
185 d = value;
186 break;
187 case 2:
188 m = value;
189 break;
190 case 3:
191 s = value;
192 break;
195 } while (ptr != NULL && endptr != NULL);
198 // Compute the result
199 result = d + m/60 + s/3600;
201 if (neg) result = - result;
203 return result;