]> git.street.me.uk Git - andy/viking.git/blame_incremental - src/vikwmscmapsource.c
Fix non slippy maps from complaining about lack of function definition.
[andy/viking.git] / src / vikwmscmapsource.c
... / ...
CommitLineData
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 /**
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 *
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"
39
40static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
41static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
42static gboolean _supports_download_only_new (VikMapSource *self );
43static gboolean _is_direct_file_access ( VikMapSource *self );
44
45static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
46static gchar *_get_hostname( VikMapSourceDefault *self );
47static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
48
49typedef struct _VikWmscMapSourcePrivate VikWmscMapSourcePrivate;
50struct _VikWmscMapSourcePrivate
51{
52 gchar *hostname;
53 gchar *url;
54 DownloadMapOptions options;
55};
56
57#define VIK_WMSC_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_WMSC_MAP_SOURCE, VikWmscMapSourcePrivate))
58
59/* properties */
60enum
61{
62 PROP_0,
63
64 PROP_HOSTNAME,
65 PROP_URL,
66 PROP_REFERER,
67 PROP_FOLLOW_LOCATION,
68 PROP_CHECK_FILE_SERVER_TIME,
69};
70
71G_DEFINE_TYPE (VikWmscMapSource, vik_wmsc_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
72
73static void
74vik_wmsc_map_source_init (VikWmscMapSource *self)
75{
76 /* initialize the object here */
77 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
78
79 priv->hostname = NULL;
80 priv->url = NULL;
81 priv->options.referer = NULL;
82 priv->options.follow_location = 0;
83 priv->options.check_file = a_check_map_file;
84 priv->options.check_file_server_time = FALSE;
85
86 g_object_set (G_OBJECT (self),
87 "tilesize-x", 256,
88 "tilesize-y", 256,
89 "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON,
90 NULL);
91}
92
93static void
94vik_wmsc_map_source_finalize (GObject *object)
95{
96 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
97 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
98
99 g_free (priv->hostname);
100 priv->hostname = NULL;
101 g_free (priv->url);
102 priv->url = NULL;
103 g_free (priv->options.referer);
104 priv->options.referer = NULL;
105
106 G_OBJECT_CLASS (vik_wmsc_map_source_parent_class)->finalize (object);
107}
108
109static void
110vik_wmsc_map_source_set_property (GObject *object,
111 guint property_id,
112 const GValue *value,
113 GParamSpec *pspec)
114{
115 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
116 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
117
118 switch (property_id)
119 {
120 case PROP_HOSTNAME:
121 g_free (priv->hostname);
122 priv->hostname = g_value_dup_string (value);
123 break;
124
125 case PROP_URL:
126 g_free (priv->url);
127 priv->url = g_value_dup_string (value);
128 break;
129
130 case PROP_REFERER:
131 g_free (priv->options.referer);
132 priv->options.referer = g_value_dup_string (value);
133 break;
134
135 case PROP_FOLLOW_LOCATION:
136 priv->options.follow_location = g_value_get_long (value);
137 break;
138
139 case PROP_CHECK_FILE_SERVER_TIME:
140 priv->options.check_file_server_time = g_value_get_boolean (value);
141 break;
142
143 default:
144 /* We don't have any other property... */
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
146 break;
147 }
148}
149
150static void
151vik_wmsc_map_source_get_property (GObject *object,
152 guint property_id,
153 GValue *value,
154 GParamSpec *pspec)
155{
156 VikWmscMapSource *self = VIK_WMSC_MAP_SOURCE (object);
157 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE (self);
158
159 switch (property_id)
160 {
161 case PROP_HOSTNAME:
162 g_value_set_string (value, priv->hostname);
163 break;
164
165 case PROP_URL:
166 g_value_set_string (value, priv->url);
167 break;
168
169 case PROP_REFERER:
170 g_value_set_string (value, priv->options.referer);
171 break;
172
173 case PROP_FOLLOW_LOCATION:
174 g_value_set_long (value, priv->options.follow_location);
175 break;
176
177 case PROP_CHECK_FILE_SERVER_TIME:
178 g_value_set_boolean (value, priv->options.check_file_server_time);
179 break;
180
181 default:
182 /* We don't have any other property... */
183 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
184 break;
185 }
186}
187
188static void
189vik_wmsc_map_source_class_init (VikWmscMapSourceClass *klass)
190{
191 GObjectClass* object_class = G_OBJECT_CLASS (klass);
192 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
193 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
194 GParamSpec *pspec = NULL;
195
196 object_class->set_property = vik_wmsc_map_source_set_property;
197 object_class->get_property = vik_wmsc_map_source_get_property;
198
199 /* Overiding methods */
200 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
201 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
202 grandparent_class->supports_download_only_new = _supports_download_only_new;
203 grandparent_class->is_direct_file_access = _is_direct_file_access;
204
205 parent_class->get_uri = _get_uri;
206 parent_class->get_hostname = _get_hostname;
207 parent_class->get_download_options = _get_download_options;
208
209 pspec = g_param_spec_string ("hostname",
210 "Hostname",
211 "The hostname of the map server",
212 "<no-set>" /* default value */,
213 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
214 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
215
216 pspec = g_param_spec_string ("url",
217 "URL",
218 "The template of the tiles' URL",
219 "<no-set>" /* default value */,
220 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
221 g_object_class_install_property (object_class, PROP_URL, pspec);
222
223 pspec = g_param_spec_string ("referer",
224 "Referer",
225 "The REFERER string to use in HTTP request",
226 NULL /* default value */,
227 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
228 g_object_class_install_property (object_class, PROP_REFERER, pspec);
229
230 pspec = g_param_spec_long ("follow-location",
231 "Follow location",
232 "Specifies the number of retries to follow a redirect while downloading a page",
233 0 /* minimum value */,
234 G_MAXLONG /* maximum value */,
235 0 /* default value */,
236 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
237 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
238
239 pspec = g_param_spec_boolean ("check-file-server-time",
240 "Check file server time",
241 "Age of current cache before redownloading tile",
242 FALSE /* default value */,
243 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
244 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
245
246 g_type_class_add_private (klass, sizeof (VikWmscMapSourcePrivate));
247
248 object_class->finalize = vik_wmsc_map_source_finalize;
249}
250
251/* 1 << (x) is like a 2**(x) */
252#define GZ(x) ((1<<x))
253
254static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
255 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
256
257static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
258
259static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
260static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
261
262#define ERROR_MARGIN 0.01
263static gint wmsc_zoom ( gdouble mpp ) {
264 gint i;
265 for ( i = 0; i < num_scales; i++ ) {
266 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
267 return i;
268 }
269 }
270 for ( i = 0; i < num_scales_neg; i++ ) {
271 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
272 return -i;
273 }
274 }
275
276 return 255;
277}
278
279gboolean
280_supports_download_only_new (VikMapSource *self)
281{
282 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), FALSE);
283
284 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
285
286 return priv->options.check_file_server_time;
287}
288
289static gboolean
290_is_direct_file_access ( VikMapSource *self )
291{
292 return FALSE;
293}
294
295static gboolean
296_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
297{
298 g_assert ( src->mode == VIK_COORD_LATLON );
299
300 if ( xzoom != yzoom )
301 return FALSE;
302
303 dest->scale = wmsc_zoom ( xzoom );
304 if ( dest->scale == 255 )
305 return FALSE;
306
307 /* Note : GZ(17) / xzoom / 2 = number of tile on Y axis */
308 g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__,
309 xzoom, yzoom, GZ(17) / xzoom / 2);
310 dest->x = floor((src->east_west + 180) / 180 * GZ(17) / xzoom / 2);
311 /* We should restore logic of viking:
312 * tile index on Y axis follow a screen logic (top -> down)
313 */
314 dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2);
315 dest->z = 0;
316 g_debug("%s: %f,%f -> %d,%d", __FUNCTION__,
317 src->east_west, src->north_south, dest->x, dest->y);
318 return TRUE;
319}
320
321static void
322_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
323{
324 gdouble socalled_mpp;
325 if (src->scale >= 0)
326 socalled_mpp = GZ(src->scale);
327 else
328 socalled_mpp = 1.0/GZ(-src->scale);
329 dest->mode = VIK_COORD_LATLON;
330 dest->east_west = (src->x+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 180;
331 /* We should restore logic of viking:
332 * tile index on Y axis follow a screen logic (top -> down)
333 */
334 dest->north_south = -((src->y+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 90);
335 g_debug("%s: %d,%d -> %f,%f", __FUNCTION__,
336 src->x, src->y, dest->east_west, dest->north_south);
337}
338
339static gchar *
340_get_uri( VikMapSourceDefault *self, MapCoord *src )
341{
342 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
343
344 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
345 gdouble socalled_mpp;
346 if (src->scale >= 0)
347 socalled_mpp = GZ(src->scale);
348 else
349 socalled_mpp = 1.0/GZ(-src->scale);
350 gdouble minx = (gdouble)src->x * 180 / GZ(17) * socalled_mpp * 2 - 180;
351 gdouble maxx = (gdouble)(src->x + 1) * 180 / GZ(17) * socalled_mpp * 2 - 180;
352 /* We should restore logic of viking:
353 * tile index on Y axis follow a screen logic (top -> down)
354 */
355 gdouble miny = -((gdouble)(src->y + 1) * 180 / GZ(17) * socalled_mpp * 2 - 90);
356 gdouble maxy = -((gdouble)(src->y) * 180 / GZ(17) * socalled_mpp * 2 - 90);
357
358 gchar sminx[G_ASCII_DTOSTR_BUF_SIZE];
359 gchar smaxx[G_ASCII_DTOSTR_BUF_SIZE];
360 gchar sminy[G_ASCII_DTOSTR_BUF_SIZE];
361 gchar smaxy[G_ASCII_DTOSTR_BUF_SIZE];
362
363 g_ascii_dtostr (sminx, G_ASCII_DTOSTR_BUF_SIZE, minx);
364 g_ascii_dtostr (smaxx, G_ASCII_DTOSTR_BUF_SIZE, maxx);
365 g_ascii_dtostr (sminy, G_ASCII_DTOSTR_BUF_SIZE, miny);
366 g_ascii_dtostr (smaxy, G_ASCII_DTOSTR_BUF_SIZE, maxy);
367
368 gchar *uri = g_strdup_printf (priv->url, sminx, sminy, smaxx, smaxy);
369
370 return uri;
371}
372
373static gchar *
374_get_hostname( VikMapSourceDefault *self )
375{
376 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
377
378 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
379 return g_strdup( priv->hostname );
380}
381
382static DownloadMapOptions *
383_get_download_options( VikMapSourceDefault *self )
384{
385 g_return_val_if_fail (VIK_IS_WMSC_MAP_SOURCE(self), NULL);
386
387 VikWmscMapSourcePrivate *priv = VIK_WMSC_MAP_SOURCE_PRIVATE(self);
388 return &(priv->options);
389}
390
391VikWmscMapSource *
392vik_wmsc_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
393{
394 return g_object_new(VIK_TYPE_WMSC_MAP_SOURCE,
395 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
396}