]> git.street.me.uk Git - andy/viking.git/blame - src/vikwmscmapsource.c
Prevent the program grinding to a halt if trying to deal with thousands of tiles
[andy/viking.git] / src / vikwmscmapsource.c
CommitLineData
5b675792
GB
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking
4 * Copyright (C) 2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
20 /**
aa69d106
GB
21 * SECTION:vikwmscmapsource
22 * @short_description: the class for WMS/WMS-C oriented map sources
23 *
24 * The #VikWmscMapSource class handles WMS/WMS-C oriented map sources.
25 *
5b675792
GB
26 * http://wiki.osgeo.org/wiki/WMS_Tile_Caching
27 */
28
29#ifdef HAVE_CONFIG_H
30#include "config.h"
31#endif
32
33#ifdef HAVE_MATH_H
34#include <math.h>
35#endif
36
37#include "globals.h"
38#include "vikwmscmapsource.h"
e1dde2a6 39#include "maputils.h"
5b675792
GB
40
41static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
42static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
a748af37 43static gboolean _supports_download_only_new ( VikMapSource *self );
a0262d5b 44static gboolean _is_direct_file_access ( VikMapSource *self );
5b675792
GB
45
46static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
47static gchar *_get_hostname( VikMapSourceDefault *self );
48static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
49
50typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
51struct _VikWmscMapSourcePrivate
52{
53 gchar *hostname;
54 gchar *url;
55 DownloadMapOptions options;
56};
57
58#define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
59
60/* properties */
61enum
62{
63 PROP_0,
64
65 PROP_HOSTNAME,
66 PROP_URL,
67 PROP_REFERER,
68 PROP_FOLLOW_LOCATION,
69 PROP_CHECK_FILE_SERVER_TIME,
70};
71
72G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
73
74static void
75vik_wmsc_map_source_init (VikWmscMapSource *self)
76{
77 /* initialize the object here */
78 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
79
80 priv->hostname = NULL;
81 priv->url = 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;
86
87 g_object_set (G_OBJECT (self),
88 "tilesize-x", 256,
89 "tilesize-y", 256,
90 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
91 NULL);
92}
93
94static void
95vik_wmsc_map_source_finalize (GObject *object)
96{
97 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
98 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
99
100 g_free (priv->hostname);
101 priv->hostname = NULL;
102 g_free (priv->url);
103 priv->url = NULL;
104 g_free (priv->options.referer);
105 priv->options.referer = NULL;
106
107 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
108}
109
110static void
111vik_wmsc_map_source_set_property (GObject *object,
112 guint property_id,
113 const GValue *value,
114 GParamSpec *pspec)
115{
116 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
117 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
118
119 switch (property_id)
120 {
121 case PROP_HOSTNAME:
122 g_free (priv->hostname);
123 priv->hostname = g_value_dup_string (value);
124 break;
125
126 case PROP_URL:
127 g_free (priv->url);
128 priv->url = g_value_dup_string (value);
129 break;
130
131 case PROP_REFERER:
132 g_free (priv->options.referer);
133 priv->options.referer = g_value_dup_string (value);
134 break;
135
136 case PROP_FOLLOW_LOCATION:
137 priv->options.follow_location = g_value_get_long (value);
138 break;
139
140 case PROP_CHECK_FILE_SERVER_TIME:
141 priv->options.check_file_server_time = g_value_get_boolean (value);
142 break;
143
144 default:
145 /* We don't have any other property... */
146 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
147 break;
148 }
149}
150
151static void
152vik_wmsc_map_source_get_property (GObject *object,
153 guint property_id,
154 GValue *value,
155 GParamSpec *pspec)
156{
157 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
158 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
159
160 switch (property_id)
161 {
162 case PROP_HOSTNAME:
163 g_value_set_string (value, priv->hostname);
164 break;
165
166 case PROP_URL:
167 g_value_set_string (value, priv->url);
168 break;
169
170 case PROP_REFERER:
171 g_value_set_string (value, priv->options.referer);
172 break;
173
174 case PROP_FOLLOW_LOCATION:
175 g_value_set_long (value, priv->options.follow_location);
176 break;
177
178 case PROP_CHECK_FILE_SERVER_TIME:
179 g_value_set_boolean (value, priv->options.check_file_server_time);
180 break;
181
182 default:
183 /* We don't have any other property... */
184 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
185 break;
186 }
187}
188
189static void
190vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
191{
192 GObjectClass* object_class = G_OBJECT_CLASS (klass);
a3319e11 193 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
5b675792
GB
194 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
195 GParamSpec *pspec = NULL;
196
197 object_class->set_property = vik_wmsc_map_source_set_property;
198 object_class->get_property = vik_wmsc_map_source_get_property;
199
200 /* Overiding methods */
a3319e11
RN
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;
a0262d5b 204 grandparent_class->is_direct_file_access = _is_direct_file_access;
5b675792
GB
205
206 parent_class->get_uri = _get_uri;
207 parent_class->get_hostname = _get_hostname;
208 parent_class->get_download_options = _get_download_options;
209
210 pspec = g_param_spec_string ("hostname",
211 "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);
216
217 pspec = g_param_spec_string ("url",
218 "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);
223
224 pspec = g_param_spec_string ("referer",
225 "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);
230
231 pspec = g_param_spec_long ("follow-location",
232 "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);
239
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);
246
247 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
248
249 object_class->finalize = vik_wmsc_map_source_finalize;
250}
251
a748af37
RN
252static gboolean
253_supports_download_only_new ( VikMapSource *self )
5b675792
GB
254{
255 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
256
257 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
258
259 return priv->options.check_file_server_time;
260}
261
a0262d5b
RN
262static gboolean
263_is_direct_file_access ( VikMapSource *self )
264{
265 return FALSE;
266}
267
5b675792
GB
268static gboolean
269_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
270{
271 g_assert ( src->mode == VIK_COORD_LATLON );
272
273 if ( xzoom != yzoom )
274 return FALSE;
275
e1dde2a6 276 dest->scale = map_utils_mpp_to_scale ( xzoom );
5b675792
GB
277 if ( dest->scale == 255 )
278 return FALSE;
279
e1dde2a6 280 /* Note : VIK_GZ(17) / xzoom / 2 = number of tile on Y axis */
5b675792 281 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
e1dde2a6
RN
282 xzoom, yzoom, VIK_GZ(17) / xzoom / 2);
283 dest->x = floor((src->east_west + 180) / 180 * VIK_GZ(17) / xzoom / 2);
5b675792
GB
284 /* We should restore logic of viking:
285 * tile index on Y axis follow a screen logic (top -> down)
286 */
e1dde2a6 287 dest->y = floor((180 - (src->north_south + 90)) / 180 * VIK_GZ(17) / xzoom / 2);
5b675792
GB
288 dest->z = 0;
289 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
290 src->east_west, src->north_south, dest->x, dest->y);
291 return TRUE;
292}
293
294static void
295_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
296{
297 gdouble socalled_mpp;
298 if (src->scale >= 0)
e1dde2a6 299 socalled_mpp = VIK_GZ(src->scale);
5b675792 300 else
e1dde2a6 301 socalled_mpp = 1.0/VIK_GZ(-src->scale);
5b675792 302 dest->mode = VIK_COORD_LATLON;
e1dde2a6 303 dest->east_west = (src->x+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 180;
5b675792
GB
304 /* We should restore logic of viking:
305 * tile index on Y axis follow a screen logic (top -> down)
306 */
e1dde2a6 307 dest->north_south = -((src->y+0.5) * 180 / VIK_GZ(17) * socalled_mpp * 2 - 90);
5b675792
GB
308 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
309 src->x, src->y, dest->east_west, dest->north_south);
310}
311
312static gchar *
313_get_uri( VikMapSourceDefault *self, MapCoord *src )
314{
315 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
316
317 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
318 gdouble socalled_mpp;
319 if (src->scale >= 0)
e1dde2a6 320 socalled_mpp = VIK_GZ(src->scale);
5b675792 321 else
e1dde2a6
RN
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;
5b675792
GB
325 /* We should restore logic of viking:
326 * tile index on Y axis follow a screen logic (top -> down)
327 */
e1dde2a6
RN
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);
5b675792
GB
330
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];
335
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);
340
341 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
342
343 return uri;
344}
345
346static gchar *
347_get_hostname( VikMapSourceDefault *self )
348{
349 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
350
351 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
352 return g_strdup( priv->hostname );
353}
354
355static DownloadMapOptions *
356_get_download_options( VikMapSourceDefault *self )
357{
358 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
359
360 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
361 return &(priv->options);
362}
363
364VikWmscMapSource *
365vik_wmsc_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
366{
367 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
368 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
369}