]> git.street.me.uk Git - andy/viking.git/blame - src/google-map-type.gob
Add Terraserver
[andy/viking.git] / src / google-map-type.gob
CommitLineData
124ed814 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
32typedef 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/* 1 << (x) is like a 2**(x) */
46#define GZ(x) ((1<<x))
47
48static 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) };
50
51static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
52
53#define ERROR_MARGIN 0.01
54guint8 google_zoom ( gdouble mpp ) {
55 gint i;
56 for ( i = 0; i < num_scales; i++ ) {
57 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
58 return i;
59 }
60 return 255;
61}
62
63
64static gchar *parse_version_number(gchar *text)
65{
66 int i;
67 gchar *vers;
68 gchar *s = text;
69
70 for (i = 0; (s[i] != '\\') && (i < 8); i++)
71 ;
72 if (s[i] != '\\') {
73 return NULL;
74 }
75
76 return vers = g_strndup(s, i);
77}
78
79static const gchar *google_version_number(MapCoord *mapcoord, GoogleType google_type)
80{
81 static gboolean first = TRUE;
82 static char *vers[] = { "w2.80", "w2t.80", "30", "w2p.81"};
83 FILE *tmp_file;
84 int tmp_fd;
85 gchar *tmpname;
86 gchar *uri;
87 VikCoord coord;
88 gchar coord_north_south[G_ASCII_DTOSTR_BUF_SIZE], coord_east_west[G_ASCII_DTOSTR_BUF_SIZE];
89 gchar *text, *pat, *beg;
90 GMappedFile *mf;
91 gsize len;
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";
97
98 g_assert(google_type < TYPE_GOOGLE_NUM);
99
100 if (!first)
101 return (vers[google_type]);
102
103
104 first = FALSE;
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);
108 exit(1);
109 }
110
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+");
116
117 if (curl_download_uri(uri, tmp_file, &dl_options)) { /* error */
118 g_warning(_("Failed downloading %s"), tmpname);
119 } else {
120 if ((mf = g_mapped_file_new(tmpname, FALSE, NULL)) == NULL) {
121 g_critical(_("couldn't map temp file"));
122 exit(1);
123 }
124 len = g_mapped_file_get_length(mf);
125 text = g_mapped_file_get_contents(mf);
126
127 if ((beg = g_strstr_len(text, len, "GLoadApi")) == NULL) {
128 g_warning(_("Failed fetching Google numbers (\"GLoadApi\" not found)"));
129 goto failed;
130 }
131
132 pat = beg;
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')
139 continue;
140 pat += 6;
141 if ((tmpvers = parse_version_number(pat)) != NULL) {
142 if (strstr(tmpvers, "t."))
143 tvers = tmpvers;
144 else if (strstr(tmpvers, "p."))
145 terrvers = tmpvers;
146 else
147 gvers = tmpvers;
148 }
149 }
150 else
151 break;
152 }
153
154 if ((pat = g_strstr_len(beg, &text[len] - beg, kvers_pat)) != NULL)
155 kvers = parse_version_number(pat + strlen(kvers_pat));
156
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;
162 }
163 else
164 g_warning(_("Failed getting google version numbers"));
165
166 if (gvers)
167 fprintf(stderr, "DEBUG gvers=%s\n", gvers);
168 if (tvers)
169 fprintf(stderr, "DEBUG tvers=%s\n", tvers);
170 if (terrvers)
171 fprintf(stderr, "DEBUG terrvers=%s\n", terrvers);
172 if (kvers)
173 fprintf(stderr, "DEBUG kvers=%s\n", kvers);
174
175failed:
176 g_mapped_file_free(mf);
177 }
178
179 fclose(tmp_file);
180 tmp_file = NULL;
181 g_free(tmpname);
182 g_free (uri);
183 return (vers[google_type]);
184}
185
186%}
187
188class Google:Map:Type from Vik:Map:Type {
189 private GoogleType type;
190
191 override (Vik:Map:Type) gboolean
192 coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
193 {
194 g_assert ( src->mode == VIK_COORD_LATLON );
195
196 if ( xzoom != yzoom )
197 return FALSE;
198
199 dest->scale = google_zoom ( xzoom );
200 if ( dest->scale == 255 )
201 return FALSE;
202
203 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
204 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
205 dest->z = 0;
206
207 return TRUE;
208 }
209
210 override (Vik:Map:Type) void
211 mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
212 {
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));
217 }
218
219 override (Vik:Map:Type) int
220 download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
221 {
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 ));
224 }
225}