]>
Commit | Line | Data |
---|---|---|
e3a90009 GB |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking | |
a482007a | 4 | * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com> |
e3a90009 GB |
5 | * |
6 | * viking is free software: you can redistribute it and/or modify it | |
7 | * under the terms of the GNU General Public License as published by the | |
8 | * Free Software Foundation, either version 3 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * viking is distributed in the hope that it will be useful, but | |
12 | * WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
14 | * See the GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License along | |
17 | * with this program. If not, see <http://www.gnu.org/licenses/>. | |
18 | */ | |
dfc4db8b GB |
19 | |
20 | /** | |
21 | * SECTION:vikslippymapsource | |
22 | * @short_description: the class for SlippyMap oriented map sources | |
23 | * | |
24 | * The #VikSlippyMapSource class handles slippy map oriented map sources. | |
25 | * The related service is tile oriented, à la Google. | |
26 | * | |
27 | * The tiles are in 'google spherical mercator', which is | |
28 | * basically a mercator projection that assumes a spherical earth. | |
29 | * http://docs.openlayers.org/library/spherical_mercator.html | |
30 | * | |
31 | * Such service is also a type of TMS (Tile Map Service) as defined in | |
32 | * OSGeo's wiki. | |
33 | * http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification | |
34 | * But take care that the Y axis is inverted, ie the origin is at top-left | |
35 | * corner. | |
36 | * Following this specification, the protocol handled by this class | |
37 | * follows the global-mercator profile. | |
38 | * | |
39 | * You can also find many interesting information on the OSM's wiki. | |
40 | * http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames | |
41 | * http://wiki.openstreetmap.org/wiki/Setting_up_TMS | |
42 | */ | |
43 | ||
e3a90009 GB |
44 | #ifdef HAVE_CONFIG_H |
45 | #include "config.h" | |
46 | #endif | |
47 | ||
48 | #ifdef HAVE_MATH_H | |
49 | #include <math.h> | |
50 | #endif | |
51 | ||
52 | #include "globals.h" | |
8eb12ac6 | 53 | #include "vikslippymapsource.h" |
e3a90009 GB |
54 | |
55 | static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ); | |
56 | static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest ); | |
f66ea3ec | 57 | |
c81ded98 | 58 | static gboolean _supports_download_only_new (VikMapSource *self ); |
e3a90009 | 59 | |
f66ea3ec GB |
60 | static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src ); |
61 | static gchar *_get_hostname( VikMapSourceDefault *self ); | |
62 | static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self ); | |
e3a90009 | 63 | |
8eb12ac6 GB |
64 | typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate; |
65 | struct _VikSlippyMapSourcePrivate | |
66 | { | |
67 | gchar *hostname; | |
68 | gchar *url; | |
b529dc9c | 69 | DownloadMapOptions options; |
8eb12ac6 GB |
70 | }; |
71 | ||
72 | #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate)) | |
73 | ||
4e53f3dc GB |
74 | /* properties */ |
75 | enum | |
76 | { | |
77 | PROP_0, | |
78 | ||
79 | PROP_HOSTNAME, | |
80 | PROP_URL, | |
c395c6a5 GB |
81 | PROP_REFERER, |
82 | PROP_FOLLOW_LOCATION, | |
0f08bd0d | 83 | PROP_CHECK_FILE_SERVER_TIME, |
05dbd9ba | 84 | PROP_USE_ETAG, |
4e53f3dc GB |
85 | }; |
86 | ||
f6411015 | 87 | G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT); |
e3a90009 GB |
88 | |
89 | static void | |
3a84e537 | 90 | vik_slippy_map_source_init (VikSlippyMapSource *self) |
e3a90009 | 91 | { |
9cca9d93 GB |
92 | /* initialize the object here */ |
93 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
94 | ||
95 | priv->hostname = NULL; | |
96 | priv->url = NULL; | |
c395c6a5 GB |
97 | priv->options.referer = NULL; |
98 | priv->options.follow_location = 0; | |
99 | priv->options.check_file = a_check_map_file; | |
6693f5f9 | 100 | priv->options.check_file_server_time = FALSE; |
9cca9d93 GB |
101 | |
102 | g_object_set (G_OBJECT (self), | |
103 | "tilesize-x", 256, | |
104 | "tilesize-y", 256, | |
105 | "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR, | |
106 | NULL); | |
e3a90009 GB |
107 | } |
108 | ||
109 | static void | |
8eb12ac6 | 110 | vik_slippy_map_source_finalize (GObject *object) |
e3a90009 | 111 | { |
9cca9d93 GB |
112 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); |
113 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
114 | ||
115 | g_free (priv->hostname); | |
116 | priv->hostname = NULL; | |
117 | g_free (priv->url); | |
118 | priv->url = NULL; | |
c395c6a5 GB |
119 | g_free (priv->options.referer); |
120 | priv->options.referer = NULL; | |
e3a90009 | 121 | |
9cca9d93 | 122 | G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object); |
e3a90009 GB |
123 | } |
124 | ||
4e53f3dc GB |
125 | static void |
126 | vik_slippy_map_source_set_property (GObject *object, | |
127 | guint property_id, | |
128 | const GValue *value, | |
129 | GParamSpec *pspec) | |
130 | { | |
131 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); | |
132 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
133 | ||
134 | switch (property_id) | |
135 | { | |
136 | case PROP_HOSTNAME: | |
137 | g_free (priv->hostname); | |
138 | priv->hostname = g_value_dup_string (value); | |
139 | break; | |
140 | ||
141 | case PROP_URL: | |
142 | g_free (priv->url); | |
143 | priv->url = g_value_dup_string (value); | |
144 | break; | |
145 | ||
c395c6a5 GB |
146 | case PROP_REFERER: |
147 | g_free (priv->options.referer); | |
148 | priv->options.referer = g_value_dup_string (value); | |
149 | break; | |
150 | ||
151 | case PROP_FOLLOW_LOCATION: | |
152 | priv->options.follow_location = g_value_get_long (value); | |
153 | break; | |
154 | ||
0f08bd0d | 155 | case PROP_CHECK_FILE_SERVER_TIME: |
6693f5f9 | 156 | priv->options.check_file_server_time = g_value_get_boolean (value); |
0f08bd0d GB |
157 | break; |
158 | ||
05dbd9ba JJ |
159 | case PROP_USE_ETAG: |
160 | priv->options.use_etag = g_value_get_boolean (value); | |
161 | break; | |
162 | ||
4e53f3dc GB |
163 | default: |
164 | /* We don't have any other property... */ | |
165 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
166 | break; | |
167 | } | |
168 | } | |
169 | ||
170 | static void | |
171 | vik_slippy_map_source_get_property (GObject *object, | |
172 | guint property_id, | |
173 | GValue *value, | |
174 | GParamSpec *pspec) | |
175 | { | |
176 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); | |
177 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
178 | ||
179 | switch (property_id) | |
180 | { | |
181 | case PROP_HOSTNAME: | |
182 | g_value_set_string (value, priv->hostname); | |
183 | break; | |
184 | ||
185 | case PROP_URL: | |
186 | g_value_set_string (value, priv->url); | |
187 | break; | |
188 | ||
c395c6a5 GB |
189 | case PROP_REFERER: |
190 | g_value_set_string (value, priv->options.referer); | |
191 | break; | |
192 | ||
193 | case PROP_FOLLOW_LOCATION: | |
194 | g_value_set_long (value, priv->options.follow_location); | |
195 | break; | |
196 | ||
0f08bd0d | 197 | case PROP_CHECK_FILE_SERVER_TIME: |
6693f5f9 | 198 | g_value_set_boolean (value, priv->options.check_file_server_time); |
0f08bd0d GB |
199 | break; |
200 | ||
05dbd9ba JJ |
201 | case PROP_USE_ETAG: |
202 | g_value_set_boolean (value, priv->options.use_etag); | |
203 | break; | |
204 | ||
4e53f3dc GB |
205 | default: |
206 | /* We don't have any other property... */ | |
207 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
208 | break; | |
209 | } | |
210 | } | |
211 | ||
e3a90009 | 212 | static void |
8eb12ac6 | 213 | vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass) |
e3a90009 GB |
214 | { |
215 | GObjectClass* object_class = G_OBJECT_CLASS (klass); | |
de674329 | 216 | VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass); |
f66ea3ec | 217 | VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass); |
4e53f3dc GB |
218 | GParamSpec *pspec = NULL; |
219 | ||
220 | object_class->set_property = vik_slippy_map_source_set_property; | |
221 | object_class->get_property = vik_slippy_map_source_get_property; | |
e3a90009 GB |
222 | |
223 | /* Overiding methods */ | |
de674329 RN |
224 | grandparent_class->coord_to_mapcoord = _coord_to_mapcoord; |
225 | grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord; | |
226 | grandparent_class->supports_download_only_new = _supports_download_only_new; | |
f66ea3ec GB |
227 | |
228 | parent_class->get_uri = _get_uri; | |
229 | parent_class->get_hostname = _get_hostname; | |
230 | parent_class->get_download_options = _get_download_options; | |
4e53f3dc GB |
231 | |
232 | pspec = g_param_spec_string ("hostname", | |
233 | "Hostname", | |
234 | "The hostname of the map server", | |
235 | "<no-set>" /* default value */, | |
236 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
237 | g_object_class_install_property (object_class, PROP_HOSTNAME, pspec); | |
238 | ||
239 | pspec = g_param_spec_string ("url", | |
240 | "URL", | |
241 | "The template of the tiles' URL", | |
242 | "<no-set>" /* default value */, | |
243 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
244 | g_object_class_install_property (object_class, PROP_URL, pspec); | |
c395c6a5 GB |
245 | |
246 | pspec = g_param_spec_string ("referer", | |
247 | "Referer", | |
248 | "The REFERER string to use in HTTP request", | |
249 | NULL /* default value */, | |
250 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
251 | g_object_class_install_property (object_class, PROP_REFERER, pspec); | |
8eb12ac6 | 252 | |
c395c6a5 GB |
253 | pspec = g_param_spec_long ("follow-location", |
254 | "Follow location", | |
255 | "Specifies the number of retries to follow a redirect while downloading a page", | |
256 | 0 /* minimum value */, | |
257 | G_MAXLONG /* maximum value */, | |
258 | 0 /* default value */, | |
259 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
260 | g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec); | |
0f08bd0d | 261 | |
6693f5f9 GB |
262 | pspec = g_param_spec_boolean ("check-file-server-time", |
263 | "Check file server time", | |
264 | "Age of current cache before redownloading tile", | |
265 | FALSE /* default value */, | |
266 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
0f08bd0d | 267 | g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec); |
c395c6a5 | 268 | |
05dbd9ba JJ |
269 | pspec = g_param_spec_boolean ("use-etag", |
270 | "Use etag values with server", | |
271 | "Store etag in a file, and send it to server to check if we have the latest file", | |
272 | FALSE /* default value */, | |
273 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
274 | g_object_class_install_property (object_class, PROP_USE_ETAG, pspec); | |
275 | ||
8eb12ac6 GB |
276 | g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate)); |
277 | ||
278 | object_class->finalize = vik_slippy_map_source_finalize; | |
e3a90009 GB |
279 | } |
280 | ||
281 | /* 1 << (x) is like a 2**(x) */ | |
a2833555 | 282 | #define GZ(x) ((1<<(x))) |
e3a90009 GB |
283 | |
284 | 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), | |
285 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
286 | ||
287 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
288 | ||
d1302dec JJ |
289 | static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) }; |
290 | static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0])); | |
291 | ||
e3a90009 | 292 | #define ERROR_MARGIN 0.01 |
d1302dec | 293 | static gint slippy_zoom ( gdouble mpp ) { |
e3a90009 GB |
294 | gint i; |
295 | for ( i = 0; i < num_scales; i++ ) { | |
d1302dec | 296 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { |
e3a90009 | 297 | return i; |
d1302dec | 298 | } |
e3a90009 | 299 | } |
d1302dec JJ |
300 | for ( i = 0; i < num_scales_neg; i++ ) { |
301 | if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) { | |
302 | return -i; | |
303 | } | |
304 | } | |
305 | ||
e3a90009 GB |
306 | return 255; |
307 | } | |
308 | ||
0f08bd0d | 309 | gboolean |
c81ded98 | 310 | _supports_download_only_new (VikMapSource *self) |
0f08bd0d | 311 | { |
c81ded98 | 312 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE); |
0f08bd0d | 313 | |
c81ded98 | 314 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); |
0f08bd0d | 315 | |
c81ded98 | 316 | return priv->options.check_file_server_time || priv->options.use_etag; |
0f08bd0d | 317 | } |
f66ea3ec | 318 | |
e3a90009 GB |
319 | static gboolean |
320 | _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) | |
321 | { | |
322 | g_assert ( src->mode == VIK_COORD_LATLON ); | |
323 | ||
324 | if ( xzoom != yzoom ) | |
325 | return FALSE; | |
326 | ||
327 | dest->scale = slippy_zoom ( xzoom ); | |
328 | if ( dest->scale == 255 ) | |
329 | return FALSE; | |
330 | ||
331 | dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom; | |
332 | dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom; | |
333 | dest->z = 0; | |
334 | ||
335 | return TRUE; | |
336 | } | |
337 | ||
338 | static void | |
339 | _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest ) | |
340 | { | |
d1302dec JJ |
341 | gdouble socalled_mpp; |
342 | if (src->scale >= 0) | |
343 | socalled_mpp = GZ(src->scale); | |
344 | else | |
345 | socalled_mpp = 1.0/GZ(-src->scale); | |
e3a90009 GB |
346 | dest->mode = VIK_COORD_LATLON; |
347 | dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180; | |
348 | dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360)); | |
349 | } | |
350 | ||
8eb12ac6 | 351 | static gchar * |
f66ea3ec | 352 | _get_uri( VikMapSourceDefault *self, MapCoord *src ) |
8eb12ac6 GB |
353 | { |
354 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
355 | ||
356 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
357 | gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y); | |
358 | return uri; | |
359 | } | |
360 | ||
361 | static gchar * | |
f66ea3ec | 362 | _get_hostname( VikMapSourceDefault *self ) |
8eb12ac6 GB |
363 | { |
364 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
365 | ||
366 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
367 | return g_strdup( priv->hostname ); | |
368 | } | |
369 | ||
b529dc9c | 370 | static DownloadMapOptions * |
f66ea3ec | 371 | _get_download_options( VikMapSourceDefault *self ) |
8eb12ac6 GB |
372 | { |
373 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
374 | ||
c395c6a5 GB |
375 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); |
376 | return &(priv->options); | |
8eb12ac6 GB |
377 | } |
378 | ||
379 | VikSlippyMapSource * | |
db03733a | 380 | vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url) |
8eb12ac6 | 381 | { |
4e53f3dc | 382 | return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE, |
db03733a | 383 | "id", id, "label", label, "hostname", hostname, "url", url, NULL); |
c395c6a5 | 384 | } |