8 #include "vik-map-type.h"
16 #include <glib/gi18n.h>
24 #include "curl_download.h"
27 #include "vikmapslayer.h"
45 /* 1 << (x) is like a 2**(x) */
46 #define GZ(x) ((1<<x))
48 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),
49 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
51 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
53 #define ERROR_MARGIN 0.01
54 guint8 google_zoom ( gdouble mpp ) {
56 for ( i = 0; i < num_scales; i++ ) {
57 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
64 static gchar *parse_version_number(gchar *text)
70 for (i = 0; (s[i] != '\\') && (i < 8); i++)
76 return vers = g_strndup(s, i);
79 static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type)
81 static gboolean first = TRUE;
82 static char *vers[] = { "w2.80", "w2t.80", "30", "w2p.81"};
88 gchar coord_north_south[G_ASCII_DTOSTR_BUF_SIZE], coord_east_west[G_ASCII_DTOSTR_BUF_SIZE];
89 gchar *text, *pat, *beg;
92 gchar *gvers, *tvers, *kvers, *terrvers, *tmpvers;
93 static DownloadOptions dl_options = { "http://maps.google.com/", 0, a_check_map_file };
94 /* static const char *gvers_pat = "http://mt0.google.com/mt?v\\x3d"; */
95 static const char *gvers_pat = "http://mt0.google.com/mt";
96 static const char *kvers_pat = "http://khm0.google.com/kh?v\\x3d";
98 g_assert(google_type < TYPE_GOOGLE_NUM);
101 return (vers[google_type]);
105 gvers = tvers = kvers = terrvers = NULL;
106 if ((tmp_fd = g_file_open_tmp ("vikgvers.XXXXXX", &tmpname, NULL)) == -1) {
107 g_critical(_("couldn't open temp file %s"), tmpname);
111 google_mapcoord_to_center_coord(mapcoord, &coord);
112 uri = g_strdup_printf("http://maps.google.com/maps?f=q&hl=en&q=%s,%s",
113 g_ascii_dtostr (coord_north_south, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.north_south),
114 g_ascii_dtostr (coord_east_west, G_ASCII_DTOSTR_BUF_SIZE, (gdouble) coord.east_west));
115 tmp_file = fdopen(tmp_fd, "r+");
117 if (curl_download_uri(uri, tmp_file, &dl_options)) { /* error */
118 g_warning(_("Failed downloading %s"), tmpname);
120 if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) {
121 g_critical(_("couldn't map temp file"));
124 len = g_mapped_file_get_length(mf);
125 text = g_mapped_file_get_contents(mf);
127 if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) {
128 g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)"));
133 while (!gvers || !tvers ||!terrvers) {
134 if ((pat = g_strstr_len(pat, &text[len] - pat, gvers_pat)) != NULL) {
135 pat += strlen(gvers_pat);
136 if ((pat[0] != '/' && pat[0] != '?') ||
137 pat[1] != 'v' || pat[2] != '\\' ||
138 pat[3] != 'x' || pat[4] != '3' || pat[5] != 'd')
141 if ((tmpvers = parse_version_number(pat)) != NULL) {
142 if (strstr(tmpvers, "t."))
144 else if (strstr(tmpvers, "p."))
154 if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL)
155 kvers = parse_version_number(pat + strlen(kvers_pat));
157 if (gvers && tvers && kvers) {
158 vers[TYPE_GOOGLE_MAPS] = gvers;
159 vers[TYPE_GOOGLE_TRANS] = tvers;
160 vers[TYPE_GOOGLE_SAT] = kvers;
161 vers[TYPE_GOOGLE_TERRAIN] = terrvers;
164 g_warning(_("Failed getting google version numbers"));
167 fprintf(stderr, "DEBUG gvers=%s\n", gvers);
169 fprintf(stderr, "DEBUG tvers=%s\n", tvers);
171 fprintf(stderr, "DEBUG terrvers=%s\n", terrvers);
173 fprintf(stderr, "DEBUG kvers=%s\n", kvers);
176 g_mapped_file_free(mf);
183 return (vers[google_type]);
188 class Google:Map:Type from Vik:Map:Type {
189 private GoogleType type;
191 override (Vik:Map:Type) gboolean
192 coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
194 g_assert ( src->mode == VIK_COORD_LATLON );
196 if ( xzoom != yzoom )
199 dest->scale = google_zoom ( xzoom );
200 if ( dest->scale == 255 )
203 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
204 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
210 override (Vik:Map:Type) void
211 mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
213 gdouble socalled_mpp = GZ(src->scale);
214 dest->mode = VIK_COORD_LATLON;
215 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
216 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
219 override (Vik:Map:Type) int
220 download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
222 const gchar *vers_str = google_version_number(src, GOOGLE_MAP_TYPE(self)->_priv->type);
223 return(real_google_download ( src, dest_fn, vers_str ));