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 2 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"
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 );
53 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
54 static gchar *_get_hostname( VikMapSourceDefault *self );
55 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
57 typedef struct _VikTmsMapSourcePrivate VikTmsMapSourcePrivate;
58 struct _VikTmsMapSourcePrivate
62 DownloadMapOptions options;
65 #define VIK_TMS_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_TMS_MAP_SOURCE, VikTmsMapSourcePrivate))
76 PROP_CHECK_FILE_SERVER_TIME,
79 G_DEFINE_TYPE (VikTmsMapSource, vik_tms_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
82 vik_tms_map_source_init (VikTmsMapSource *self)
84 /* initialize the object here */
85 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
87 priv->hostname = 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;
94 g_object_set (G_OBJECT (self),
97 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
102 vik_tms_map_source_finalize (GObject *object)
104 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
105 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
107 g_free (priv->hostname);
108 priv->hostname = NULL;
111 g_free (priv->options.referer);
112 priv->options.referer = NULL;
114 G_OBJECT_CLASS (vik_tms_map_source_parent_class)->finalize (object);
118 vik_tms_map_source_set_property (GObject *object,
123 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
124 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
129 g_free (priv->hostname);
130 priv->hostname = g_value_dup_string (value);
135 priv->url = g_value_dup_string (value);
139 g_free (priv->options.referer);
140 priv->options.referer = g_value_dup_string (value);
143 case PROP_FOLLOW_LOCATION:
144 priv->options.follow_location = g_value_get_long (value);
147 case PROP_CHECK_FILE_SERVER_TIME:
148 priv->options.check_file_server_time = g_value_get_boolean (value);
152 /* We don't have any other property... */
153 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 vik_tms_map_source_get_property (GObject *object,
164 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
165 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
170 g_value_set_string (value, priv->hostname);
174 g_value_set_string (value, priv->url);
178 g_value_set_string (value, priv->options.referer);
181 case PROP_FOLLOW_LOCATION:
182 g_value_set_long (value, priv->options.follow_location);
185 case PROP_CHECK_FILE_SERVER_TIME:
186 g_value_set_boolean (value, priv->options.check_file_server_time);
190 /* We don't have any other property... */
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
197 vik_tms_map_source_class_init (VikTmsMapSourceClass *klass)
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;
204 object_class->set_property = vik_tms_map_source_set_property;
205 object_class->get_property = vik_tms_map_source_get_property;
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;
213 parent_class->get_uri = _get_uri;
214 parent_class->get_hostname = _get_hostname;
215 parent_class->get_download_options = _get_download_options;
217 pspec = g_param_spec_string ("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);
224 pspec = g_param_spec_string ("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);
231 pspec = g_param_spec_string ("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);
238 pspec = g_param_spec_long ("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);
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);
254 g_type_class_add_private (klass, sizeof (VikTmsMapSourcePrivate));
256 object_class->finalize = vik_tms_map_source_finalize;
260 _is_direct_file_access ( VikMapSource *self )
266 _supports_download_only_new ( VikMapSource *self )
268 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), FALSE);
270 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
272 return priv->options.check_file_server_time;
276 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
278 g_assert ( src->mode == VIK_COORD_LATLON );
280 if ( xzoom != yzoom )
283 dest->scale = map_utils_mpp_to_scale ( xzoom );
284 if ( dest->scale == 255 )
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)
294 dest->y = floor((180 - (src->north_south + 90)) / 180 * VIK_GZ(17) / xzoom / 2);
296 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
297 src->east_west, src->north_south, dest->x, dest->y);
302 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
304 gdouble socalled_mpp;
306 socalled_mpp = VIK_GZ(src->scale);
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)
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);
320 _get_uri( VikMapSourceDefault *self, MapCoord *src )
322 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
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)
329 /* Note : nb tiles on Y axis */
330 gint nb_tiles = VIK_GZ(17 - src->scale - 1);
332 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale - 1, src->x, nb_tiles - src->y - 1);
338 _get_hostname( VikMapSourceDefault *self )
340 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
342 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
343 return g_strdup( priv->hostname );
346 static DownloadMapOptions *
347 _get_download_options( VikMapSourceDefault *self )
349 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
351 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
352 return &(priv->options);
356 vik_tms_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
358 return g_object_new(VIK_TYPE_TMS_MAP_SOURCE,
359 "id", id, "label", label, "hostname", hostname, "url", url, NULL);