1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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/>.
21 * SECTION:viktmsmapsource
22 * @short_description: the class for TMS oriented map sources
24 * The #VikTmsMapSource class handles TMS oriented map sources.
26 * The tiles are in 'equirectangular'.
27 * http://en.wikipedia.org/wiki/Equirectangular_projection
29 * Such service is also a type of TMS (Tile Map Service) as defined in
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.
45 #include "viktmsmapsource.h"
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 );
52 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
53 static gchar *_get_hostname( VikMapSourceDefault *self );
54 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
56 typedef struct _VikTmsMapSourcePrivate VikTmsMapSourcePrivate;
57 struct _VikTmsMapSourcePrivate
61 DownloadMapOptions options;
64 #define VIK_TMS_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_TMS_MAP_SOURCE, VikTmsMapSourcePrivate))
75 PROP_CHECK_FILE_SERVER_TIME,
78 G_DEFINE_TYPE (VikTmsMapSource, vik_tms_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
81 vik_tms_map_source_init (VikTmsMapSource *self)
83 /* initialize the object here */
84 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
86 priv->hostname = 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;
93 g_object_set (G_OBJECT (self),
96 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
101 vik_tms_map_source_finalize (GObject *object)
103 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
104 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
106 g_free (priv->hostname);
107 priv->hostname = NULL;
110 g_free (priv->options.referer);
111 priv->options.referer = NULL;
113 G_OBJECT_CLASS (vik_tms_map_source_parent_class)->finalize (object);
117 vik_tms_map_source_set_property (GObject *object,
122 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
123 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
128 g_free (priv->hostname);
129 priv->hostname = g_value_dup_string (value);
134 priv->url = g_value_dup_string (value);
138 g_free (priv->options.referer);
139 priv->options.referer = g_value_dup_string (value);
142 case PROP_FOLLOW_LOCATION:
143 priv->options.follow_location = g_value_get_long (value);
146 case PROP_CHECK_FILE_SERVER_TIME:
147 priv->options.check_file_server_time = g_value_get_boolean (value);
151 /* We don't have any other property... */
152 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
158 vik_tms_map_source_get_property (GObject *object,
163 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
164 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
169 g_value_set_string (value, priv->hostname);
173 g_value_set_string (value, priv->url);
177 g_value_set_string (value, priv->options.referer);
180 case PROP_FOLLOW_LOCATION:
181 g_value_set_long (value, priv->options.follow_location);
184 case PROP_CHECK_FILE_SERVER_TIME:
185 g_value_set_boolean (value, priv->options.check_file_server_time);
189 /* We don't have any other property... */
190 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196 vik_tms_map_source_class_init (VikTmsMapSourceClass *klass)
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;
203 object_class->set_property = vik_tms_map_source_set_property;
204 object_class->get_property = vik_tms_map_source_get_property;
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;
212 parent_class->get_uri = _get_uri;
213 parent_class->get_hostname = _get_hostname;
214 parent_class->get_download_options = _get_download_options;
216 pspec = g_param_spec_string ("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);
223 pspec = g_param_spec_string ("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);
230 pspec = g_param_spec_string ("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);
237 pspec = g_param_spec_long ("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);
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);
253 g_type_class_add_private (klass, sizeof (VikTmsMapSourcePrivate));
255 object_class->finalize = vik_tms_map_source_finalize;
258 /* 1 << (x) is like a 2**(x) */
259 #define GZ(x) ((1<<(x)))
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) };
264 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
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]));
269 #define ERROR_MARGIN 0.01
270 static gint tms_zoom ( gdouble mpp ) {
272 for ( i = 0; i < num_scales; i++ ) {
273 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
277 for ( i = 0; i < num_scales_neg; i++ ) {
278 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
287 _is_direct_file_access ( VikMapSource *self )
293 _supports_download_only_new ( VikMapSource *self )
295 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), FALSE);
297 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
299 return priv->options.check_file_server_time;
303 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
305 g_assert ( src->mode == VIK_COORD_LATLON );
307 if ( xzoom != yzoom )
310 dest->scale = tms_zoom ( xzoom );
311 if ( dest->scale == 255 )
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)
321 dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2);
323 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
324 src->east_west, src->north_south, dest->x, dest->y);
329 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
331 gdouble socalled_mpp;
333 socalled_mpp = GZ(src->scale);
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)
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);
347 _get_uri( VikMapSourceDefault *self, MapCoord *src )
349 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
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)
356 /* Note : nb tiles on Y axis */
357 gint nb_tiles = GZ(17 - src->scale - 1);
359 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale - 1, src->x, nb_tiles - src->y - 1);
365 _get_hostname( VikMapSourceDefault *self )
367 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
369 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
370 return g_strdup( priv->hostname );
373 static DownloadMapOptions *
374 _get_download_options( VikMapSourceDefault *self )
376 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
378 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
379 return &(priv->options);
383 vik_tms_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
385 return g_object_new(VIK_TYPE_TMS_MAP_SOURCE,
386 "id", id, "label", label, "hostname", hostname, "url", url, NULL);