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 );
52 static gboolean _is_mbtiles ( VikMapSource *self );
54 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
55 static gchar *_get_hostname( VikMapSourceDefault *self );
56 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
58 typedef struct _VikTmsMapSourcePrivate VikTmsMapSourcePrivate;
59 struct _VikTmsMapSourcePrivate
63 DownloadMapOptions options;
66 #define VIK_TMS_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_TMS_MAP_SOURCE, VikTmsMapSourcePrivate))
77 PROP_CHECK_FILE_SERVER_TIME,
80 G_DEFINE_TYPE (VikTmsMapSource, vik_tms_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
83 vik_tms_map_source_init (VikTmsMapSource *self)
85 /* initialize the object here */
86 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
88 priv->hostname = NULL;
90 priv->options.referer = NULL;
91 priv->options.follow_location = 0;
92 priv->options.check_file = a_check_map_file;
93 priv->options.check_file_server_time = FALSE;
95 g_object_set (G_OBJECT (self),
98 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
103 vik_tms_map_source_finalize (GObject *object)
105 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
106 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
108 g_free (priv->hostname);
109 priv->hostname = NULL;
112 g_free (priv->options.referer);
113 priv->options.referer = NULL;
115 G_OBJECT_CLASS (vik_tms_map_source_parent_class)->finalize (object);
119 vik_tms_map_source_set_property (GObject *object,
124 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
125 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
130 g_free (priv->hostname);
131 priv->hostname = g_value_dup_string (value);
136 priv->url = g_value_dup_string (value);
140 g_free (priv->options.referer);
141 priv->options.referer = g_value_dup_string (value);
144 case PROP_FOLLOW_LOCATION:
145 priv->options.follow_location = g_value_get_long (value);
148 case PROP_CHECK_FILE_SERVER_TIME:
149 priv->options.check_file_server_time = g_value_get_boolean (value);
153 /* We don't have any other property... */
154 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
160 vik_tms_map_source_get_property (GObject *object,
165 VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object);
166 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self);
171 g_value_set_string (value, priv->hostname);
175 g_value_set_string (value, priv->url);
179 g_value_set_string (value, priv->options.referer);
182 case PROP_FOLLOW_LOCATION:
183 g_value_set_long (value, priv->options.follow_location);
186 case PROP_CHECK_FILE_SERVER_TIME:
187 g_value_set_boolean (value, priv->options.check_file_server_time);
191 /* We don't have any other property... */
192 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
198 vik_tms_map_source_class_init (VikTmsMapSourceClass *klass)
200 GObjectClass* object_class = G_OBJECT_CLASS (klass);
201 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
202 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
203 GParamSpec *pspec = NULL;
205 object_class->set_property = vik_tms_map_source_set_property;
206 object_class->get_property = vik_tms_map_source_get_property;
208 /* Overiding methods */
209 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
210 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
211 grandparent_class->supports_download_only_new = _supports_download_only_new;
212 grandparent_class->is_direct_file_access = _is_direct_file_access;
213 grandparent_class->is_mbtiles = _is_mbtiles;
215 parent_class->get_uri = _get_uri;
216 parent_class->get_hostname = _get_hostname;
217 parent_class->get_download_options = _get_download_options;
219 pspec = g_param_spec_string ("hostname",
221 "The hostname of the map server",
222 "<no-set>" /* default value */,
223 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
224 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
226 pspec = g_param_spec_string ("url",
228 "The template of the tiles' URL",
229 "<no-set>" /* default value */,
230 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
231 g_object_class_install_property (object_class, PROP_URL, pspec);
233 pspec = g_param_spec_string ("referer",
235 "The REFERER string to use in HTTP request",
236 NULL /* default value */,
237 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
238 g_object_class_install_property (object_class, PROP_REFERER, pspec);
240 pspec = g_param_spec_long ("follow-location",
242 "Specifies the number of retries to follow a redirect while downloading a page",
243 0 /* minimum value */,
244 G_MAXLONG /* maximum value */,
245 0 /* default value */,
246 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
247 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
249 pspec = g_param_spec_boolean ("check-file-server-time",
250 "Check file server time",
251 "Age of current cache before redownloading tile",
252 FALSE /* default value */,
253 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
254 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
256 g_type_class_add_private (klass, sizeof (VikTmsMapSourcePrivate));
258 object_class->finalize = vik_tms_map_source_finalize;
262 _is_direct_file_access ( VikMapSource *self )
268 _is_mbtiles ( VikMapSource *self )
274 _supports_download_only_new ( VikMapSource *self )
276 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), FALSE);
278 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
280 return priv->options.check_file_server_time;
284 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
286 g_assert ( src->mode == VIK_COORD_LATLON );
288 if ( xzoom != yzoom )
291 dest->scale = map_utils_mpp_to_scale ( xzoom );
292 if ( dest->scale == 255 )
295 /* Note : VIK_GZ(17) / xzoom / 2 = number of tile on Y axis */
296 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
297 xzoom, yzoom, VIK_GZ(17) / xzoom / 2);
298 dest->x = floor((src->east_west + 180) / 180 * VIK_GZ(17) / xzoom / 2);
299 /* We should restore logic of viking:
300 * tile index on Y axis follow a screen logic (top -> down)
302 dest->y = floor((180 - (src->north_south + 90)) / 180 * VIK_GZ(17) / xzoom / 2);
304 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
305 src->east_west, src->north_south, dest->x, dest->y);
310 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
312 gdouble socalled_mpp;
314 socalled_mpp = VIK_GZ(src->scale);
316 socalled_mpp = 1.0/VIK_GZ(-src->scale);
317 dest->mode = VIK_COORD_LATLON;
318 dest->east_west = (src->x+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
319 /* We should restore logic of viking:
320 * tile index on Y axis follow a screen logic (top -> down)
322 dest->north_south = -((src->y+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
323 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
324 src->x, src->y, dest->east_west, dest->north_south);
328 _get_uri( VikMapSourceDefault *self, MapCoord *src )
330 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
332 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
333 /* We should restore logic of viking:
334 * tile index on Y axis follow a screen logic (top -> down)
337 /* Note : nb tiles on Y axis */
338 gint nb_tiles = VIK_GZ(17 - src->scale - 1);
340 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale - 1, src->x, nb_tiles - src->y - 1);
346 _get_hostname( VikMapSourceDefault *self )
348 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
350 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
351 return g_strdup( priv->hostname );
354 static DownloadMapOptions *
355 _get_download_options( VikMapSourceDefault *self )
357 g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL);
359 VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self);
360 return &(priv->options);
364 vik_tms_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
366 return g_object_new(VIK_TYPE_TMS_MAP_SOURCE,
367 "id", id, "label", label, "hostname", hostname, "url", url, NULL);