]> git.street.me.uk Git - andy/viking.git/blob - src/viktmsmapsource.c
Fix: generate doc/reference/Makefile when needed
[andy/viking.git] / src / viktmsmapsource.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking
4  * Copyright (C) 2010, 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:viktmsmapsource
22   * @short_description: the class for TMS oriented map sources
23   * 
24   * The #VikTmsMapSource class handles TMS oriented map sources.
25   * 
26   * The tiles are in 'equirectangular'.
27   * http://en.wikipedia.org/wiki/Equirectangular_projection
28   * 
29   * Such service is also a type of TMS (Tile Map Service) as defined in
30   * OSGeo's wiki.
31   * http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
32   * Following this specification, the protocol handled by this class
33   * follows the global-geodetic profile.
34   */
35   
36 #ifdef HAVE_CONFIG_H
37 #include "config.h"
38 #endif
39
40 #ifdef HAVE_MATH_H
41 #include <math.h>
42 #endif
43
44 #include "globals.h"
45 #include "viktmsmapsource.h"
46
47 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
48 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
49 static gboolean _supports_download_only_new (VikMapSource *self );
50
51 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
52 static gchar *_get_hostname( VikMapSourceDefault *self );
53 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
54
55 typedef struct _VikTmsMapSourcePrivate VikTmsMapSourcePrivate;
56 struct _VikTmsMapSourcePrivate
57 {
58   gchar *hostname;
59   gchar *url;
60   DownloadMapOptions options;
61 };
62
63 #define VIK_TMS_MAP_SOURCE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_TMS_MAP_SOURCE, VikTmsMapSourcePrivate))
64
65 /* properties */
66 enum
67 {
68   PROP_0,
69
70   PROP_HOSTNAME,
71   PROP_URL,
72   PROP_REFERER,
73   PROP_FOLLOW_LOCATION,
74   PROP_CHECK_FILE_SERVER_TIME,
75 };
76
77 G_DEFINE_TYPE (VikTmsMapSource, vik_tms_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
78
79 static void
80 vik_tms_map_source_init (VikTmsMapSource *self)
81 {
82   /* initialize the object here */
83   VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
84
85   priv->hostname = NULL;
86   priv->url = NULL;
87   priv->options.referer = NULL;
88   priv->options.follow_location = 0;
89   priv->options.check_file = a_check_map_file;
90   priv->options.check_file_server_time = FALSE;
91
92   g_object_set (G_OBJECT (self),
93                 "tilesize-x", 256,
94                 "tilesize-y", 256,
95                 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
96                 NULL);
97 }
98
99 static void
100 vik_tms_map_source_finalize (GObject *object)
101 {
102   VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
103   VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
104
105   g_free (priv->hostname);
106   priv->hostname = NULL;
107   g_free (priv->url);
108   priv->url = NULL;
109   g_free (priv->options.referer);
110   priv->options.referer = NULL;
111
112   G_OBJECT_CLASS (vik_tms_map_source_parent_class)->finalize (object);
113 }
114
115 static void
116 vik_tms_map_source_set_property (GObject      *object,
117                                     guint         property_id,
118                                     const GValue *value,
119                                     GParamSpec   *pspec)
120 {
121   VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
122   VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
123
124   switch (property_id)
125     {
126     case PROP_HOSTNAME:
127       g_free (priv->hostname);
128       priv->hostname = g_value_dup_string (value);
129       break;
130
131     case PROP_URL:
132       g_free (priv->url);
133       priv->url = g_value_dup_string (value);
134       break;
135
136     case PROP_REFERER:
137       g_free (priv->options.referer);
138       priv->options.referer = g_value_dup_string (value);
139       break;
140
141     case PROP_FOLLOW_LOCATION:
142       priv->options.follow_location = g_value_get_long (value);
143       break;
144
145     case PROP_CHECK_FILE_SERVER_TIME:
146       priv->options.check_file_server_time = g_value_get_boolean (value);
147       break;
148
149     default:
150       /* We don't have any other property... */
151       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
152       break;
153     }
154 }
155
156 static void
157 vik_tms_map_source_get_property (GObject    *object,
158                                     guint       property_id,
159                                     GValue     *value,
160                                     GParamSpec *pspec)
161 {
162   VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
163   VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
164
165   switch (property_id)
166     {
167     case PROP_HOSTNAME:
168       g_value_set_string (value, priv->hostname);
169       break;
170
171     case PROP_URL:
172       g_value_set_string (value, priv->url);
173       break;
174
175     case PROP_REFERER:
176       g_value_set_string (value, priv->options.referer);
177       break;
178
179     case PROP_FOLLOW_LOCATION:
180       g_value_set_long (value, priv->options.follow_location);
181       break;
182
183     case PROP_CHECK_FILE_SERVER_TIME:
184       g_value_set_boolean (value, priv->options.check_file_server_time);
185       break;
186           
187     default:
188       /* We don't have any other property... */
189       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
190       break;
191     }
192 }
193
194 static void
195 vik_tms_map_source_class_init (VikTmsMapSourceClass *klass)
196 {
197         GObjectClass* object_class = G_OBJECT_CLASS (klass);
198         VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
199         VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
200         GParamSpec *pspec = NULL;
201                 
202         object_class->set_property = vik_tms_map_source_set_property;
203     object_class->get_property = vik_tms_map_source_get_property;
204
205         /* Overiding methods */
206         grandparent_class->coord_to_mapcoord =        _coord_to_mapcoord;
207         grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
208         grandparent_class->supports_download_only_new = _supports_download_only_new;
209         
210         parent_class->get_uri = _get_uri;
211         parent_class->get_hostname = _get_hostname;
212         parent_class->get_download_options = _get_download_options;
213
214         pspec = g_param_spec_string ("hostname",
215                                      "Hostname",
216                                      "The hostname of the map server",
217                                      "<no-set>" /* default value */,
218                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
219         g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
220
221         pspec = g_param_spec_string ("url",
222                                      "URL",
223                                      "The template of the tiles' URL",
224                                      "<no-set>" /* default value */,
225                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
226         g_object_class_install_property (object_class, PROP_URL, pspec);
227
228         pspec = g_param_spec_string ("referer",
229                                      "Referer",
230                                      "The REFERER string to use in HTTP request",
231                                      NULL /* default value */,
232                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
233         g_object_class_install_property (object_class, PROP_REFERER, pspec);
234         
235         pspec = g_param_spec_long ("follow-location",
236                                    "Follow location",
237                                "Specifies the number of retries to follow a redirect while downloading a page",
238                                0  /* minimum value */,
239                                G_MAXLONG /* maximum value */,
240                                0  /* default value */,
241                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
242         g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
243         
244         pspec = g_param_spec_boolean ("check-file-server-time",
245                                       "Check file server time",
246                                   "Age of current cache before redownloading tile",
247                                   FALSE  /* default value */,
248                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
249         g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
250
251         g_type_class_add_private (klass, sizeof (VikTmsMapSourcePrivate));
252         
253         object_class->finalize = vik_tms_map_source_finalize;
254 }
255
256 /* 1 << (x) is like a 2**(x) */
257 #define GZ(x) ((1<<(x)))
258
259 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),
260                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
261
262 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
263
264 static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
265 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
266
267 #define ERROR_MARGIN 0.01
268 static gint tms_zoom ( gdouble mpp ) {
269   gint i;
270   for ( i = 0; i < num_scales; i++ ) {
271     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
272       return i;
273     }
274   }
275   for ( i = 0; i < num_scales_neg; i++ ) {
276     if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
277       return -i;
278     }
279   }
280
281   return 255;
282 }
283
284 gboolean
285 _supports_download_only_new (VikMapSource *self)
286 {
287         g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), FALSE);
288         
289     VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
290         
291         return priv->options.check_file_server_time;
292 }
293
294 static gboolean
295 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
296 {
297   g_assert ( src->mode == VIK_COORD_LATLON );
298
299   if ( xzoom != yzoom )
300     return FALSE;
301
302   dest->scale = tms_zoom ( xzoom );
303   if ( dest->scale == 255 )
304     return FALSE;
305
306   /* Note : GZ(17) / xzoom / 2 = number of tile on Y axis */
307         g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
308           xzoom, yzoom, GZ(17) / xzoom / 2);
309   dest->x = floor((src->east_west + 180) / 180 * GZ(17) / xzoom / 2);
310   /* We should restore logic of viking:
311    * tile index on Y axis follow a screen logic (top -> down)
312    */
313   dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2);
314   dest->z = 0;
315   g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
316           src->east_west, src->north_south, dest->x, dest->y);
317   return TRUE;
318 }
319
320 static void
321 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
322 {
323   gdouble socalled_mpp;
324   if (src->scale >= 0)
325     socalled_mpp = GZ(src->scale);
326   else
327     socalled_mpp = 1.0/GZ(-src->scale);
328   dest->mode = VIK_COORD_LATLON;
329   dest->east_west = (src->x+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 180;
330   /* We should restore logic of viking:
331    * tile index on Y axis follow a screen logic (top -> down)
332    */
333   dest->north_south = -((src->y+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 90);
334   g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
335           src->x, src->y, dest->east_west, dest->north_south);
336 }
337
338 static gchar *
339 _get_uri( VikMapSourceDefault *self, MapCoord *src )
340 {
341         g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
342         
343     VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
344         gdouble socalled_mpp;
345         if (src->scale >= 0)
346                 socalled_mpp = GZ(src->scale);
347         else
348                 socalled_mpp = 1.0/GZ(-src->scale);
349         /* We should restore logic of viking:
350      * tile index on Y axis follow a screen logic (top -> down)
351      */
352
353         /* Note : nb tiles on Y axis */
354         gint nb_tiles = GZ(17 - src->scale - 1);
355
356         gchar *uri = g_strdup_printf (priv->url, 17 - src->scale - 1, src->x, nb_tiles - src->y - 1);
357         
358         return uri;
359
360
361 static gchar *
362 _get_hostname( VikMapSourceDefault *self )
363 {
364         g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
365         
366     VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
367         return g_strdup( priv->hostname );
368 }
369
370 static DownloadMapOptions *
371 _get_download_options( VikMapSourceDefault *self )
372 {
373         g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
374         
375         VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
376         return &(priv->options);
377 }
378
379 VikTmsMapSource *
380 vik_tms_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
381 {
382         return g_object_new(VIK_TYPE_TMS_MAP_SOURCE,
383                             "id", id, "label", label, "hostname", hostname, "url", url, NULL);
384 }