]> git.street.me.uk Git - andy/viking.git/blob - src/degrees_converters.c
Bump up default map cache size to 128MB and allow setting to 1GB.
[andy/viking.git] / src / degrees_converters.c
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  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #ifdef HAVE_MATH_H
26 #include <math.h>
27 #endif
28 #include <glib.h>
29 #include <string.h>
30
31 #define DEGREE_SYMBOL "\302\260"
32
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 */
54   result = g_strdup_printf ( "%c%f" DEGREE_SYMBOL, sign_c, val_d );
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
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 */
95   result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'",
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 }
109
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)
115 {
116   gdouble tmp;
117   gchar sign_c = ' ';
118   gint val_d, val_m;
119   gdouble val_s;
120   gchar *result = NULL;
121
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 = ' ';
128
129   /* Degree */
130   tmp = fabs(dec);
131   val_d = (gint)tmp;
132
133   /* Minutes */
134   tmp = (tmp - val_d) * 60;
135   val_m = (gint)tmp;
136
137   /* Minutes */
138   val_s = (tmp - val_m) * 60;
139
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;
144 }
145
146 gchar *convert_lat_dec_to_dms(gdouble lat)
147 {
148   return convert_dec_to_dms(lat, 'N', 'S');
149 }
150
151 gchar *convert_lon_dec_to_dms(gdouble lon)
152 {
153   return convert_dec_to_dms(lon, 'E', 'W');
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;
166                 const gchar *ptr, *endptr;
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                 // 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((const gchar *)ptr, (gchar **)&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;
193                 }
194                         }
195                 } while (ptr != NULL && endptr != NULL);
196         }
197         
198         // Compute the result
199         result = d + m/60 + s/3600;
200         
201         if (neg) result = - result;
202         
203         return result;
204 }