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