]> git.street.me.uk Git - andy/viking.git/blobdiff - src/degrees_converters.c
Refactoring: retrieve the VikWindow from the VikViewport via its GtkWidget nature
[andy/viking.git] / src / degrees_converters.c
index f3823c84f0ba2154fdddba08cda59659db232296..2261d674e2bc00eb8b0f3de0421ad5ee4a396ca7 100644 (file)
+/*
+ * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
+ *
+ * Copyright (C) 2006-2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ */
+
 #include <math.h>
 #include <glib.h>
-
 #include <stdio.h>
+#include <string.h>
 
-gchar *convert_lat_dec_to_dms(gdouble lat)
+/**
+ * @param pos_c char for positive value
+ * @param neg_c char for negative value
+ */
+static gchar *convert_dec_to_ddd(gdouble dec, gchar pos_c, gchar neg_c)
 {
-  gdouble tmp;
-  gchar lat_c;
-  gint lat_d, lat_m;
-  gdouble lat_s;
+  gchar sign_c = ' ';
+  gdouble val_d;
   gchar *result = NULL;
 
-  /* North ? South ? */
-  if ( lat > 0 )
-    lat_c = 'N';
-  else
-    lat_c = 'S';
+  if ( dec > 0 )
+    sign_c = pos_c;
+  else if ( dec < 0 )
+    sign_c = neg_c;
+  else /* Nul value */
+    sign_c = ' ';
 
   /* Degree */
-  tmp = fabs(lat);
-  lat_d = (gint)tmp;
+  val_d = fabs(dec);
 
-  /* Minutes */
-  tmp = (tmp - lat_d) * 60;
-  lat_m = (gint)tmp;
+  /* Format */
+  /* TODO : replace "°" as UTF-8 */
+  result = g_strdup_printf ( "%c%f°", sign_c, val_d );
+  return result;
+}
+
+gchar *convert_lat_dec_to_ddd(gdouble lat)
+{
+  return convert_dec_to_ddd(lat, 'N', 'S');
+}
+
+gchar *convert_lon_dec_to_ddd(gdouble lon)
+{
+  return convert_dec_to_ddd(lon, 'E', 'W');
+}
+
+/**
+ * @param pos_c char for positive value
+ * @param neg_c char for negative value
+ */
+static gchar *convert_dec_to_dmm(gdouble dec, gchar pos_c, gchar neg_c)
+{
+  gdouble tmp;
+  gchar sign_c = ' ';
+  gint val_d;
+  gdouble val_m;
+  gchar *result = NULL;
+
+  if ( dec > 0 )
+    sign_c = pos_c;
+  else if ( dec < 0 )
+    sign_c = neg_c;
+  else /* Nul value */
+    sign_c = ' ';
+
+  /* Degree */
+  tmp = fabs(dec);
+  val_d = (gint)tmp;
 
   /* Minutes */
-  lat_s = (tmp - lat_m) * 60;
+  val_m = (tmp - val_d) * 60;
 
   /* Format */
-  /* todo : replace "deg" substring by "°" as UTF-8 */
-  result = g_strdup_printf ( "%c %d° %d' %f",
-                             lat_c, lat_d, lat_m, lat_s );
+  /* TODO : replace "°" as UTF-8 */
+  result = g_strdup_printf ( "%c%d°%f'",
+                             sign_c, val_d, val_m );
   return result;
 }
 
-gchar *convert_lon_dec_to_dms(gdouble lon)
+gchar *convert_lat_dec_to_dmm(gdouble lat)
+{
+  return convert_dec_to_dmm(lat, 'N', 'S');
+}
+
+gchar *convert_lon_dec_to_dmm(gdouble lon)
+{
+  return convert_dec_to_dmm(lon, 'E', 'W');
+}
+
+/**
+ * @param pos_c char for positive value
+ * @param neg_c char for negative value
+ */
+static gchar *convert_dec_to_dms(gdouble dec, gchar pos_c, gchar neg_c)
 {
   gdouble tmp;
-  gchar lon_c;
-  gint lon_d, lon_m;
-  gdouble lon_s;
+  gchar sign_c = ' ';
+  gint val_d, val_m;
+  gdouble val_s;
   gchar *result = NULL;
 
-  /* North ? South ? */
-  if ( lon > 0 )
-    lon_c = 'E';
-  else
-    lon_c = 'W';
+  if ( dec > 0 )
+    sign_c = pos_c;
+  else if ( dec < 0 )
+    sign_c = neg_c;
+  else /* Nul value */
+    sign_c = ' ';
 
   /* Degree */
-  tmp = fabs(lon);
-  lon_d = (gint)tmp;
+  tmp = fabs(dec);
+  val_d = (gint)tmp;
 
   /* Minutes */
-  tmp = (tmp - lon_d) * 60;
-  lon_m = (gint)tmp;
+  tmp = (tmp - val_d) * 60;
+  val_m = (gint)tmp;
 
   /* Minutes */
-  lon_s = (tmp - lon_m) * 60;
+  val_s = (tmp - val_m) * 60;
 
   /* Format */
-  /* todo : replace "deg" substring by "°" as UTF-8 */
-  result = g_strdup_printf ( "%c %d° %d' %f\"",
-                             lon_c, lon_d, lon_m, lon_s );
+  /* TODO : replace "°" as UTF-8 */
+  result = g_strdup_printf ( "%c%d°%d'%f\"",
+                             sign_c, val_d, val_m, val_s );
   return result;
 }
 
+gchar *convert_lat_dec_to_dms(gdouble lat)
+{
+  return convert_dec_to_dms(lat, 'N', 'S');
+}
+
+gchar *convert_lon_dec_to_dms(gdouble lon)
+{
+  return convert_dec_to_dms(lon, 'E', 'W');
+}
+
 gdouble convert_dms_to_dec(const gchar *dms)
 {
        gdouble d = 0.0; /* Degree */
@@ -77,7 +160,7 @@ gdouble convert_dms_to_dec(const gchar *dms)
        
        if (dms != NULL) {
                int nbFloat = 0;
-               gchar *ptr, *endptr;
+               const gchar *ptr, *endptr;
 
                // Compute the sign
                // It is negative if: