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