]> git.street.me.uk Git - andy/viking.git/blob - src/google-map-type.gob
Start using GoogleMapType GObject
[andy/viking.git] / src / google-map-type.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 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <glib/gi18n.h>
17 #include <gtk/gtk.h>
18 #include <math.h>
19 #include <string.h>
20 #include "coords.h"
21 #include "vikcoord.h"
22 #include "mapcoord.h"
23 #include "download.h"
24 #include "curl_download.h"
25 #include "globals.h"
26 #include "google.h"
27 #include "vikmapslayer.h"
28 %}
29
30 %h{
31
32 typedef enum {
33         TYPE_GOOGLE_MAPS = 0,
34         TYPE_GOOGLE_TRANS,
35         TYPE_GOOGLE_SAT,
36         TYPE_GOOGLE_TERRAIN,
37
38         TYPE_GOOGLE_NUM
39 } GoogleType;
40
41 %}
42
43 %{
44
45 static DownloadOptions google_options = { "http://maps.google.com/", 0, a_check_map_file };
46
47 /* 1 << (x) is like a 2**(x) */
48 #define GZ(x) ((1<<x))
49
50 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),
51                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
52
53 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
54
55 #define ERROR_MARGIN 0.01
56 guint8 google_zoom ( gdouble mpp ) {
57   gint i;
58   for ( i = 0; i < num_scales; i++ ) {
59     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
60       return i;
61   }
62   return 255;
63 }
64
65
66 static gchar *parse_version_number(gchar *text)
67 {
68   int i;
69   gchar *vers;
70   gchar *s = text;
71
72   for (i = 0; (s[i] != '\\') && (i < 8); i++)
73     ;
74   if (s[i] != '\\') {
75     return NULL;
76   }
77
78   return vers = g_strndup(s, i);
79 }
80
81 static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type)
82 {
83   static gboolean first = TRUE;
84   static char *vers[] = { "w2.80", "w2t.80", "30", "w2p.81"};
85   FILE *tmp_file;
86   int tmp_fd;
87   gchar *tmpname;
88   gchar *uri;
89   VikCoord coord;
90   gchar coord_north_south[G_ASCII_DTOSTR_BUF_SIZE], coord_east_west[G_ASCII_DTOSTR_BUF_SIZE];
91   gchar *text, *pat, *beg;
92   GMappedFile *mf;
93   gsize len;
94   gchar *gvers, *tvers, *kvers, *terrvers, *tmpvers;
95   static DownloadOptions dl_options = { "http://maps.google.com/", 0, a_check_map_file };
96   /* static const char *gvers_pat = "http://mt0.google.com/mt?v\\x3d"; */
97   static const char *gvers_pat = "http://mt0.google.com/mt";
98   static const char *kvers_pat = "http://khm0.google.com/kh?v\\x3d";
99
100   g_assert(google_type < TYPE_GOOGLE_NUM);
101
102   if (!first)
103     return (vers[google_type]);
104
105
106   first = FALSE;
107   gvers = tvers = kvers = terrvers = NULL;
108   if ((tmp_fd = g_file_open_tmp ("vikgvers.XXXXXX", &tmpname, NULL)) == -1) {
109     g_critical(_("couldn't open temp file %s"), tmpname);
110     exit(1);
111   } 
112
113   google_mapcoord_to_center_coord(mapcoord, &coord);
114   uri = g_strdup_printf("http://maps.google.com/maps?f=q&hl=en&q=%s,%s",
115                         g_ascii_dtostr (coord_north_south, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.north_south),
116                         g_ascii_dtostr (coord_east_west, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.east_west));
117   tmp_file = fdopen(tmp_fd, "r+");
118
119   if (curl_download_uri(uri, tmp_file, &dl_options)) {  /* error */
120     g_warning(_("Failed downloading %s"), tmpname);
121   } else {
122     if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) {
123       g_critical(_("couldn't map temp file"));
124       exit(1);
125     }
126     len = g_mapped_file_get_length(mf);
127     text = g_mapped_file_get_contents(mf);
128
129     if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) {
130       g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)"));
131       goto failed;
132     }
133
134     pat = beg;
135     while (!gvers || !tvers ||!terrvers) {
136       if ((pat = g_strstr_len(pat, &text[len] - pat, gvers_pat)) != NULL) {
137         pat += strlen(gvers_pat);
138         if ((pat[0] != '/' && pat[0] != '?') ||
139              pat[1] != 'v' || pat[2] != '\\' ||
140              pat[3] != 'x' || pat[4] != '3' || pat[5] != 'd')
141           continue;
142         pat += 6;
143         if ((tmpvers = parse_version_number(pat)) != NULL) {
144           if (strstr(tmpvers, "t."))
145             tvers = tmpvers;
146           else if (strstr(tmpvers, "p."))
147             terrvers = tmpvers;
148           else
149             gvers = tmpvers;
150         }
151       }
152       else
153         break;
154     }
155
156     if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL)
157         kvers = parse_version_number(pat + strlen(kvers_pat));
158
159     if (gvers && tvers && kvers) {
160       vers[TYPE_GOOGLE_MAPS] = gvers;
161       vers[TYPE_GOOGLE_TRANS] = tvers;
162       vers[TYPE_GOOGLE_SAT] = kvers;
163       vers[TYPE_GOOGLE_TERRAIN] = terrvers;
164     }
165     else
166       g_warning(_("Failed getting google version numbers"));
167
168     if (gvers)
169       fprintf(stderr, "DEBUG gvers=%s\n", gvers);
170     if (tvers)
171       fprintf(stderr, "DEBUG tvers=%s\n", tvers);
172     if (terrvers)
173       fprintf(stderr, "DEBUG terrvers=%s\n", terrvers);
174     if (kvers)
175       fprintf(stderr, "DEBUG kvers=%s\n", kvers);
176
177 failed:
178     g_mapped_file_free(mf);
179   }
180
181   fclose(tmp_file);
182   tmp_file = NULL;
183   g_free(tmpname);
184   g_free (uri);
185   return (vers[google_type]);
186 }
187
188 static int real_google_download ( MapCoord *src, const gchar *dest_fn, const char *verstr )
189 {
190    int res;
191    gchar *uri = g_strdup_printf ( "/mt?n=404&v=%s&x=%d&y=%d&zoom=%d", verstr, src->x, src->y, src->scale );
192    res = a_http_download_get_url ( "mt.google.com", uri, dest_fn, &google_options );
193    g_free ( uri );
194    return res;
195 }
196
197 %}
198
199 class Google:Map:Type from Vik:Map:Type {
200   private GoogleType type;
201
202   init (self) {
203           /* initialize the object here */
204           self->tilesize_x = 256;
205           self->tilesize_y = 256;
206           self->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
207   }
208
209   public GObject *
210   new_with_id (guint8 id) {
211           GObject *ret = GET_NEW;
212           VIK_MAP_TYPE(ret)->uniq_id = id;
213           return G_OBJECT (ret);
214   }
215
216   public GObject *
217   new (void) {
218           GObject *ret = GET_NEW;
219           return G_OBJECT (ret);
220   }
221
222   override (Vik:Map:Type) gboolean
223   coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
224   {
225   g_assert ( src->mode == VIK_COORD_LATLON );
226
227   if ( xzoom != yzoom )
228     return FALSE;
229
230   dest->scale = google_zoom ( xzoom );
231   if ( dest->scale == 255 )
232     return FALSE;
233
234   dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
235   dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
236   dest->z = 0;
237
238   return TRUE;
239   }
240
241   override (Vik:Map:Type) void
242   mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
243   {
244   gdouble socalled_mpp = GZ(src->scale);
245   dest->mode = VIK_COORD_LATLON;
246   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
247   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
248   }
249
250   override (Vik:Map:Type) int
251    download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
252   {
253      const gchar *vers_str = google_version_number(src, GOOGLE_MAP_TYPE(self)->_priv->type);
254       return(real_google_download ( src, dest_fn, vers_str ));
255   }
256 }