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:vikwmscmapsource
22 * @short_description: the class for WMS/WMS-C oriented map sources
24 * The #VikWmscMapSource class handles WMS/WMS-C oriented map sources.
26 * http://wiki.osgeo.org/wiki/WMS_Tile_Caching
38 #include "vikwmscmapsource.h"
40 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
41 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
42 static gboolean _supports_download_only_new (VikMapSource *self );
44 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
45 static gchar *_get_hostname( VikMapSourceDefault *self );
46 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
48 typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
49 struct _VikWmscMapSourcePrivate
53 DownloadMapOptions options;
56 #define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
67 PROP_CHECK_FILE_SERVER_TIME,
70 G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
73 vik_wmsc_map_source_init (VikWmscMapSource *self)
75 /* initialize the object here */
76 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
78 priv->hostname = NULL;
80 priv->options.referer = NULL;
81 priv->options.follow_location = 0;
82 priv->options.check_file = a_check_map_file;
83 priv->options.check_file_server_time = FALSE;
85 g_object_set (G_OBJECT (self),
88 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
93 vik_wmsc_map_source_finalize (GObject *object)
95 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
96 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
98 g_free (priv->hostname);
99 priv->hostname = NULL;
102 g_free (priv->options.referer);
103 priv->options.referer = NULL;
105 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
109 vik_wmsc_map_source_set_property (GObject *object,
114 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
115 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
120 g_free (priv->hostname);
121 priv->hostname = g_value_dup_string (value);
126 priv->url = g_value_dup_string (value);
130 g_free (priv->options.referer);
131 priv->options.referer = g_value_dup_string (value);
134 case PROP_FOLLOW_LOCATION:
135 priv->options.follow_location = g_value_get_long (value);
138 case PROP_CHECK_FILE_SERVER_TIME:
139 priv->options.check_file_server_time = g_value_get_boolean (value);
143 /* We don't have any other property... */
144 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
150 vik_wmsc_map_source_get_property (GObject *object,
155 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
156 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
161 g_value_set_string (value, priv->hostname);
165 g_value_set_string (value, priv->url);
169 g_value_set_string (value, priv->options.referer);
172 case PROP_FOLLOW_LOCATION:
173 g_value_set_long (value, priv->options.follow_location);
176 case PROP_CHECK_FILE_SERVER_TIME:
177 g_value_set_boolean (value, priv->options.check_file_server_time);
181 /* We don't have any other property... */
182 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
188 vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
190 GObjectClass* object_class = G_OBJECT_CLASS (klass);
191 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
192 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
193 GParamSpec *pspec = NULL;
195 object_class->set_property = vik_wmsc_map_source_set_property;
196 object_class->get_property = vik_wmsc_map_source_get_property;
198 /* Overiding methods */
199 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
200 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
201 grandparent_class->supports_download_only_new = _supports_download_only_new;
203 parent_class->get_uri = _get_uri;
204 parent_class->get_hostname = _get_hostname;
205 parent_class->get_download_options = _get_download_options;
207 pspec = g_param_spec_string ("hostname",
209 "The hostname of the map server",
210 "<no-set>" /* default value */,
211 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
212 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
214 pspec = g_param_spec_string ("url",
216 "The template of the tiles' URL",
217 "<no-set>" /* default value */,
218 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
219 g_object_class_install_property (object_class, PROP_URL, pspec);
221 pspec = g_param_spec_string ("referer",
223 "The REFERER string to use in HTTP request",
224 NULL /* default value */,
225 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
226 g_object_class_install_property (object_class, PROP_REFERER, pspec);
228 pspec = g_param_spec_long ("follow-location",
230 "Specifies the number of retries to follow a redirect while downloading a page",
231 0 /* minimum value */,
232 G_MAXLONG /* maximum value */,
233 0 /* default value */,
234 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
235 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
237 pspec = g_param_spec_boolean ("check-file-server-time",
238 "Check file server time",
239 "Age of current cache before redownloading tile",
240 FALSE /* default value */,
241 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
242 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
244 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
246 object_class->finalize = vik_wmsc_map_source_finalize;
249 /* 1 << (x) is like a 2**(x) */
250 #define GZ(x) ((1<<x))
252 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),
253 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
255 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
257 static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
258 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
260 #define ERROR_MARGIN 0.01
261 static gint wmsc_zoom ( gdouble mpp ) {
263 for ( i = 0; i < num_scales; i++ ) {
264 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
268 for ( i = 0; i < num_scales_neg; i++ ) {
269 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
278 _supports_download_only_new (VikMapSource *self)
280 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
282 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
284 return priv->options.check_file_server_time;
288 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
290 g_assert ( src->mode == VIK_COORD_LATLON );
292 if ( xzoom != yzoom )
295 dest->scale = wmsc_zoom ( xzoom );
296 if ( dest->scale == 255 )
299 /* Note : GZ(17) / xzoom / 2 = number of tile on Y axis */
300 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
301 xzoom, yzoom, GZ(17) / xzoom / 2);
302 dest->x = floor((src->east_west + 180) / 180 * GZ(17) / xzoom / 2);
303 /* We should restore logic of viking:
304 * tile index on Y axis follow a screen logic (top -> down)
306 dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2);
308 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
309 src->east_west, src->north_south, dest->x, dest->y);
314 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
316 gdouble socalled_mpp;
318 socalled_mpp = GZ(src->scale);
320 socalled_mpp = 1.0/GZ(-src->scale);
321 dest->mode = VIK_COORD_LATLON;
322 dest->east_west = (src->x+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 180;
323 /* We should restore logic of viking:
324 * tile index on Y axis follow a screen logic (top -> down)
326 dest->north_south = -((src->y+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 90);
327 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
328 src->x, src->y, dest->east_west, dest->north_south);
332 _get_uri( VikMapSourceDefault *self, MapCoord *src )
334 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
336 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
337 gdouble socalled_mpp;
339 socalled_mpp = GZ(src->scale);
341 socalled_mpp = 1.0/GZ(-src->scale);
342 gdouble minx = (gdouble)src->x * 180 / GZ(17) * socalled_mpp * 2 - 180;
343 gdouble maxx = (gdouble)(src->x + 1) * 180 / GZ(17) * socalled_mpp * 2 - 180;
344 /* We should restore logic of viking:
345 * tile index on Y axis follow a screen logic (top -> down)
347 gdouble miny = -((gdouble)(src->y + 1) * 180 / GZ(17) * socalled_mpp * 2 - 90);
348 gdouble maxy = -((gdouble)(src->y) * 180 / GZ(17) * socalled_mpp * 2 - 90);
350 gchar sminx[G_ASCII_DTOSTR_BUF_SIZE];
351 gchar smaxx[G_ASCII_DTOSTR_BUF_SIZE];
352 gchar sminy[G_ASCII_DTOSTR_BUF_SIZE];
353 gchar smaxy[G_ASCII_DTOSTR_BUF_SIZE];
355 g_ascii_dtostr (sminx, G_ASCII_DTOSTR_BUF_SIZE, minx);
356 g_ascii_dtostr (smaxx, G_ASCII_DTOSTR_BUF_SIZE, maxx);
357 g_ascii_dtostr (sminy, G_ASCII_DTOSTR_BUF_SIZE, miny);
358 g_ascii_dtostr (smaxy, G_ASCII_DTOSTR_BUF_SIZE, maxy);
360 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
366 _get_hostname( VikMapSourceDefault *self )
368 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
370 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
371 return g_strdup( priv->hostname );
374 static DownloadMapOptions *
375 _get_download_options( VikMapSourceDefault *self )
377 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
379 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
380 return &(priv->options);
384 vik_wmsc_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
386 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
387 "id", id, "label", label, "hostname", hostname, "url", url, NULL);