]>
Commit | Line | Data |
---|---|---|
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ | |
2 | /* | |
3 | * viking | |
4 | * Copyright (C) Guilhem Bonnefille 2009 <guilhem.bonnefille@gmail.com> | |
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 | */ | |
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 | ||
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" | |
53 | #include "vikslippymapsource.h" | |
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 ); | |
57 | static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle ); | |
58 | static void * _download_handle_init ( VikMapSource *self); | |
59 | static void _download_handle_cleanup ( VikMapSource *self, void *handle); | |
60 | static gboolean _supports_if_modified_since (VikMapSource *self ); | |
61 | ||
62 | static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src ); | |
63 | static gchar *_get_hostname( VikSlippyMapSource *self ); | |
64 | static DownloadOptions *_get_download_options( VikSlippyMapSource *self ); | |
65 | ||
66 | typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate; | |
67 | struct _VikSlippyMapSourcePrivate | |
68 | { | |
69 | gchar *hostname; | |
70 | gchar *url; | |
71 | DownloadOptions options; | |
72 | }; | |
73 | ||
74 | #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate)) | |
75 | ||
76 | /* properties */ | |
77 | enum | |
78 | { | |
79 | PROP_0, | |
80 | ||
81 | PROP_HOSTNAME, | |
82 | PROP_URL, | |
83 | PROP_REFERER, | |
84 | PROP_FOLLOW_LOCATION, | |
85 | PROP_CHECK_FILE_SERVER_TIME, | |
86 | }; | |
87 | ||
88 | G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT); | |
89 | ||
90 | static void | |
91 | vik_slippy_map_source_init (VikSlippyMapSource *self) | |
92 | { | |
93 | /* initialize the object here */ | |
94 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
95 | ||
96 | priv->hostname = NULL; | |
97 | priv->url = NULL; | |
98 | priv->options.referer = NULL; | |
99 | priv->options.follow_location = 0; | |
100 | priv->options.check_file = a_check_map_file; | |
101 | priv->options.check_file_server_time = FALSE; | |
102 | ||
103 | g_object_set (G_OBJECT (self), | |
104 | "tilesize-x", 256, | |
105 | "tilesize-y", 256, | |
106 | "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR, | |
107 | NULL); | |
108 | } | |
109 | ||
110 | static void | |
111 | vik_slippy_map_source_finalize (GObject *object) | |
112 | { | |
113 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); | |
114 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
115 | ||
116 | g_free (priv->hostname); | |
117 | priv->hostname = NULL; | |
118 | g_free (priv->url); | |
119 | priv->url = NULL; | |
120 | g_free (priv->options.referer); | |
121 | priv->options.referer = NULL; | |
122 | ||
123 | G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object); | |
124 | } | |
125 | ||
126 | static void | |
127 | vik_slippy_map_source_set_property (GObject *object, | |
128 | guint property_id, | |
129 | const GValue *value, | |
130 | GParamSpec *pspec) | |
131 | { | |
132 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); | |
133 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
134 | ||
135 | switch (property_id) | |
136 | { | |
137 | case PROP_HOSTNAME: | |
138 | g_free (priv->hostname); | |
139 | priv->hostname = g_value_dup_string (value); | |
140 | break; | |
141 | ||
142 | case PROP_URL: | |
143 | g_free (priv->url); | |
144 | priv->url = g_value_dup_string (value); | |
145 | break; | |
146 | ||
147 | case PROP_REFERER: | |
148 | g_free (priv->options.referer); | |
149 | priv->options.referer = g_value_dup_string (value); | |
150 | break; | |
151 | ||
152 | case PROP_FOLLOW_LOCATION: | |
153 | priv->options.follow_location = g_value_get_long (value); | |
154 | break; | |
155 | ||
156 | case PROP_CHECK_FILE_SERVER_TIME: | |
157 | priv->options.check_file_server_time = g_value_get_boolean (value); | |
158 | break; | |
159 | ||
160 | default: | |
161 | /* We don't have any other property... */ | |
162 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
163 | break; | |
164 | } | |
165 | } | |
166 | ||
167 | static void | |
168 | vik_slippy_map_source_get_property (GObject *object, | |
169 | guint property_id, | |
170 | GValue *value, | |
171 | GParamSpec *pspec) | |
172 | { | |
173 | VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object); | |
174 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self); | |
175 | ||
176 | switch (property_id) | |
177 | { | |
178 | case PROP_HOSTNAME: | |
179 | g_value_set_string (value, priv->hostname); | |
180 | break; | |
181 | ||
182 | case PROP_URL: | |
183 | g_value_set_string (value, priv->url); | |
184 | break; | |
185 | ||
186 | case PROP_REFERER: | |
187 | g_value_set_string (value, priv->options.referer); | |
188 | break; | |
189 | ||
190 | case PROP_FOLLOW_LOCATION: | |
191 | g_value_set_long (value, priv->options.follow_location); | |
192 | break; | |
193 | ||
194 | case PROP_CHECK_FILE_SERVER_TIME: | |
195 | g_value_set_boolean (value, priv->options.check_file_server_time); | |
196 | break; | |
197 | ||
198 | default: | |
199 | /* We don't have any other property... */ | |
200 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
201 | break; | |
202 | } | |
203 | } | |
204 | ||
205 | static void | |
206 | vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass) | |
207 | { | |
208 | GObjectClass* object_class = G_OBJECT_CLASS (klass); | |
209 | VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass); | |
210 | GParamSpec *pspec = NULL; | |
211 | ||
212 | object_class->set_property = vik_slippy_map_source_set_property; | |
213 | object_class->get_property = vik_slippy_map_source_get_property; | |
214 | ||
215 | /* Overiding methods */ | |
216 | parent_class->coord_to_mapcoord = _coord_to_mapcoord; | |
217 | parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord; | |
218 | parent_class->download = _download; | |
219 | parent_class->download_handle_init = _download_handle_init; | |
220 | parent_class->download_handle_cleanup = _download_handle_cleanup; | |
221 | parent_class->supports_if_modified_since = _supports_if_modified_since; | |
222 | ||
223 | /* Default implementation of methods */ | |
224 | klass->get_uri = _get_uri; | |
225 | klass->get_hostname = _get_hostname; | |
226 | klass->get_download_options = _get_download_options; | |
227 | ||
228 | pspec = g_param_spec_string ("hostname", | |
229 | "Hostname", | |
230 | "The hostname of the map server", | |
231 | "<no-set>" /* default value */, | |
232 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
233 | g_object_class_install_property (object_class, PROP_HOSTNAME, pspec); | |
234 | ||
235 | pspec = g_param_spec_string ("url", | |
236 | "URL", | |
237 | "The template of the tiles' URL", | |
238 | "<no-set>" /* default value */, | |
239 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
240 | g_object_class_install_property (object_class, PROP_URL, pspec); | |
241 | ||
242 | pspec = g_param_spec_string ("referer", | |
243 | "Referer", | |
244 | "The REFERER string to use in HTTP request", | |
245 | NULL /* default value */, | |
246 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
247 | g_object_class_install_property (object_class, PROP_REFERER, pspec); | |
248 | ||
249 | pspec = g_param_spec_long ("follow-location", | |
250 | "Follow location", | |
251 | "Specifies the number of retries to follow a redirect while downloading a page", | |
252 | 0 /* minimum value */, | |
253 | G_MAXLONG /* maximum value */, | |
254 | 0 /* default value */, | |
255 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
256 | g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec); | |
257 | ||
258 | pspec = g_param_spec_boolean ("check-file-server-time", | |
259 | "Check file server time", | |
260 | "Age of current cache before redownloading tile", | |
261 | FALSE /* default value */, | |
262 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
263 | g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec); | |
264 | ||
265 | g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate)); | |
266 | ||
267 | object_class->finalize = vik_slippy_map_source_finalize; | |
268 | } | |
269 | ||
270 | /* 1 << (x) is like a 2**(x) */ | |
271 | #define GZ(x) ((1<<x)) | |
272 | ||
273 | 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), | |
274 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
275 | ||
276 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
277 | ||
278 | static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) }; | |
279 | static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0])); | |
280 | ||
281 | #define ERROR_MARGIN 0.01 | |
282 | static gint slippy_zoom ( gdouble mpp ) { | |
283 | gint i; | |
284 | for ( i = 0; i < num_scales; i++ ) { | |
285 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { | |
286 | return i; | |
287 | } | |
288 | } | |
289 | for ( i = 0; i < num_scales_neg; i++ ) { | |
290 | if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) { | |
291 | return -i; | |
292 | } | |
293 | } | |
294 | ||
295 | return 255; | |
296 | } | |
297 | ||
298 | gchar * | |
299 | vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src ) | |
300 | { | |
301 | VikSlippyMapSourceClass *klass; | |
302 | g_return_val_if_fail (self != NULL, 0); | |
303 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0); | |
304 | klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self); | |
305 | ||
306 | g_return_val_if_fail (klass->get_uri != NULL, 0); | |
307 | ||
308 | return (*klass->get_uri)(self, src); | |
309 | } | |
310 | ||
311 | gchar * | |
312 | vik_slippy_map_source_get_hostname( VikSlippyMapSource *self ) | |
313 | { | |
314 | VikSlippyMapSourceClass *klass; | |
315 | g_return_val_if_fail (self != NULL, 0); | |
316 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0); | |
317 | klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self); | |
318 | ||
319 | g_return_val_if_fail (klass->get_hostname != NULL, 0); | |
320 | ||
321 | return (*klass->get_hostname)(self); | |
322 | } | |
323 | ||
324 | DownloadOptions * | |
325 | vik_slippy_map_source_get_download_options( VikSlippyMapSource *self ) | |
326 | { | |
327 | VikSlippyMapSourceClass *klass; | |
328 | g_return_val_if_fail (self != NULL, 0); | |
329 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0); | |
330 | klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self); | |
331 | ||
332 | g_return_val_if_fail (klass->get_download_options != NULL, 0); | |
333 | ||
334 | return (*klass->get_download_options)(self); | |
335 | } | |
336 | ||
337 | gboolean | |
338 | _supports_if_modified_since (VikMapSource *self) | |
339 | { | |
340 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE); | |
341 | ||
342 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
343 | ||
344 | return priv->options.check_file_server_time; | |
345 | } | |
346 | static gboolean | |
347 | _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) | |
348 | { | |
349 | g_assert ( src->mode == VIK_COORD_LATLON ); | |
350 | ||
351 | if ( xzoom != yzoom ) | |
352 | return FALSE; | |
353 | ||
354 | dest->scale = slippy_zoom ( xzoom ); | |
355 | if ( dest->scale == 255 ) | |
356 | return FALSE; | |
357 | ||
358 | dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom; | |
359 | dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom; | |
360 | dest->z = 0; | |
361 | ||
362 | return TRUE; | |
363 | } | |
364 | ||
365 | static void | |
366 | _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest ) | |
367 | { | |
368 | gdouble socalled_mpp; | |
369 | if (src->scale >= 0) | |
370 | socalled_mpp = GZ(src->scale); | |
371 | else | |
372 | socalled_mpp = 1.0/GZ(-src->scale); | |
373 | dest->mode = VIK_COORD_LATLON; | |
374 | dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180; | |
375 | dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360)); | |
376 | } | |
377 | ||
378 | static int | |
379 | _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle ) | |
380 | { | |
381 | int res; | |
382 | gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src); | |
383 | gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self)); | |
384 | DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self)); | |
385 | res = a_http_download_get_url ( host, uri, dest_fn, options, handle ); | |
386 | g_free ( uri ); | |
387 | g_free ( host ); | |
388 | return res; | |
389 | } | |
390 | ||
391 | static void * | |
392 | _download_handle_init ( VikMapSource *self ) | |
393 | { | |
394 | return a_download_handle_init (); | |
395 | } | |
396 | ||
397 | ||
398 | static void | |
399 | _download_handle_cleanup ( VikMapSource *self, void *handle ) | |
400 | { | |
401 | return a_download_handle_cleanup ( handle ); | |
402 | } | |
403 | ||
404 | static gchar * | |
405 | _get_uri( VikSlippyMapSource *self, MapCoord *src ) | |
406 | { | |
407 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
408 | ||
409 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
410 | gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y); | |
411 | return uri; | |
412 | } | |
413 | ||
414 | static gchar * | |
415 | _get_hostname( VikSlippyMapSource *self ) | |
416 | { | |
417 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
418 | ||
419 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
420 | return g_strdup( priv->hostname ); | |
421 | } | |
422 | ||
423 | static DownloadOptions * | |
424 | _get_download_options( VikSlippyMapSource *self ) | |
425 | { | |
426 | g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL); | |
427 | ||
428 | VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self); | |
429 | return &(priv->options); | |
430 | } | |
431 | ||
432 | VikSlippyMapSource * | |
433 | vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url) | |
434 | { | |
435 | return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE, | |
436 | "id", id, "label", label, "hostname", hostname, "url", url, NULL); | |
437 | } |