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: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"
41 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
42 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
43 static gboolean _supports_download_only_new ( VikMapSource *self );
44 static gboolean _is_direct_file_access ( VikMapSource *self );
45 static gboolean _is_mbtiles ( VikMapSource *self );
47 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
48 static gchar *_get_hostname( VikMapSourceDefault *self );
49 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
51 typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
52 struct _VikWmscMapSourcePrivate
56 DownloadMapOptions options;
59 #define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
70 PROP_CHECK_FILE_SERVER_TIME,
73 G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
76 vik_wmsc_map_source_init (VikWmscMapSource *self)
78 /* initialize the object here */
79 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
81 priv->hostname = NULL;
83 priv->options.referer = NULL;
84 priv->options.follow_location = 0;
85 priv->options.check_file = a_check_map_file;
86 priv->options.check_file_server_time = FALSE;
88 g_object_set (G_OBJECT (self),
91 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
96 vik_wmsc_map_source_finalize (GObject *object)
98 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
99 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
101 g_free (priv->hostname);
102 priv->hostname = NULL;
105 g_free (priv->options.referer);
106 priv->options.referer = NULL;
108 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
112 vik_wmsc_map_source_set_property (GObject *object,
117 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
118 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
123 g_free (priv->hostname);
124 priv->hostname = g_value_dup_string (value);
129 priv->url = g_value_dup_string (value);
133 g_free (priv->options.referer);
134 priv->options.referer = g_value_dup_string (value);
137 case PROP_FOLLOW_LOCATION:
138 priv->options.follow_location = g_value_get_long (value);
141 case PROP_CHECK_FILE_SERVER_TIME:
142 priv->options.check_file_server_time = g_value_get_boolean (value);
146 /* We don't have any other property... */
147 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
153 vik_wmsc_map_source_get_property (GObject *object,
158 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
159 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
164 g_value_set_string (value, priv->hostname);
168 g_value_set_string (value, priv->url);
172 g_value_set_string (value, priv->options.referer);
175 case PROP_FOLLOW_LOCATION:
176 g_value_set_long (value, priv->options.follow_location);
179 case PROP_CHECK_FILE_SERVER_TIME:
180 g_value_set_boolean (value, priv->options.check_file_server_time);
184 /* We don't have any other property... */
185 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
191 vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
193 GObjectClass* object_class = G_OBJECT_CLASS (klass);
194 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
195 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
196 GParamSpec *pspec = NULL;
198 object_class->set_property = vik_wmsc_map_source_set_property;
199 object_class->get_property = vik_wmsc_map_source_get_property;
201 /* Overiding methods */
202 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
203 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
204 grandparent_class->supports_download_only_new = _supports_download_only_new;
205 grandparent_class->is_direct_file_access = _is_direct_file_access;
206 grandparent_class->is_mbtiles = _is_mbtiles;
208 parent_class->get_uri = _get_uri;
209 parent_class->get_hostname = _get_hostname;
210 parent_class->get_download_options = _get_download_options;
212 pspec = g_param_spec_string ("hostname",
214 "The hostname of the map server",
215 "<no-set>" /* default value */,
216 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
217 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
219 pspec = g_param_spec_string ("url",
221 "The template of the tiles' URL",
222 "<no-set>" /* default value */,
223 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
224 g_object_class_install_property (object_class, PROP_URL, pspec);
226 pspec = g_param_spec_string ("referer",
228 "The REFERER string to use in HTTP request",
229 NULL /* default value */,
230 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
231 g_object_class_install_property (object_class, PROP_REFERER, pspec);
233 pspec = g_param_spec_long ("follow-location",
235 "Specifies the number of retries to follow a redirect while downloading a page",
236 0 /* minimum value */,
237 G_MAXLONG /* maximum value */,
238 0 /* default value */,
239 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
240 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
242 pspec = g_param_spec_boolean ("check-file-server-time",
243 "Check file server time",
244 "Age of current cache before redownloading tile",
245 FALSE /* default value */,
246 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
247 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
249 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
251 object_class->finalize = vik_wmsc_map_source_finalize;
255 _supports_download_only_new ( VikMapSource *self )
257 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
259 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
261 return priv->options.check_file_server_time;
265 _is_direct_file_access ( VikMapSource *self )
271 _is_mbtiles ( VikMapSource *self )
277 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
279 g_assert ( src->mode == VIK_COORD_LATLON );
281 if ( xzoom != yzoom )
284 dest->scale = map_utils_mpp_to_scale ( xzoom );
285 if ( dest->scale == 255 )
288 /* Note : VIK_GZ(17) / xzoom / 2 = number of tile on Y axis */
289 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
290 xzoom, yzoom, VIK_GZ(17) / xzoom / 2);
291 dest->x = floor((src->east_west + 180) / 180 * VIK_GZ(17) / xzoom / 2);
292 /* We should restore logic of viking:
293 * tile index on Y axis follow a screen logic (top -> down)
295 dest->y = floor((180 - (src->north_south + 90)) / 180 * VIK_GZ(17) / xzoom / 2);
297 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
298 src->east_west, src->north_south, dest->x, dest->y);
303 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
305 gdouble socalled_mpp;
307 socalled_mpp = VIK_GZ(src->scale);
309 socalled_mpp = 1.0/VIK_GZ(-src->scale);
310 dest->mode = VIK_COORD_LATLON;
311 dest->east_west = (src->x+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
312 /* We should restore logic of viking:
313 * tile index on Y axis follow a screen logic (top -> down)
315 dest->north_south = -((src->y+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
316 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
317 src->x, src->y, dest->east_west, dest->north_south);
321 _get_uri( VikMapSourceDefault *self, MapCoord *src )
323 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
325 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
326 gdouble socalled_mpp;
328 socalled_mpp = VIK_GZ(src->scale);
330 socalled_mpp = 1.0/VIK_GZ(-src->scale);
331 gdouble minx = (gdouble)src->x * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
332 gdouble maxx = (gdouble)(src->x + 1) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
333 /* We should restore logic of viking:
334 * tile index on Y axis follow a screen logic (top -> down)
336 gdouble miny = -((gdouble)(src->y + 1) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
337 gdouble maxy = -((gdouble)(src->y) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
339 gchar sminx[G_ASCII_DTOSTR_BUF_SIZE];
340 gchar smaxx[G_ASCII_DTOSTR_BUF_SIZE];
341 gchar sminy[G_ASCII_DTOSTR_BUF_SIZE];
342 gchar smaxy[G_ASCII_DTOSTR_BUF_SIZE];
344 g_ascii_dtostr (sminx, G_ASCII_DTOSTR_BUF_SIZE, minx);
345 g_ascii_dtostr (smaxx, G_ASCII_DTOSTR_BUF_SIZE, maxx);
346 g_ascii_dtostr (sminy, G_ASCII_DTOSTR_BUF_SIZE, miny);
347 g_ascii_dtostr (smaxy, G_ASCII_DTOSTR_BUF_SIZE, maxy);
349 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
355 _get_hostname( VikMapSourceDefault *self )
357 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
359 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
360 return g_strdup( priv->hostname );
363 static DownloadMapOptions *
364 _get_download_options( VikMapSourceDefault *self )
366 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
368 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
369 return &(priv->options);
373 vik_wmsc_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
375 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
376 "id", id, "label", label, "hostname", hostname, "url", url, NULL);