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