]> git.street.me.uk Git - andy/viking.git/blob - src/google.c
Cosmetic change
[andy/viking.git] / src / google.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <math.h>
31 #include <string.h>
32 #include "coords.h"
33 #include "vikcoord.h"
34 #include "mapcoord.h"
35 #include "download.h"
36 #include "curl_download.h"
37 #include "globals.h"
38 #include "google.h"
39 #include "vikmapslayer.h"
40
41
42 static int google_download ( MapCoord *src, const gchar *dest_fn );
43 static int google_trans_download ( MapCoord *src, const gchar *dest_fn );
44 static int google_terrain_download ( MapCoord *src, const gchar *dest_fn );
45 static int google_kh_download ( MapCoord *src, const gchar *dest_fn );
46 static void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
47 static gboolean google_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
48
49 static DownloadOptions google_options = { "http://maps.google.com/", 0 };
50
51 void google_init () {
52   VikMapsLayer_MapType google_1 = { 7, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_download };
53   VikMapsLayer_MapType google_2 = { 10, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_trans_download };
54   VikMapsLayer_MapType google_3 = { 11, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_kh_download };
55   VikMapsLayer_MapType google_4 = { 16, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_terrain_download };
56
57   maps_layer_register_type(_("Google Maps"), 7, &google_1);
58   maps_layer_register_type(_("Transparent Google Maps"), 10, &google_2);
59   maps_layer_register_type(_("Google Satellite Images"), 11, &google_3);
60   maps_layer_register_type(_("Google Terrain Maps"), 16, &google_4);
61 }
62
63 /* 1 << (x) is like a 2**(x) */
64 #define GZ(x) ((1<<x))
65
66 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),
67                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
68
69 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
70
71 #define ERROR_MARGIN 0.01
72 guint8 google_zoom ( gdouble mpp ) {
73   gint i;
74   for ( i = 0; i < num_scales; i++ ) {
75     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
76       return i;
77   }
78   return 255;
79 }
80
81 typedef enum {
82         TYPE_GOOGLE_MAPS = 0,
83         TYPE_GOOGLE_TRANS,
84         TYPE_GOOGLE_SAT,
85         TYPE_GOOGLE_TERRAIN,
86
87         TYPE_GOOGLE_NUM
88 } GoogleType;
89
90 static gchar *parse_version_number(gchar *text)
91 {
92   int i;
93   gchar *vers;
94   gchar *s = text;
95
96   for (i = 0; (s[i] != '\\') && (i < 8); i++)
97     ;
98   if (s[i] != '\\') {
99     return NULL;
100   }
101
102   return vers = g_strndup(s, i);
103 }
104
105 static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type)
106 {
107   static gboolean first = TRUE;
108   static char *vers[] = { "w2.60", "w2t.60", "20", "w2p.60" };
109   FILE *tmp_file;
110   int tmp_fd;
111   gchar *tmpname;
112   gchar *uri;
113   VikCoord coord;
114   gchar *text, *pat, *beg;
115   GMappedFile *mf;
116   gsize len;
117   gchar *gvers, *tvers, *kvers, *terrvers, *tmpvers;
118   static DownloadOptions dl_options = { "http://maps.google.com/", 0 };
119   static const char *gvers_pat = "http://mt0.google.com/mt?n\\x3d404\\x26v\\x3d";
120   static const char *kvers_pat = "http://kh0.google.com/kh?n\\x3d404\\x26v\\x3d";
121
122   g_assert(google_type < TYPE_GOOGLE_NUM);
123
124   if (!first)
125     return (vers[google_type]);
126
127
128   first = FALSE;
129   gvers = tvers = kvers = terrvers = NULL;
130   if ((tmp_fd = g_file_open_tmp ("vikgvers.XXXXXX", &tmpname, NULL)) == -1) {
131     g_critical(_("couldn't open temp file %s"), tmpname);
132     exit(1);
133   } 
134
135   google_mapcoord_to_center_coord(mapcoord, &coord);
136   uri = g_strdup_printf("http://maps.google.com/maps?f=q&hl=en&q=%f,%f", coord.north_south, coord.east_west);
137   tmp_file = fdopen(tmp_fd, "r+");
138
139   if (curl_download_uri(uri, tmp_file, &dl_options)) {  /* error */
140     g_warning(_("Failed downloading %s"), tmpname);
141   } else {
142     if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) {
143       g_critical(_("couldn't map temp file"));
144       exit(1);
145     }
146     len = g_mapped_file_get_length(mf);
147     text = g_mapped_file_get_contents(mf);
148
149     if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) {
150       g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)"));
151       goto failed;
152     }
153
154     pat = beg;
155     while (!gvers || !tvers ||!terrvers) {
156       if ((pat = g_strstr_len(pat, &text[len] - pat, gvers_pat)) != NULL) {
157         pat += strlen(gvers_pat);
158         if ((tmpvers = parse_version_number(pat)) != NULL) {
159           if (strstr(tmpvers, "t."))
160             tvers = tmpvers;
161           else if (strstr(tmpvers, "p."))
162             terrvers = tmpvers;
163           else
164             gvers = tmpvers;
165         }
166       }
167       else
168         break;
169     }
170
171     if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL)
172         kvers = parse_version_number(pat + strlen(kvers_pat));
173
174     if (gvers && tvers && kvers) {
175       vers[TYPE_GOOGLE_MAPS] = gvers;
176       vers[TYPE_GOOGLE_TRANS] = tvers;
177       vers[TYPE_GOOGLE_SAT] = kvers;
178       vers[TYPE_GOOGLE_TERRAIN] = terrvers;
179     }
180     else
181       g_warning(_("Failed getting google version numbers"));
182
183     if (gvers)
184       fprintf(stderr, "DEBUG gvers=%s\n", gvers);
185     if (tvers)
186       fprintf(stderr, "DEBUG tvers=%s\n", tvers);
187     if (terrvers)
188       fprintf(stderr, "DEBUG terrvers=%s\n", terrvers);
189     if (kvers)
190       fprintf(stderr, "DEBUG kvers=%s\n", kvers);
191
192 failed:
193     g_mapped_file_free(mf);
194   }
195
196   fclose(tmp_file);
197   tmp_file = NULL;
198   g_free(tmpname);
199   g_free (uri);
200   return (vers[google_type]);
201 }
202
203 gboolean google_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
204 {
205   g_assert ( src->mode == VIK_COORD_LATLON );
206
207   if ( xzoom != yzoom )
208     return FALSE;
209
210   dest->scale = google_zoom ( xzoom );
211   if ( dest->scale == 255 )
212     return FALSE;
213
214   dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
215   dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
216   dest->z = 0;
217
218   return TRUE;
219 }
220
221 void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
222 {
223   gdouble socalled_mpp = GZ(src->scale);
224   dest->mode = VIK_COORD_LATLON;
225   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
226   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
227 }
228
229 static int real_google_download ( MapCoord *src, const gchar *dest_fn, const char *verstr )
230 {
231    int res;
232    gchar *uri = g_strdup_printf ( "/mt?n=404&v=%s&x=%d&y=%d&zoom=%d", verstr, src->x, src->y, src->scale );
233    res = a_http_download_get_url ( "mt.google.com", uri, dest_fn, &google_options );
234    g_free ( uri );
235    return res;
236 }
237
238 static int google_download ( MapCoord *src, const gchar *dest_fn )
239 {
240    const gchar *vers_str = google_version_number(src, TYPE_GOOGLE_MAPS);
241    return(real_google_download ( src, dest_fn, vers_str ));
242 }
243
244 static int google_trans_download ( MapCoord *src, const gchar *dest_fn )
245 {
246    const gchar *vers_str = google_version_number(src, TYPE_GOOGLE_TRANS);
247    return(real_google_download ( src, dest_fn, vers_str ));
248 }
249
250 static int google_terrain_download ( MapCoord *src, const gchar *dest_fn )
251 {
252    const gchar *vers_str = google_version_number(src, TYPE_GOOGLE_TERRAIN);
253    return(real_google_download ( src, dest_fn, vers_str ));
254 }
255
256 static char *kh_encode(guint32 x, guint32 y, guint8 scale)
257 {
258   gchar *buf = g_malloc ( (20-scale)*sizeof(gchar) );
259   guint32 ya = 1 << (17 - scale);
260   gint8 i, j;
261
262   if (y < 0 || (ya-1 < y)) {
263     strcpy(buf,"tqq"); /* BAD */
264     return buf;
265   }
266   if (x < 0 || ya-1 < x) {
267     x %= ya;
268     if (x < 0)
269       x += ya;
270   }
271
272   buf[0] = 't';
273   for (j = 1, i = 16; i >= scale; i--, j++) {
274     ya /= 2;
275     if (y < ya) {
276       if (x < ya)
277         buf[j]='q';
278       else {
279         buf[j]='r';
280         x -= ya;
281       }
282     } else {
283       if (x < ya) {
284         buf[j] = 't';
285         y -= ya;
286       } else {
287         buf[j] = 's';
288         x -= ya;
289         y -= ya;
290       }
291     }
292   }
293   buf[j] = '\0';
294   return buf;
295 }
296
297 static int google_kh_download ( MapCoord *src, const gchar *dest_fn )
298 {
299    int res;
300    gchar *khenc = kh_encode( src->x, src->y, src->scale );
301    const gchar *vers_str = google_version_number(src, TYPE_GOOGLE_SAT);
302    gchar *uri = g_strdup_printf ( "/kh?n=404&v=%s&t=%s", vers_str, khenc );
303    g_free ( khenc );
304    res = a_http_download_get_url ( "kh.google.com", uri, dest_fn, &google_options );
305    g_free ( uri );
306    return(res);
307 }