]>
Commit | Line | Data |
---|---|---|
8d098cbb GB |
1 | /* |
2 | * viking -- GPS Data and Topo Analyzer, Explorer, and Manager | |
3 | * | |
4 | * Copyright (C) 2006-2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> | |
5 | * | |
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. | |
10 | * | |
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. | |
15 | * | |
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 | |
19 | * | |
20 | */ | |
8c00358d GB |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
8d098cbb | 24 | |
8c00358d | 25 | #ifdef HAVE_MATH_H |
9d3e06a4 | 26 | #include <math.h> |
8c00358d | 27 | #endif |
9d3e06a4 | 28 | #include <glib.h> |
2162b266 GB |
29 | #include <string.h> |
30 | ||
a58aaed4 GB |
31 | #define DEGREE_SYMBOL "\302\260" |
32 | ||
16dbae57 GB |
33 | /** |
34 | * @param pos_c char for positive value | |
35 | * @param neg_c char for negative value | |
36 | */ | |
37 | static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c) | |
38 | { | |
39 | gchar sign_c = ' '; | |
40 | gdouble val_d; | |
41 | gchar *result = NULL; | |
42 | ||
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 = ' '; | |
49 | ||
50 | /* Degree */ | |
51 | val_d = fabs(dec); | |
52 | ||
53 | /* Format */ | |
a58aaed4 | 54 | result = g_strdup_printf ( "%c%f" DEGREE_SYMBOL, sign_c, val_d ); |
16dbae57 GB |
55 | return result; |
56 | } | |
57 | ||
58 | gchar *convert_lat_dec_to_ddd(gdouble lat) | |
59 | { | |
60 | return convert_dec_to_ddd(lat, 'N', 'S'); | |
61 | } | |
62 | ||
63 | gchar *convert_lon_dec_to_ddd(gdouble lon) | |
64 | { | |
65 | return convert_dec_to_ddd(lon, 'E', 'W'); | |
66 | } | |
67 | ||
2162b266 GB |
68 | /** |
69 | * @param pos_c char for positive value | |
70 | * @param neg_c char for negative value | |
71 | */ | |
72 | static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c) | |
73 | { | |
74 | gdouble tmp; | |
75 | gchar sign_c = ' '; | |
76 | gint val_d; | |
77 | gdouble val_m; | |
78 | gchar *result = NULL; | |
79 | ||
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 = ' '; | |
86 | ||
87 | /* Degree */ | |
88 | tmp = fabs(dec); | |
89 | val_d = (gint)tmp; | |
90 | ||
91 | /* Minutes */ | |
92 | val_m = (tmp - val_d) * 60; | |
93 | ||
94 | /* Format */ | |
a58aaed4 | 95 | result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'", |
2162b266 GB |
96 | sign_c, val_d, val_m ); |
97 | return result; | |
98 | } | |
99 | ||
100 | gchar *convert_lat_dec_to_dmm(gdouble lat) | |
101 | { | |
102 | return convert_dec_to_dmm(lat, 'N', 'S'); | |
103 | } | |
104 | ||
105 | gchar *convert_lon_dec_to_dmm(gdouble lon) | |
106 | { | |
107 | return convert_dec_to_dmm(lon, 'E', 'W'); | |
108 | } | |
9d3e06a4 | 109 | |
f88152fe GB |
110 | /** |
111 | * @param pos_c char for positive value | |
112 | * @param neg_c char for negative value | |
113 | */ | |
114 | static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c) | |
9d3e06a4 GB |
115 | { |
116 | gdouble tmp; | |
f88152fe GB |
117 | gchar sign_c = ' '; |
118 | gint val_d, val_m; | |
119 | gdouble val_s; | |
9d3e06a4 GB |
120 | gchar *result = NULL; |
121 | ||
f88152fe GB |
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 = ' '; | |
9d3e06a4 GB |
128 | |
129 | /* Degree */ | |
f88152fe GB |
130 | tmp = fabs(dec); |
131 | val_d = (gint)tmp; | |
9d3e06a4 GB |
132 | |
133 | /* Minutes */ | |
f88152fe GB |
134 | tmp = (tmp - val_d) * 60; |
135 | val_m = (gint)tmp; | |
9d3e06a4 GB |
136 | |
137 | /* Minutes */ | |
f88152fe | 138 | val_s = (tmp - val_m) * 60; |
9d3e06a4 GB |
139 | |
140 | /* Format */ | |
81eb3c04 | 141 | result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%.4f\"", |
f88152fe | 142 | sign_c, val_d, val_m, val_s ); |
9d3e06a4 GB |
143 | return result; |
144 | } | |
145 | ||
f88152fe | 146 | gchar *convert_lat_dec_to_dms(gdouble lat) |
9d3e06a4 | 147 | { |
f88152fe GB |
148 | return convert_dec_to_dms(lat, 'N', 'S'); |
149 | } | |
9d3e06a4 | 150 | |
f88152fe GB |
151 | gchar *convert_lon_dec_to_dms(gdouble lon) |
152 | { | |
153 | return convert_dec_to_dms(lon, 'E', 'W'); | |
9d3e06a4 GB |
154 | } |
155 | ||
156 | gdouble convert_dms_to_dec(const gchar *dms) | |
157 | { | |
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; | |
163 | ||
164 | if (dms != NULL) { | |
165 | int nbFloat = 0; | |
2162b266 | 166 | const gchar *ptr, *endptr; |
9d3e06a4 GB |
167 | |
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; | |
174 | ||
175 |