]>
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 | */ | |
21 | ||
22 | #include <gtk/gtk.h> | |
23 | #include <math.h> | |
24 | #include <string.h> | |
25 | #include "coords.h" | |
26 | #include "vikcoord.h" | |
27 | #include "mapcoord.h" | |
28 | #include "http.h" | |
29 | #include "globals.h" | |
30 | #include "google.h" | |
cdcaf41c QT |
31 | #include "vikmapslayer.h" |
32 | ||
33 | void google_init () { | |
34 | VikMapsLayer_MapType google_1 = { 7, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_download }; | |
35 | VikMapsLayer_MapType google_2 = { 10, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_trans_download }; | |
36 | VikMapsLayer_MapType google_3 = { 11, 256, 256, VIK_VIEWPORT_DRAWMODE_MERCATOR, google_coord_to_mapcoord, google_mapcoord_to_center_coord, google_kh_download }; | |
37 | ||
38 | maps_layer_register_type("Google Maps", 7, &google_1); | |
39 | maps_layer_register_type("Transparent Google Maps", 10, &google_2); | |
40 | maps_layer_register_type("Google Satellite Images", 11, &google_3); | |
41 | } | |
50a14534 EB |
42 | |
43 | /* 1 << (x) is like a 2**(x) */ | |
44 | #define GZ(x) ((1<<x)) | |
45 | ||
46 | 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), | |
47 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
48 | ||
49 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
50 | ||
51 | #define ERROR_MARGIN 0.01 | |
52 | guint8 google_zoom ( gdouble mpp ) { | |
53 | gint i; | |
54 | for ( i = 0; i < num_scales; i++ ) { | |
55 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) | |
56 | return i; | |
57 | } | |
58 | return 255; | |
59 | } | |
60 | ||
61 | gboolean google_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) | |
62 | { | |
63 | g_assert ( src->mode == VIK_COORD_LATLON ); | |
64 | ||
65 | if ( xzoom != yzoom ) | |
66 | return FALSE; | |
67 | ||
68 | dest->scale = google_zoom ( xzoom ); | |
69 | if ( dest->scale == 255 ) | |
70 | return FALSE; | |
71 | ||
72 | dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom; | |
73 | dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom; | |
74 | dest->z = 0; | |
75 | ||
76 | return TRUE; | |
77 | } | |
78 | ||
79 | void google_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest ) | |
80 | { | |
81 | gdouble socalled_mpp = GZ(src->scale); | |
82 | dest->mode = VIK_COORD_LATLON; | |
83 | dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180; | |
84 | dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360)); | |
85 | } | |
86 | ||
87 | static void real_google_download ( MapCoord *src, const gchar *dest_fn, const char *verstr ) | |
88 | { | |
89 | gchar *uri = g_strdup_printf ( "/mt?v=%s&x=%d&y=%d&zoom=%d", verstr, src->x, src->y, src->scale ); | |
90 | a_http_download_get_url ( "mt.google.com", uri, dest_fn ); | |
91 | g_free ( uri ); | |
92 | } | |
93 | ||
94 | void google_download ( MapCoord *src, const gchar *dest_fn ) | |
95 | { | |
b291388a | 96 | real_google_download ( src, dest_fn, "w2.35" ); |
50a14534 EB |
97 | } |
98 | ||
99 | void google_trans_download ( MapCoord *src, const gchar *dest_fn ) | |
100 | { | |
101 | real_google_download ( src, dest_fn, "w2t.1" ); | |
102 | } | |
103 | ||
104 | static char *kh_encode(guint32 x, guint32 y, guint8 scale) | |
105 | { | |
106 | gchar *buf = g_malloc ( (20-scale)*sizeof(gchar) ); | |
107 | guint32 ya = 1 << (17 - scale); | |
108 | gint8 i, j; | |
109 | ||
110 | if (y < 0 || (ya-1 < y)) { | |
111 | strcpy(buf,"tqq"); /* BAD */ | |
112 | return buf; | |
113 | } | |
114 | if (x < 0 || ya-1 < x) { | |
115 | x %= ya; | |
116 | if (x < 0) | |
117 | x += ya; | |
118 | } | |
119 | ||
120 | buf[0] = 't'; | |
121 | for (j = 1, i = 16; i >= scale; i--, j++) { | |
122 | ya /= 2; | |
123 | if (y < ya) { | |
124 | if (x < ya) | |
125 | buf[j]='q'; | |
126 | else { | |
127 | buf[j]='r'; | |
128 | x -= ya; | |
129 | } | |
130 | } else { | |
131 | if (x < ya) { | |
132 | buf[j] = 't'; | |
133 | y -= ya; | |
134 | } else { | |
135 | buf[j] = 's'; | |
136 | x -= ya; | |
137 | y -= ya; | |
138 | } | |
139 | } | |
140 | } | |
141 | buf[j] = '\0'; | |
142 | return buf; | |
143 | } | |
144 | ||
145 | void google_kh_download ( MapCoord *src, const gchar *dest_fn ) | |
146 | { | |
147 | gchar *khenc = kh_encode( src->x, src->y, src->scale ); | |
148 | gchar *uri = g_strdup_printf ( "/kh?v=3&t=%s", khenc ); | |
149 | g_free ( khenc ); | |
150 | a_http_download_get_url ( "kh.google.com", uri, dest_fn ); | |
151 | g_free ( uri ); | |
152 | } |