]> git.street.me.uk Git - andy/viking.git/commitdiff
Make mpp_to_zoom() a generic function
authorRob Norris <rw_norris@hotmail.com>
Sat, 2 Mar 2013 21:26:19 +0000 (21:26 +0000)
committerRob Norris <rw_norris@hotmail.com>
Thu, 2 May 2013 23:46:16 +0000 (00:46 +0100)
It doesn't rely on any Webtool center data.

src/util.c
src/util.h

index 5d329d7807d906b4f44b501ea8843b9d8fdd4b60..6fbf9f104db7b66f424b523dbb1fd37f2f6eb44e 100644 (file)
@@ -157,3 +157,28 @@ gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar
 
   return TRUE;
 }
+
+/* 1 << (x) is like a 2**(x) */
+#define GZ(x) (1<<(x))
+
+static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
+                                      GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
+
+static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
+
+#define ERROR_MARGIN 0.01
+guint8 mpp_to_zoom ( gdouble mpp )
+{
+  gint i;
+  for ( i = 0; i < num_scales; i++ ) {
+    if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
+      g_debug ( "mpp_to_zoom: %f -> %d", mpp, i );
+      return i;
+    }
+  }
+  // Handle mpp smaller than 1
+  // return a useful value such that '17 - this number' gives a natural number.
+  // Ideally should return '-1' or '0.5' but that's tricky with an unsigned int type!
+  // (i.e. should rework to support zoom levels of 18 or 19)
+  return 0;
+}
index 8c10de45fe042aff609f48102a7ff8c58d126cd7..7b719dd4448bcd5d8de056e81fa838d4351b5250 100644 (file)
@@ -38,7 +38,8 @@ GList * str_array_to_glist(gchar* data[]);
 
 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val );
 
+guint8 mpp_to_zoom ( gdouble mpp );
+
 G_END_DECLS
 
 #endif
-