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 * http://wiki.osgeo.org/wiki/WMS_Tile_Caching
33 #include "vikwmscmapsource.h"
35 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
36 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
37 static gboolean _supports_download_only_new (VikMapSource *self );
39 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
40 static gchar *_get_hostname( VikMapSourceDefault *self );
41 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
43 typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
44 struct _VikWmscMapSourcePrivate
48 DownloadMapOptions options;
51 #define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
62 PROP_CHECK_FILE_SERVER_TIME,
65 G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
68 vik_wmsc_map_source_init (VikWmscMapSource *self)
70 /* initialize the object here */
71 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
73 priv->hostname = NULL;
75 priv->options.referer = NULL;
76 priv->options.follow_location = 0;
77 priv->options.check_file = a_check_map_file;
78 priv->options.check_file_server_time = FALSE;
80 g_object_set (G_OBJECT (self),
83 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
88 vik_wmsc_map_source_finalize (GObject *object)
90 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
91 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
93 g_free (priv->hostname);
94 priv->hostname = NULL;
97 g_free (priv->options.referer);
98 priv->options.referer = NULL;
100 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
104 vik_wmsc_map_source_set_property (GObject *object,
109 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
110 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
115 g_free (priv->hostname);
116 priv->hostname = g_value_dup_string (value);
121 priv->url = g_value_dup_string (value);
125 g_free (priv->options.referer);
126 priv->options.referer = g_value_dup_string (value);
129 case PROP_FOLLOW_LOCATION:
130 priv->options.follow_location = g_value_get_long (value);
133 case PROP_CHECK_FILE_SERVER_TIME:
134 priv->options.check_file_server_time = g_value_get_boolean (value);
138 /* We don't have any other property... */
139 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
145 vik_wmsc_map_source_get_property (GObject *object,
150 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
151 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
156 g_value_set_string (value, priv->hostname);
160 g_value_set_string (value, priv->url);
164 g_value_set_string (value, priv->options.referer);
167 case PROP_FOLLOW_LOCATION:
168 g_value_set_long (value, priv->options.follow_location);
171 case PROP_CHECK_FILE_SERVER_TIME:
172 g_value_set_boolean (value, priv->options.check_file_server_time);
176 /* We don't have any other property... */
177 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
183 vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
185 GObjectClass* object_class = G_OBJECT_CLASS (klass);
186 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
187 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
188 GParamSpec *pspec = NULL;
190 object_class->set_property = vik_wmsc_map_source_set_property;
191 object_class->get_property = vik_wmsc_map_source_get_property;
193 /* Overiding methods */
194 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
195 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
196 grandparent_class->supports_download_only_new = _supports_download_only_new;
198 parent_class->get_uri = _get_uri;
199 parent_class->get_hostname = _get_hostname;
200 parent_class->get_download_options = _get_download_options;
202 pspec = g_param_spec_string ("hostname",
204 "The hostname of the map server",
205 "<no-set>" /* default value */,
206 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
207 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
209 pspec = g_param_spec_string ("url",
211 "The template of the tiles' URL",
212 "<no-set>" /* default value */,
213 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
214 g_object_class_install_property (object_class, PROP_URL, pspec);
216 pspec = g_param_spec_string ("referer",
218 "The REFERER string to use in HTTP request",
219 NULL /* default value */,
220 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
221 g_object_class_install_property (object_class, PROP_REFERER, pspec);
223 pspec = g_param_spec_long ("follow-location",
225 "Specifies the number of retries to follow a redirect while downloading a page",
226 0 /* minimum value */,
227 G_MAXLONG /* maximum value */,
228 0 /* default value */,
229 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
230 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
232 pspec = g_param_spec_boolean ("check-file-server-time",
233 "Check file server time",
234 "Age of current cache before redownloading tile",
235 FALSE /* default value */,
236 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
237 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
239 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
241 object_class->finalize = vik_wmsc_map_source_finalize;
244 /* 1 << (x) is like a 2**(x) */
245 #define GZ(x) ((1<<x))
247 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),
248 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
250 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
252 static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
253 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
255 #define ERROR_MARGIN 0.01
256 static gint wmsc_zoom ( gdouble mpp ) {
258 for ( i = 0; i < num_scales; i++ ) {
259 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
263 for ( i = 0; i < num_scales_neg; i++ ) {
264 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
273 _supports_download_only_new (VikMapSource *self)
275 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
277 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
279 return priv->options.check_file_server_time;
283 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
285 g_assert ( src->mode == VIK_COORD_LATLON );
287 if ( xzoom != yzoom )
290 dest->scale = wmsc_zoom ( xzoom );
291 if ( dest->scale == 255 )
294 /* Note : GZ(17) / xzoom / 2 = number of tile on Y axis */
295 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
296 xzoom, yzoom, GZ(17) / xzoom / 2);
297 dest->x = floor((src->east_west + 180) / 180 * GZ(17) / xzoom / 2);
298 /* We should restore logic of viking:
299 * tile index on Y axis follow a screen logic (top -> down)
301 dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2);
303 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
304 src->east_west, src->north_south, dest->x, dest->y);
309 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
311 gdouble socalled_mpp;
313 socalled_mpp = GZ(src->scale);
315 socalled_mpp = 1.0/GZ(-src->scale);
316 dest->mode = VIK_COORD_LATLON;
317 dest->east_west = (src->x+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 180;
318 /* We should restore logic of viking:
319 * tile index on Y axis follow a screen logic (top -> down)
321 dest->north_south = -((src->y+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 90);
322 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
323 src->x, src->y, dest->east_west, dest->north_south);
327 _get_uri( VikMapSourceDefault *self, MapCoord *src )
329 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
331 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
332 gdouble socalled_mpp;
334 socalled_mpp = GZ(src->scale);
336 socalled_mpp = 1.0/GZ(-src->scale);
337 gdouble minx = (gdouble)src->x * 180 / GZ(17) * socalled_mpp * 2 - 180;
338 gdouble maxx = (gdouble)(src->x + 1) * 180 / GZ(17) * socalled_mpp * 2 - 180;
339 /* We should restore logic of viking:
340 * tile index on Y axis follow a screen logic (top -> down)
342 gdouble miny = -((gdouble)(src->y + 1) * 180 / GZ(17) * socalled_mpp * 2 - 90);
343 gdouble maxy = -((gdouble)(src->y) * 180 / GZ(17) * socalled_mpp * 2 - 90);
345 gchar sminx[G_ASCII_DTOSTR_BUF_SIZE];
346 gchar smaxx[G_ASCII_DTOSTR_BUF_SIZE];
347 gchar sminy[G_ASCII_DTOSTR_BUF_SIZE];
348 gchar smaxy[G_ASCII_DTOSTR_BUF_SIZE];
350 g_ascii_dtostr (sminx, G_ASCII_DTOSTR_BUF_SIZE, minx);
351 g_ascii_dtostr (smaxx, G_ASCII_DTOSTR_BUF_SIZE, maxx);
352 g_ascii_dtostr (sminy, G_ASCII_DTOSTR_BUF_SIZE, miny);
353 g_ascii_dtostr (smaxy, G_ASCII_DTOSTR_BUF_SIZE, maxy);
355 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
361 _get_hostname( VikMapSourceDefault *self )
363 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
365 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
366 return g_strdup( priv->hostname );
369 static DownloadMapOptions *
370 _get_download_options( VikMapSourceDefault *self )
372 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
374 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
375 return &(priv->options);
379 vik_wmsc_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
381 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
382 "id", id, "label", label, "hostname", hostname, "url", url, NULL);