1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2009, 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:vikslippymapsource
22 * @short_description: the class for SlippyMap oriented map sources
24 * The #VikSlippyMapSource class handles slippy map oriented map sources.
25 * The related service is tile oriented, à la Google.
27 * The tiles are in 'google spherical mercator', which is
28 * basically a mercator projection that assumes a spherical earth.
29 * http://docs.openlayers.org/library/spherical_mercator.html
31 * Such service is also a type of TMS (Tile Map Service) as defined in
33 * http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
34 * But take care that the Y axis is inverted, ie the origin is at top-left
36 * Following this specification, the protocol handled by this class
37 * follows the global-mercator profile.
39 * You can also find many interesting information on the OSM's wiki.
40 * http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
41 * http://wiki.openstreetmap.org/wiki/Setting_up_TMS
53 #include "vikslippymapsource.h"
55 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
56 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
57 static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
58 static void * _download_handle_init ( VikMapSource *self);
59 static void _download_handle_cleanup ( VikMapSource *self, void *handle);
60 static gboolean _supports_download_only_new (VikMapSource *self );
62 static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
63 static gchar *_get_hostname( VikSlippyMapSource *self );
64 static DownloadMapOptions *_get_download_options( VikSlippyMapSource *self );
66 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
67 struct _VikSlippyMapSourcePrivate
71 DownloadMapOptions options;
74 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
85 PROP_CHECK_FILE_SERVER_TIME,
89 G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
92 vik_slippy_map_source_init (VikSlippyMapSource *self)
94 /* initialize the object here */
95 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
97 priv->hostname = NULL;
99 priv->options.referer = NULL;
100 priv->options.follow_location = 0;
101 priv->options.check_file = a_check_map_file;
102 priv->options.check_file_server_time = FALSE;
104 g_object_set (G_OBJECT (self),
107 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
112 vik_slippy_map_source_finalize (GObject *object)
114 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
115 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
117 g_free (priv->hostname);
118 priv->hostname = NULL;
121 g_free (priv->options.referer);
122 priv->options.referer = NULL;
124 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
128 vik_slippy_map_source_set_property (GObject *object,
133 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
134 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
139 g_free (priv->hostname);
140 priv->hostname = g_value_dup_string (value);
145 priv->url = g_value_dup_string (value);
149 g_free (priv->options.referer);
150 priv->options.referer = g_value_dup_string (value);
153 case PROP_FOLLOW_LOCATION:
154 priv->options.follow_location = g_value_get_long (value);
157 case PROP_CHECK_FILE_SERVER_TIME:
158 priv->options.check_file_server_time = g_value_get_boolean (value);
162 priv->options.use_etag = g_value_get_boolean (value);
166 /* We don't have any other property... */
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
173 vik_slippy_map_source_get_property (GObject *object,
178 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
179 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
184 g_value_set_string (value, priv->hostname);
188 g_value_set_string (value, priv->url);
192 g_value_set_string (value, priv->options.referer);
195 case PROP_FOLLOW_LOCATION:
196 g_value_set_long (value, priv->options.follow_location);
199 case PROP_CHECK_FILE_SERVER_TIME:
200 g_value_set_boolean (value, priv->options.check_file_server_time);
204 g_value_set_boolean (value, priv->options.use_etag);
208 /* We don't have any other property... */
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
215 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
217 GObjectClass* object_class = G_OBJECT_CLASS (klass);
218 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
219 GParamSpec *pspec = NULL;
221 object_class->set_property = vik_slippy_map_source_set_property;
222 object_class->get_property = vik_slippy_map_source_get_property;
224 /* Overiding methods */
225 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
226 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
227 parent_class->download = _download;
228 parent_class->download_handle_init = _download_handle_init;
229 parent_class->download_handle_cleanup = _download_handle_cleanup;
230 parent_class->supports_download_only_new = _supports_download_only_new;
232 /* Default implementation of methods */
233 klass->get_uri = _get_uri;
234 klass->get_hostname = _get_hostname;
235 klass->get_download_options = _get_download_options;
237 pspec = g_param_spec_string ("hostname",
239 "The hostname of the map server",
240 "<no-set>" /* default value */,
241 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
242 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
244 pspec = g_param_spec_string ("url",
246 "The template of the tiles' URL",
247 "<no-set>" /* default value */,
248 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
249 g_object_class_install_property (object_class, PROP_URL, pspec);
251 pspec = g_param_spec_string ("referer",
253 "The REFERER string to use in HTTP request",
254 NULL /* default value */,
255 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
256 g_object_class_install_property (object_class, PROP_REFERER, pspec);
258 pspec = g_param_spec_long ("follow-location",
260 "Specifies the number of retries to follow a redirect while downloading a page",
261 0 /* minimum value */,
262 G_MAXLONG /* maximum value */,
263 0 /* default value */,
264 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
265 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
267 pspec = g_param_spec_boolean ("check-file-server-time",
268 "Check file server time",
269 "Age of current cache before redownloading tile",
270 FALSE /* default value */,
271 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
272 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
274 pspec = g_param_spec_boolean ("use-etag",
275 "Use etag values with server",
276 "Store etag in a file, and send it to server to check if we have the latest file",
277 FALSE /* default value */,
278 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
279 g_object_class_install_property (object_class, PROP_USE_ETAG, pspec);
281 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
283 object_class->finalize = vik_slippy_map_source_finalize;
286 /* 1 << (x) is like a 2**(x) */
287 #define GZ(x) ((1<<(x)))
289 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),
290 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
292 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
294 static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
295 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
297 #define ERROR_MARGIN 0.01
298 static gint slippy_zoom ( gdouble mpp ) {
300 for ( i = 0; i < num_scales; i++ ) {
301 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
305 for ( i = 0; i < num_scales_neg; i++ ) {
306 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
315 vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
317 VikSlippyMapSourceClass *klass;
318 g_return_val_if_fail (self != NULL, 0);
319 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
320 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
322 g_return_val_if_fail (klass->get_uri != NULL, 0);
324 return (*klass->get_uri)(self, src);
328 vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
330 VikSlippyMapSourceClass *klass;
331 g_return_val_if_fail (self != NULL, 0);
332 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
333 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
335 g_return_val_if_fail (klass->get_hostname != NULL, 0);
337 return (*klass->get_hostname)(self);
341 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
343 VikSlippyMapSourceClass *klass;
344 g_return_val_if_fail (self != NULL, 0);
345 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
346 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
348 g_return_val_if_fail (klass->get_download_options != NULL, 0);
350 return (*klass->get_download_options)(self);
354 _supports_download_only_new (VikMapSource *self)
356 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
358 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
360 return priv->options.check_file_server_time || priv->options.use_etag;
363 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
365 g_assert ( src->mode == VIK_COORD_LATLON );
367 if ( xzoom != yzoom )
370 dest->scale = slippy_zoom ( xzoom );
371 if ( dest->scale == 255 )
374 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
375 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
382 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
384 gdouble socalled_mpp;
386 socalled_mpp = GZ(src->scale);
388 socalled_mpp = 1.0/GZ(-src->scale);
389 dest->mode = VIK_COORD_LATLON;
390 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
391 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
395 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
398 gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
399 gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
400 DownloadMapOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
401 res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
408 _download_handle_init ( VikMapSource *self )
410 return a_download_handle_init ();
415 _download_handle_cleanup ( VikMapSource *self, void *handle )
417 return a_download_handle_cleanup ( handle );
421 _get_uri( VikSlippyMapSource *self, MapCoord *src )
423 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
425 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
426 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
431 _get_hostname( VikSlippyMapSource *self )
433 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
435 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
436 return g_strdup( priv->hostname );
439 static DownloadMapOptions *
440 _get_download_options( VikSlippyMapSource *self )
442 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
444 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
445 return &(priv->options);
449 vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
451 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
452 "id", id, "label", label, "hostname", hostname, "url", url, NULL);