]> git.street.me.uk Git - andy/viking.git/blob - src/google-maps.gob
Initial GoogleMaps implementation based on gob2
[andy/viking.git] / src / google-maps.gob
1 %headertop{
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 #include "vikcoord.h"
7 #include "mapcoord.h"
8 #include "vik-map-type.h"
9 %}
10
11
12 %{
13
14 #ifdef HAVE_MATH_H
15 #include <math.h>
16 #endif
17
18 #include "googlemaps.h"
19
20 /* 1 << (x-1) is like a 2**(x-1) */
21 #define GZ(x) ((1<<(x-1))*GOOGLEMAPS_ZOOM_ONE_MPP)
22
23 static const gdouble scale_mpps[] = { GZ(1)/2, GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
24                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15) };
25
26 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
27
28 #define ERROR_MARGIN 0.01
29 static guint8 googlemaps_zoom ( gdouble mpp ) {
30   gint i;
31   for ( i = 0; i < num_scales; i++ ) {
32     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
33       return i;
34   }
35   return 255;
36 }
37 %}
38
39 class Google:Maps from Vik:Map:Type {
40   override (Vik:Map:Type) void
41   mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
42   {
43   dest->mode = VIK_COORD_LATLON;
44   dest->north_south = 39.5 - (1.0/(131072 >> src->scale) * (src->y+0.5) * 128);
45   dest->east_west = 1.0 / (131072 >> src->scale) * (src->x+0.5) / 0.77162458338772 * 128 - 98.35;
46   }
47
48   override (Vik:Map:Type) gboolean
49   coord_to_mapcoord ( self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
50   {
51   g_assert ( src->mode == VIK_COORD_LATLON );
52
53   if ( xzoom != yzoom )
54     return FALSE;
55
56   dest->scale = googlemaps_zoom ( xzoom );
57   if ( dest->scale == 255 )
58     return FALSE;
59
60   dest->x = (gint) floor ( floor ((src->east_west + 98.35) * (131072 >> dest->scale) * 0.77162458338772) / 128);
61   dest->y = (gint) floor ( floor ((39.5 - src->north_south) * (131072 >> dest->scale)) / 128);
62   dest->z = 0;
63   return TRUE;
64   }
65
66 }