]> git.street.me.uk Git - andy/viking.git/blobdiff - src/coords.c
[DOC] Mention map tilesize configuration.
[andy/viking.git] / src / coords.c
index 62a2d9f90d5885e5fd7de06d48704a43eae09c75..f6dae4c78b1f9a0611136aa6a99fb8737fa19872 100644 (file)
@@ -49,9 +49,28 @@ renaming functions and defining LatLon and UTM structs.
 #include <math.h>
 #endif
 
+#include "coords.h"
+#ifdef HAVE_VIKING
 #include "viking.h"
 #include "globals.h"
+#else
+#define DEG2RAD(x) ((x)*(M_PI/180))
+#define RAD2DEG(x) ((x)*(180/M_PI))
+#endif
 #include "degrees_converters.h"
+#include "misc/fpconv.h"
+
+/**
+ *
+ */
+void a_coords_dtostr_buffer ( double d, char buffer[COORDS_STR_BUFFER_SIZE] )
+{
+  int str_len = fpconv_dtoa(d, buffer);
+  if ( str_len < COORDS_STR_BUFFER_SIZE )
+    buffer[str_len] = '\0';
+  else
+    buffer[COORDS_STR_BUFFER_SIZE-1] = '\0';
+}
 
 /**
  * Convert a double to a string WITHOUT LOCALE.
@@ -64,7 +83,10 @@ renaming functions and defining LatLon and UTM structs.
 char *a_coords_dtostr ( double d )
 {
   gchar *buffer = g_malloc(G_ASCII_DTOSTR_BUF_SIZE*sizeof(gchar));
-  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) d);
+  // Note that this doesn't necessary produce the shortest string
+  //g_ascii_formatd (buffer, G_ASCII_DTOSTR_BUF_SIZE, "%.9lf", (gdouble)d);
+  // Thus use third party code:
+  a_coords_dtostr_buffer ( d, buffer );
   return buffer;
 }
 
@@ -209,7 +231,6 @@ void a_coords_utm_to_latlon( const struct UTM *utm, struct LatLon *latlon )
     double N1, T1, C1, R1, D, M;
     double long_origin;
     double mu, phi1_rad;
-    int northernHemisphere;    /* 1 for northern hemisphere, 0 for southern */
     double latitude, longitude;
 
     northing = utm->northing;
@@ -220,13 +241,11 @@ void a_coords_utm_to_latlon( const struct UTM *utm, struct LatLon *latlon )
     /* Now convert. */
     x = easting - 500000.0;    /* remove 500000 meter offset */
     y = northing;
-    if ( ( *letter - 'N' ) >= 0 )
-       northernHemisphere = 1; /* northern hemisphere */
-    else
-       {
-       northernHemisphere = 0; /* southern hemisphere */
-       y -= 10000000.0;        /* remove 1e7 meter offset */
-       }
+    if ( ( *letter - 'N' ) < 0 ) {
+      /* southern hemisphere */
+      y -= 10000000.0; /* remove 1e7 meter offset */
+    }
+
     long_origin = ( zone - 1 ) * 6 - 180 + 3;  /* +3 puts origin in middle of zone */
     eccPrimeSquared = EccentricitySquared / ( 1.0 - EccentricitySquared );
     e1 = ( 1.0 - sqrt( 1.0 - EccentricitySquared ) ) / ( 1.0 + sqrt( 1.0 - EccentricitySquared ) );
@@ -255,7 +274,7 @@ void a_coords_latlon_to_string ( const struct LatLon *latlon,
                                 gchar **lon )
 {
   g_return_if_fail ( latlon != NULL );
-
+#ifdef HAVE_VIKING
   vik_degree_format_t format = a_vik_get_degree_format ();
 
   switch (format) {
@@ -271,7 +290,15 @@ void a_coords_latlon_to_string ( const struct LatLon *latlon,
     *lat = convert_lat_dec_to_dms ( latlon->lat );
     *lon = convert_lon_dec_to_dms ( latlon->lon );
     break;
+  case VIK_DEGREE_FORMAT_RAW:
+    *lat = g_strdup_printf ( "%.6f", latlon->lat );
+    *lon = g_strdup_printf ( "%.6f", latlon->lon );
+    break;
   default:
     g_critical("Houston, we've had a problem. format=%d", format);
   }
+#else
+  *lat = convert_lat_dec_to_ddd ( latlon->lat );
+  *lon = convert_lon_dec_to_ddd ( latlon->lon );
+#endif
 }