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"
56 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
57 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
59 static gboolean _is_direct_file_access (VikMapSource *self );
60 static gboolean _is_mbtiles (VikMapSource *self );
61 static gboolean _supports_download_only_new (VikMapSource *self );
63 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
64 static gchar *_get_hostname( VikMapSourceDefault *self );
65 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
67 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
68 struct _VikSlippyMapSourcePrivate
72 DownloadMapOptions options;
73 gboolean is_direct_file_access;
75 // Mainly for ARCGIS Tile Server URL Layout // http://help.arcgis.com/EN/arcgisserver/10.0/apis/rest/tile.html
79 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
90 PROP_CHECK_FILE_SERVER_TIME,
92 PROP_IS_DIRECT_FILE_ACCESS,
97 G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
100 vik_slippy_map_source_init (VikSlippyMapSource *self)
102 /* initialize the object here */
103 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
105 priv->hostname = NULL;
107 priv->options.referer = NULL;
108 priv->options.follow_location = 0;
109 priv->options.check_file = a_check_map_file;
110 priv->options.check_file_server_time = FALSE;
111 priv->options.use_etag = FALSE;
112 priv->is_direct_file_access = FALSE;
113 priv->is_mbtiles = FALSE;
114 priv->switch_xy = FALSE;
116 g_object_set (G_OBJECT (self),
119 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
124 vik_slippy_map_source_finalize (GObject *object)
126 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
127 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
129 g_free (priv->hostname);
130 priv->hostname = NULL;
133 g_free (priv->options.referer);
134 priv->options.referer = NULL;
136 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
140 vik_slippy_map_source_set_property (GObject *object,
145 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
146 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
151 g_free (priv->hostname);
152 priv->hostname = g_value_dup_string (value);
157 priv->url = g_value_dup_string (value);
161 g_free (priv->options.referer);
162 priv->options.referer = g_value_dup_string (value);
165 case PROP_FOLLOW_LOCATION:
166 priv->options.follow_location = g_value_get_long (value);
169 case PROP_CHECK_FILE_SERVER_TIME:
170 priv->options.check_file_server_time = g_value_get_boolean (value);
174 priv->options.use_etag = g_value_get_boolean (value);
177 case PROP_IS_DIRECT_FILE_ACCESS:
178 priv->is_direct_file_access = g_value_get_boolean (value);
181 case PROP_IS_MBTILES:
182 priv->is_mbtiles = g_value_get_boolean (value);
186 priv->switch_xy = g_value_get_boolean (value);
190 /* We don't have any other property... */
191 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
197 vik_slippy_map_source_get_property (GObject *object,
202 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
203 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
208 g_value_set_string (value, priv->hostname);
212 g_value_set_string (value, priv->url);
216 g_value_set_string (value, priv->options.referer);
219 case PROP_FOLLOW_LOCATION:
220 g_value_set_long (value, priv->options.follow_location);
223 case PROP_CHECK_FILE_SERVER_TIME:
224 g_value_set_boolean (value, priv->options.check_file_server_time);
228 g_value_set_boolean (value, priv->options.use_etag);
231 case PROP_IS_DIRECT_FILE_ACCESS:
232 g_value_set_boolean (value, priv->is_direct_file_access);
235 case PROP_IS_MBTILES:
236 g_value_set_boolean (value, priv->is_mbtiles);
240 g_value_set_boolean (value, priv->switch_xy);
244 /* We don't have any other property... */
245 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
251 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
253 GObjectClass* object_class = G_OBJECT_CLASS (klass);
254 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
255 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
256 GParamSpec *pspec = NULL;
258 object_class->set_property = vik_slippy_map_source_set_property;
259 object_class->get_property = vik_slippy_map_source_get_property;
261 /* Overiding methods */
262 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
263 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
264 grandparent_class->is_direct_file_access = _is_direct_file_access;
265 grandparent_class->is_mbtiles = _is_mbtiles;
266 grandparent_class->supports_download_only_new = _supports_download_only_new;
268 parent_class->get_uri = _get_uri;
269 parent_class->get_hostname = _get_hostname;
270 parent_class->get_download_options = _get_download_options;
272 pspec = g_param_spec_string ("hostname",
274 "The hostname of the map server",
275 "<no-set>" /* default value */,
277 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
279 pspec = g_param_spec_string ("url",
281 "The template of the tiles' URL",
282 "<no-set>" /* default value */,
284 g_object_class_install_property (object_class, PROP_URL, pspec);
286 pspec = g_param_spec_string ("referer",
288 "The REFERER string to use in HTTP request",
289 NULL /* default value */,
290 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
291 g_object_class_install_property (object_class, PROP_REFERER, pspec);
293 pspec = g_param_spec_long ("follow-location",
295 "Specifies the number of retries to follow a redirect while downloading a page",
296 0 /* minimum value */,
297 G_MAXLONG /* maximum value */,
298 0 /* default value */,
299 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
300 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
302 pspec = g_param_spec_boolean ("check-file-server-time",
303 "Check file server time",
304 "Age of current cache before redownloading tile",
305 FALSE /* default value */,
306 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
307 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
309 pspec = g_param_spec_boolean ("use-etag",
310 "Use etag values with server",
311 "Store etag in a file, and send it to server to check if we have the latest file",
312 FALSE /* default value */,
313 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
314 g_object_class_install_property (object_class, PROP_USE_ETAG, pspec);
316 pspec = g_param_spec_boolean ("use-direct-file-access",
317 "Use direct file access",
318 "Use direct file access to OSM like tile images - no need for a webservice",
319 FALSE /* default value */,
320 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
321 g_object_class_install_property (object_class, PROP_IS_DIRECT_FILE_ACCESS, pspec);
323 pspec = g_param_spec_boolean ("is-mbtiles",
324 "Is an SQL MBTiles File",
325 "Use an SQL MBTiles File for the tileset - no need for a webservice",
326 FALSE /* default value */,
327 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
328 g_object_class_install_property (object_class, PROP_IS_MBTILES, pspec);
330 pspec = g_param_spec_boolean ("switch-xy",
331 "Switch the order of x,y components in the URL",
332 "Switch the order of x,y components in the URL (such as used by ARCGIS Tile Server",
333 FALSE /* default value */,
334 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
335 g_object_class_install_property (object_class, PROP_SWITCH_XY, pspec);
337 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
339 object_class->finalize = vik_slippy_map_source_finalize;
343 _is_direct_file_access (VikMapSource *self)
345 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
347 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
349 return priv->is_direct_file_access;
353 _is_mbtiles (VikMapSource *self)
355 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
357 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
359 return priv->is_mbtiles;
363 _supports_download_only_new (VikMapSource *self)
365 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
367 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
369 return priv->options.check_file_server_time || priv->options.use_etag;
373 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
375 g_assert ( src->mode == VIK_COORD_LATLON );
377 if ( xzoom != yzoom )
380 dest->scale = map_utils_mpp_to_scale ( xzoom );
381 if ( dest->scale == 255 )
384 dest->x = (src->east_west + 180) / 360 * VIK_GZ(17) / xzoom;
385 dest->y = (180 - MERCLAT(src->north_south)) / 360 * VIK_GZ(17) / xzoom;
392 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
394 gdouble socalled_mpp;
396 socalled_mpp = VIK_GZ(src->scale);
398 socalled_mpp = 1.0/VIK_GZ(-src->scale);
399 dest->mode = VIK_COORD_LATLON;
400 dest->east_west = ((src->x+0.5) / VIK_GZ(17) * socalled_mpp * 360) - 180;
401 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / VIK_GZ(17) * socalled_mpp * 360));
405 _get_uri( VikMapSourceDefault *self, MapCoord *src )
407 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
409 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
412 if ( priv->switch_xy )
413 // 'ARC GIS' Tile Server layout ordering
414 uri = g_strdup_printf (priv->url, 17 - src->scale, src->y, src->x);
416 // (Default) Standard OSM Tile Server layout ordering
417 uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
423 _get_hostname( VikMapSourceDefault *self )
425 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
427 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
428 return g_strdup( priv->hostname );
431 static DownloadMapOptions *
432 _get_download_options( VikMapSourceDefault *self )
434 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
436 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
437 return &(priv->options);
441 vik_slippy_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
443 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
444 "id", id, "label", label, "hostname", hostname, "url", url, NULL);