]> git.street.me.uk Git - andy/viking.git/blame - src/degrees_converters.c
Remove dependencies to gob2
[andy/viking.git] / src / degrees_converters.c
CommitLineData
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 */
21
9d3e06a4
GB
22#include <math.h>
23#include <glib.h>
2162b266
GB
24#include <string.h>
25
a58aaed4
GB
26#define DEGREE_SYMBOL "\302\260"
27
16dbae57
GB
28/**
29 * @param pos_c char for positive value
30 * @param neg_c char for negative value
31 */
32static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c)
33{
34 gchar sign_c = ' ';
35 gdouble val_d;
36 gchar *result = NULL;
37
38 if ( dec > 0 )
39 sign_c = pos_c;
40 else if ( dec < 0 )
41 sign_c = neg_c;
42 else /* Nul value */
43 sign_c = ' ';
44
45 /* Degree */
46 val_d = fabs(dec);
47
48 /* Format */
a58aaed4 49 result = g_strdup_printf ( "%c%f" DEGREE_SYMBOL, sign_c, val_d );
16dbae57
GB
50 return result;
51}
52
53gchar *convert_lat_dec_to_ddd(gdouble lat)
54{
55 return convert_dec_to_ddd(lat, 'N', 'S');
56}
57
58gchar *convert_lon_dec_to_ddd(gdouble lon)
59{
60 return convert_dec_to_ddd(lon, 'E', 'W');
61}
62
2162b266
GB
63/**
64 * @param pos_c char for positive value
65 * @param neg_c char for negative value
66 */
67static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
68{
69 gdouble tmp;
70 gchar sign_c = ' ';
71 gint val_d;
72 gdouble val_m;
73 gchar *result = NULL;
74
75 if ( dec > 0 )
76 sign_c = pos_c;
77 else if ( dec < 0 )
78 sign_c = neg_c;
79 else /* Nul value */
80 sign_c = ' ';
81
82 /* Degree */
83 tmp = fabs(dec);
84 val_d = (gint)tmp;
85
86 /* Minutes */
87 val_m = (tmp - val_d) * 60;
88
89 /* Format */
a58aaed4 90 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%f'",
2162b266
GB
91 sign_c, val_d, val_m );
92 return result;
93}
94
95gchar *convert_lat_dec_to_dmm(gdouble lat)
96{
97 return convert_dec_to_dmm(lat, 'N', 'S');
98}
99
100gchar *convert_lon_dec_to_dmm(gdouble lon)
101{
102 return convert_dec_to_dmm(lon, 'E', 'W');
103}
9d3e06a4 104
f88152fe
GB
105/**
106 * @param pos_c char for positive value
107 * @param neg_c char for negative value
108 */
109static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
9d3e06a4
GB
110{
111 gdouble tmp;
f88152fe
GB
112 gchar sign_c = ' ';
113 gint val_d, val_m;
114 gdouble val_s;
9d3e06a4
GB
115 gchar *result = NULL;
116
f88152fe
GB
117 if ( dec > 0 )
118 sign_c = pos_c;
119 else if ( dec < 0 )
120 sign_c = neg_c;
121 else /* Nul value */
122 sign_c = ' ';
9d3e06a4
GB
123
124 /* Degree */
f88152fe
GB
125 tmp = fabs(dec);
126 val_d = (gint)tmp;
9d3e06a4
GB
127
128 /* Minutes */
f88152fe
GB
129 tmp = (tmp - val_d) * 60;
130 val_m = (gint)tmp;
9d3e06a4
GB
131
132 /* Minutes */
f88152fe 133 val_s = (tmp - val_m) * 60;
9d3e06a4
GB
134
135 /* Format */
a58aaed4 136 result = g_strdup_printf ( "%c%d" DEGREE_SYMBOL "%d'%f\"",
f88152fe 137 sign_c, val_d, val_m, val_s );
9d3e06a4
GB
138 return result;
139}
140
f88152fe 141gchar *convert_lat_dec_to_dms(gdouble lat)
9d3e06a4 142{
f88152fe
GB
143 return convert_dec_to_dms(lat, 'N', 'S');
144}
9d3e06a4 145
f88152fe
GB
146gchar *convert_lon_dec_to_dms(gdouble lon)
147{
148 return convert_dec_to_dms(lon, 'E', 'W');
9d3e06a4
GB
149}
150
151gdouble convert_dms_to_dec(const gchar *dms)
152{
153 gdouble d = 0.0; /* Degree */
154 gdouble m = 0.0; /* Minutes */
155 gdouble s = 0.0; /* Seconds */
156 gint neg = FALSE;
157 gdouble result;
158
159 if (dms != NULL) {
160 int nbFloat = 0;
2162b266 161 const gchar *ptr, *endptr;
9d3e06a4
GB
162
163 // Compute the sign
164 // It is negative if:
165 // - the '-' sign occurs
166 // - it is a west longitude or south latitude
167 if (strpbrk (dms, "-wWsS") != NULL)
168 neg = TRUE;
169
170