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