]>
Commit | Line | Data |
---|---|---|
50a14534 EB |
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 | */ | |
4c77d5e0 GB |
21 | #ifdef HAVE_CONFIG_H |
22 | #include "config.h" | |
23 | #endif | |
50a14534 | 24 | |
8c060406 | 25 | #include <stdio.h> |
35462baf QT |
26 | #include <stdlib.h> |
27 | #include <string.h> | |
4c77d5e0 | 28 | #include <glib/gi18n.h> |
50a14534 EB |
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" | |
85611cd9 | 35 | #include "download.h" |
35462baf | 36 | #include "curl_download.h" |
50a14534 EB |
37 | #include "globals.h" |
38 | #include "google.h" | |
f40fb47d | 39 | #include "google-map-type.h" |
cdcaf41c QT |
40 | #include "vikmapslayer.h" |
41 | ||
dad2c114 | 42 | |
94493114 QT |
43 | static int google_kh_download ( MapCoord *src, const gchar *dest_fn ); |
44 | static void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ); | |
94493114 | 45 | |
1ac37c09 | 46 | static DownloadOptions google_options = { "http://maps.google.com/", 0, a_check_map_file }; |
80214df2 | 47 | |
cdcaf41c | 48 | void google_init () { |
93015549 | 49 | VikMapType *google_1 = VIK_MAP_TYPE(google_map_type_new_with_id(7, TYPE_GOOGLE_MAPS)); |
50 | VikMapType *google_2 = VIK_MAP_TYPE(google_map_type_new_with_id(10, TYPE_GOOGLE_TRANS)); | |
51 | VikMapType *google_3 = VIK_MAP_TYPE(google_map_type_new_with_id(11, TYPE_GOOGLE_SAT)); | |
52 | VikMapType *google_4 = VIK_MAP_TYPE(google_map_type_new_with_id(16, TYPE_GOOGLE_TERRAIN)); | |
f40fb47d | 53 | |
54 | maps_layer_register_type(_("Google Maps"), 7, google_1); | |
55 | maps_layer_register_type(_("Transparent Google Maps"), 10, google_2); | |
56 | maps_layer_register_type(_("Google Satellite Images"), 11, google_3); | |
57 | maps_layer_register_type(_("Google Terrain Maps"), 16, google_4); | |
cdcaf41c | 58 | } |
50a14534 EB |
59 | |
60 | /* 1 << (x) is like a 2**(x) */ | |
61 | #define GZ(x) ((1<<x)) | |
62 | ||
63 | 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), | |
64 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
65 | ||
66 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
67 | ||
68 | #define ERROR_MARGIN 0.01 | |
93015549 | 69 | static guint8 google_zoom ( gdouble mpp ) { |
50a14534 EB |
70 | gint i; |
71 | for ( i = 0; i < num_scales; i++ ) { | |
72 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) | |
73 | return i; | |
74 | } | |
75 | return 255; | |
76 | } | |
77 | ||
35462baf QT |
78 | static gchar *parse_version_number(gchar *text) |
79 | { | |
80 | int i; | |
81 | gchar *vers; | |
82 | gchar *s = text; | |
83 | ||
9128a946 | 84 | for (i = 0; (s[i] != '\\') && (i < 8); i++) |
35462baf | 85 | ; |
9128a946 | 86 | if (s[i] != '\\') { |
35462baf QT |
87 | return NULL; |
88 | } | |
89 | ||
90 | return vers = g_strndup(s, i); | |
91 | } | |
92 | ||
93 | static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type) | |
94 | { | |
95 | static gboolean first = TRUE; | |
3cf8131d | 96 | static char *vers[] = { "w2.80", "w2t.80", "30", "w2p.81"}; |
35462baf QT |
97 | FILE *tmp_file; |
98 | int tmp_fd; | |
99 | gchar *tmpname; | |
100 | gchar *uri; | |
101 | VikCoord coord; | |
27c30bbe | 102 | gchar coord_north_south[G_ASCII_DTOSTR_BUF_SIZE], coord_east_west[G_ASCII_DTOSTR_BUF_SIZE]; |
35462baf QT |
103 | gchar *text, *pat, *beg; |
104 | GMappedFile *mf; | |
105 | gsize len; | |
8a920b62 | 106 | gchar *gvers, *tvers, *kvers, *terrvers, *tmpvers; |
1ac37c09 | 107 | static DownloadOptions dl_options = { "http://maps.google.com/", 0, a_check_map_file }; |
3cf8131d QT |
108 | /* static const char *gvers_pat = "http://mt0.google.com/mt?v\\x3d"; */ |
109 | static const char *gvers_pat = "http://mt0.google.com/mt"; | |
09dd3c51 | 110 | static const char *kvers_pat = "http://khm0.google.com/kh?v\\x3d"; |
35462baf QT |
111 | |
112 | g_assert(google_type < TYPE_GOOGLE_NUM); | |
113 | ||
114 | if (!first) | |
115 | return (vers[google_type]); | |
116 | ||
117 | ||
118 | first = FALSE; | |
8a920b62 | 119 | gvers = tvers = kvers = terrvers = NULL; |
35462baf | 120 | if ((tmp_fd = g_file_open_tmp ("vikgvers.XXXXXX", &tmpname, NULL)) == -1) { |
4258f4e2 | 121 | g_critical(_("couldn't open temp file %s"), tmpname); |
35462baf QT |
122 | exit(1); |
123 | } | |
124 | ||
125 | google_mapcoord_to_center_coord(mapcoord, &coord); | |
27c30bbe MA |
126 | uri = g_strdup_printf("http://maps.google.com/maps?f=q&hl=en&q=%s,%s", |
127 | g_ascii_dtostr (coord_north_south, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.north_south), | |
128 | g_ascii_dtostr (coord_east_west, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.east_west)); | |
35462baf QT |
129 | tmp_file = fdopen(tmp_fd, "r+"); |
130 | ||
131 | if (curl_download_uri(uri, tmp_file, &dl_options)) { /* error */ | |
4258f4e2 | 132 | g_warning(_("Failed downloading %s"), tmpname); |
35462baf QT |
133 | } else { |
134 | if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) { | |
4258f4e2 | 135 | g_critical(_("couldn't map temp file")); |
35462baf QT |
136 | exit(1); |
137 | } | |
138 | len = g_mapped_file_get_length(mf); | |
139 | text = g_mapped_file_get_contents(mf); | |
140 | ||
141 | if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) { | |
4258f4e2 | 142 | g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)")); |
35462baf QT |
143 | goto failed; |
144 | } | |
145 | ||
146 | pat = beg; | |
8a920b62 | 147 | while (!gvers || !tvers ||!terrvers) { |
35462baf QT |
148 | if ((pat = g_strstr_len(pat, &text[len] - pat, gvers_pat)) != NULL) { |
149 | pat += strlen(gvers_pat); | |
3cf8131d QT |
150 | if ((pat[0] != '/' && pat[0] != '?') || |
151 | pat[1] != 'v' || pat[2] != '\\' || | |
152 | pat[3] != 'x' || pat[4] != '3' || pat[5] != 'd') | |
153 | continue; | |
154 | pat += 6; | |
35462baf QT |
155 | if ((tmpvers = parse_version_number(pat)) != NULL) { |
156 | if (strstr(tmpvers, "t.")) | |
157 | tvers = tmpvers; | |
8a920b62 EB |
158 | else if (strstr(tmpvers, "p.")) |
159 | terrvers = tmpvers; | |
35462baf QT |
160 | else |
161 | gvers = tmpvers; | |
162 | } | |
163 | } | |
164 | else | |
165 | break; | |
166 | } | |
167 | ||
168 | if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL) | |
169 | kvers = parse_version_number(pat + strlen(kvers_pat)); | |
170 | ||
171 | if (gvers && tvers && kvers) { | |
172 | vers[TYPE_GOOGLE_MAPS] = gvers; | |
173 | vers[TYPE_GOOGLE_TRANS] = tvers; | |
174 | vers[TYPE_GOOGLE_SAT] = kvers; | |
8a920b62 | 175 | vers[TYPE_GOOGLE_TERRAIN] = terrvers; |
35462baf QT |
176 | } |
177 | else | |
4c77d5e0 | 178 | g_warning(_("Failed getting google version numbers")); |
35462baf QT |
179 | |
180 | if (gvers) | |
181 | fprintf(stderr, "DEBUG gvers=%s\n", gvers); | |
182 | if (tvers) | |
183 | fprintf(stderr, "DEBUG tvers=%s\n", tvers); | |
8a920b62 EB |
184 | if (terrvers) |
185 | fprintf(stderr, "DEBUG terrvers=%s\n", terrvers); | |
35462baf QT |
186 | if (kvers) |
187 | fprintf(stderr, "DEBUG kvers=%s\n", kvers); | |
188 | ||
189 | failed: | |
190 | g_mapped_file_free(mf); | |
191 | } | |
192 | ||
193 | fclose(tmp_file); | |
8c060406 | 194 | tmp_file = NULL; |
35462baf | 195 | g_free(tmpname); |
97634600 | 196 | g_free (uri); |
35462baf QT |
197 | return (vers[google_type]); |
198 | } | |
199 | ||
50a14534 EB |
200 | gboolean google_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) |
201 | { | |
202 | g_assert ( src->mode == VIK_COORD_LATLON ); | |
203 | ||
204 | if ( xzoom != yzoom ) | |
205 | return FALSE; | |
206 | ||
207 | dest->scale = google_zoom ( xzoom ); | |
208 | if ( dest->scale == 255 ) | |
209 | return FALSE; | |
210 | ||
211 | dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom; | |
212 | dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom; | |
213 | dest->z = 0; | |
214 | ||
215 | return TRUE; | |
216 | } | |
217 | ||
93015549 | 218 | static void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ) |
50a14534 EB |
219 | { |
220 | gdouble socalled_mpp = GZ(src->scale); | |
221 | dest->mode = VIK_COORD_LATLON; | |
222 | dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180; | |
223 | dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360)); | |
224 | } | |
225 | ||
50a14534 EB |
226 | static char *kh_encode(guint32 x, guint32 y, guint8 scale) |
227 | { | |
228 | gchar *buf = g_malloc ( (20-scale)*sizeof(gchar) ); | |
229 | guint32 ya = 1 << (17 - scale); | |
230 | gint8 i, j; | |
231 | ||
232 | if (y < 0 || (ya-1 < y)) { | |
233 | strcpy(buf,"tqq"); /* BAD */ | |
234 | return buf; | |
235 | } | |
236 | if (x < 0 || ya-1 < x) { | |
237 | x %= ya; | |
238 | if (x < 0) | |
239 | x += ya; | |
240 | } | |
241 | ||
242 | buf[0] = 't'; | |
243 | for (j = 1, i = 16; i >= scale; i--, j++) { | |
244 | ya /= 2; | |
245 | if (y < ya) { | |
246 | if (x < ya) | |
247 | buf[j]='q'; | |
248 | else { | |
249 | buf[j]='r'; | |
250 | x -= ya; | |
251 | } | |
252 | } else { | |
253 | if (x < ya) { | |
254 | buf[j] = 't'; | |
255 | y -= ya; | |
256 | } else { | |
257 | buf[j] = 's'; | |
258 | x -= ya; | |
259 | y -= ya; | |
260 | } | |
261 | } | |
262 | } | |
263 | buf[j] = '\0'; | |
264 | return buf; | |
265 | } | |
266 | ||
94493114 | 267 | static int google_kh_download ( MapCoord *src, const gchar *dest_fn ) |
50a14534 | 268 | { |
94493114 | 269 | int res; |
50a14534 | 270 | gchar *khenc = kh_encode( src->x, src->y, src->scale ); |
35462baf QT |
271 | const gchar *vers_str = google_version_number(src, TYPE_GOOGLE_SAT); |
272 | gchar *uri = g_strdup_printf ( "/kh?n=404&v=%s&t=%s", vers_str, khenc ); | |
50a14534 | 273 | g_free ( khenc ); |
804e8de9 | 274 | res = a_http_download_get_url ( "khm.google.com", uri, dest_fn, &google_options ); |
50a14534 | 275 | g_free ( uri ); |
94493114 | 276 | return(res); |
50a14534 | 277 | } |