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
31 #define DEGREE_SYMBOL "\302\260"
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
)
54 result
= g_strdup_printf ( "%c%f" DEGREE_SYMBOL
, sign_c
, val_d
);
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');
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
)
92 val_m
= (tmp
- val_d
) * 60;
95 result
= g_strdup_printf ( "%c%d" DEGREE_SYMBOL
"%f'",
96 sign_c
, val_d
, val_m
);
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
)
120 gchar
*result
= NULL
;
134 tmp
= (tmp
- val_d
) * 60;
138 val_s
= (tmp
- val_m
) * 60;
141 result
= g_strdup_printf ( "%c%d" DEGREE_SYMBOL
"%d'%f\"",
142 sign_c
, val_d
, val_m
, val_s
);
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 */
166 const gchar
*ptr
, *endptr
;
169 // It is negative if:
170 // - the '-' sign occurs
171 // - it is a west longitude or south latitude
172 if (strpbrk (dms
, "-wWsS") != NULL
)
175 // Peek the différent components
179 ptr
= strpbrk (endptr
, "0123456789,.");
181 value
= g_strtod(ptr
, &endptr
);
195 } while (ptr
!= NULL
&& endptr
!= NULL
);
198 // Compute the result
199 result
= d
+ m
/60 + s
/3600;
201 if (neg
) result
= - result
;