8 #include "vik-map-type.h"
16 #include <glib/gi18n.h>
24 #include "curl_download.h"
27 #include "vikmapslayer.h"
45 static DownloadOptions google_options = { "http://maps.google.com/", 0, a_check_map_file };
47 /* 1 << (x) is like a 2**(x) */
48 #define GZ(x) ((1<<x))
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) };
53 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
55 #define ERROR_MARGIN 0.01
56 guint8 google_zoom ( gdouble mpp ) {
58 for ( i = 0; i < num_scales; i++ ) {
59 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
66 static gchar *parse_version_number(gchar *text)
72 for (i = 0; (s[i] != '\\') && (i < 8); i++)
78 return vers = g_strndup(s, i);
81 static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type)
83 static gboolean first = TRUE;
84 static char *vers[] = { "w2.80", "w2t.80", "30", "w2p.81"};
90 gchar coord_north_south[G_ASCII_DTOSTR_BUF_SIZE], coord_east_west[G_ASCII_DTOSTR_BUF_SIZE];
91 gchar *text, *pat, *beg;
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";
100 g_assert(google_type < TYPE_GOOGLE_NUM);
103 return (vers[google_type]);
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);
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+");
119 if (curl_download_uri(uri, tmp_file, &dl_options)) { /* error */
120 g_warning(_("Failed downloading %s"), tmpname);
122 if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) {
123 g_critical(_("couldn't map temp file"));
126 len = g_mapped_file_get_length(mf);
127 text = g_mapped_file_get_contents(mf);
129 if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) {
130 g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)"));
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')
143 if ((tmpvers = parse_version_number(pat)) != NULL) {
144 if (strstr(tmpvers, "t."))
146 else if (strstr(tmpvers, "p."))
156 if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL)
157 kvers = parse_version_number(pat + strlen(kvers_pat));
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;
166 g_warning(_("Failed getting google version numbers"));
169 fprintf(stderr, "DEBUG gvers=%s\n", gvers);
171 fprintf(stderr, "DEBUG tvers=%s\n", tvers);
173 fprintf(stderr, "DEBUG terrvers=%s\n", terrvers);
175 fprintf(stderr, "DEBUG kvers=%s\n", kvers);
178 g_mapped_file_free(mf);
185 return (vers[google_type]);
188 static int real_google_download ( MapCoord *src, const gchar *dest_fn, const char *verstr )
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 );
199 class Google:Map:Type from Vik:Map:Type {
200 private GoogleType type;
203 /* initialize the object here */
204 self->tilesize_x = 256;
205 self->tilesize_y = 256;
206 self->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
210 new_with_id (guint8 id) {
211 GObject *ret = GET_NEW;
212 VIK_MAP_TYPE(ret)->uniq_id = id;
213 return G_OBJECT (ret);
218 GObject *ret = GET_NEW;
219 return G_OBJECT (ret);
222 override (Vik:Map:Type) gboolean
223 coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
225 g_assert ( src->mode == VIK_COORD_LATLON );
227 if ( xzoom != yzoom )
230 dest->scale = google_zoom ( xzoom );
231 if ( dest->scale == 255 )
234 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
235 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
241 override (Vik:Map:Type) void
242 mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
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));
250 override (Vik:Map:Type) int
251 download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
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 ));