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"
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 );
46 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
47 static gchar *_get_hostname( VikMapSourceDefault *self );
48 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
50 typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
51 struct _VikWmscMapSourcePrivate
55 DownloadMapOptions options;
58 #define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
69 PROP_CHECK_FILE_SERVER_TIME,
72 G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
75 vik_wmsc_map_source_init (VikWmscMapSource *self)
77 /* initialize the object here */
78 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
80 priv->hostname = NULL;
82 priv->options.referer = NULL;
83 priv->options.follow_location = 0;
84 priv->options.check_file = a_check_map_file;
85 priv->options.check_file_server_time = FALSE;
87 g_object_set (G_OBJECT (self),
90 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
95 vik_wmsc_map_source_finalize (GObject *object)
97 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
98 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
100 g_free (priv->hostname);
101 priv->hostname = NULL;
104 g_free (priv->options.referer);
105 priv->options.referer = NULL;
107 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
111 vik_wmsc_map_source_set_property (GObject *object,
116 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
117 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
122 g_free (priv->hostname);
123 priv->hostname = g_value_dup_string (value);
128 priv->url = g_value_dup_string (value);
132 g_free (priv->options.referer);
133 priv->options.referer = g_value_dup_string (value);
136 case PROP_FOLLOW_LOCATION:
137 priv->options.follow_location = g_value_get_long (value);
140 case PROP_CHECK_FILE_SERVER_TIME:
141 priv->options.check_file_server_time = g_value_get_boolean (value);
145 /* We don't have any other property... */
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
152 vik_wmsc_map_source_get_property (GObject *object,
157 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
158 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
163 g_value_set_string (value, priv->hostname);
167 g_value_set_string (value, priv->url);
171 g_value_set_string (value, priv->options.referer);
174 case PROP_FOLLOW_LOCATION:
175 g_value_set_long (value, priv->options.follow_location);
178 case PROP_CHECK_FILE_SERVER_TIME:
179 g_value_set_boolean (value, priv->options.check_file_server_time);
183 /* We don't have any other property... */
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
190 vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
192 GObjectClass* object_class = G_OBJECT_CLASS (klass);
193 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
194 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
195 GParamSpec *pspec = NULL;
197 object_class->set_property = vik_wmsc_map_source_set_property;
198 object_class->get_property = vik_wmsc_map_source_get_property;
200 /* Overiding methods */
201 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
202 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
203 grandparent_class->supports_download_only_new = _supports_download_only_new;
204 grandparent_class->is_direct_file_access = _is_direct_file_access;
206 parent_class->get_uri = _get_uri;
207 parent_class->get_hostname = _get_hostname;
208 parent_class->get_download_options = _get_download_options;
210 pspec = g_param_spec_string ("hostname",
212 "The hostname of the map server",
213 "<no-set>" /* default value */,
214 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
215 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
217 pspec = g_param_spec_string ("url",
219 "The template of the tiles' URL",
220 "<no-set>" /* default value */,
221 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
222 g_object_class_install_property (object_class, PROP_URL, pspec);
224 pspec = g_param_spec_string ("referer",
226 "The REFERER string to use in HTTP request",
227 NULL /* default value */,
228 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
229 g_object_class_install_property (object_class, PROP_REFERER, pspec);
231 pspec = g_param_spec_long ("follow-location",
233 "Specifies the number of retries to follow a redirect while downloading a page",
234 0 /* minimum value */,
235 G_MAXLONG /* maximum value */,
236 0 /* default value */,
237 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
238 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
240 pspec = g_param_spec_boolean ("check-file-server-time",
241 "Check file server time",
242 "Age of current cache before redownloading tile",
243 FALSE /* default value */,
244 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
245 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
247 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
249 object_class->finalize = vik_wmsc_map_source_finalize;
253 _supports_download_only_new ( VikMapSource *self )
255 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
257 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
259 return priv->options.check_file_server_time;
263 _is_direct_file_access ( VikMapSource *self )
269 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
271 g_assert ( src->mode == VIK_COORD_LATLON );
273 if ( xzoom != yzoom )
276 dest->scale = map_utils_mpp_to_scale ( xzoom );
277 if ( dest->scale == 255 )
280 /* Note : VIK_GZ(17) / xzoom / 2 = number of tile on Y axis */
281 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
282 xzoom, yzoom, VIK_GZ(17) / xzoom / 2);
283 dest->x = floor((src->east_west + 180) / 180 * VIK_GZ(17) / xzoom / 2);
284 /* We should restore logic of viking:
285 * tile index on Y axis follow a screen logic (top -> down)
287 dest->y = floor((180 - (src->north_south + 90)) / 180 * VIK_GZ(17) / xzoom / 2);
289 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
290 src->east_west, src->north_south, dest->x, dest->y);
295 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
297 gdouble socalled_mpp;
299 socalled_mpp = VIK_GZ(src->scale);
301 socalled_mpp = 1.0/VIK_GZ(-src->scale);
302 dest->mode = VIK_COORD_LATLON;
303 dest->east_west = (src->x+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
304 /* We should restore logic of viking:
305 * tile index on Y axis follow a screen logic (top -> down)
307 dest->north_south = -((src->y+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
308 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
309 src->x, src->y, dest->east_west, dest->north_south);
313 _get_uri( VikMapSourceDefault *self, MapCoord *src )
315 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
317 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
318 gdouble socalled_mpp;
320 socalled_mpp = VIK_GZ(src->scale);
322 socalled_mpp = 1.0/VIK_GZ(-src->scale);
323 gdouble minx = (gdouble)src->x * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
324 gdouble maxx = (gdouble)(src->x + 1) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
325 /* We should restore logic of viking:
326 * tile index on Y axis follow a screen logic (top -> down)
328 gdouble miny = -((gdouble)(src->y + 1) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
329 gdouble maxy = -((gdouble)(src->y) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
331 gchar sminx[G_ASCII_DTOSTR_BUF_SIZE];
332 gchar smaxx[G_ASCII_DTOSTR_BUF_SIZE];
333 gchar sminy[G_ASCII_DTOSTR_BUF_SIZE];
334 gchar smaxy[G_ASCII_DTOSTR_BUF_SIZE];
336 g_ascii_dtostr (sminx, G_ASCII_DTOSTR_BUF_SIZE, minx);
337 g_ascii_dtostr (smaxx, G_ASCII_DTOSTR_BUF_SIZE, maxx);
338 g_ascii_dtostr (sminy, G_ASCII_DTOSTR_BUF_SIZE, miny);
339 g_ascii_dtostr (smaxy, G_ASCII_DTOSTR_BUF_SIZE, maxy);
341 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
347 _get_hostname( VikMapSourceDefault *self )
349 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
351 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
352 return g_strdup( priv->hostname );
355 static DownloadMapOptions *
356 _get_download_options( VikMapSourceDefault *self )
358 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
360 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
361 return &(priv->options);
365 vik_wmsc_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
367 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
368 "id", id, "label", label, "hostname", hostname, "url", url, NULL);