]> git.street.me.uk Git - andy/viking.git/blame - src/vikslippymapsource.c
Set 'OK' default for goto dialogs.
[andy/viking.git] / src / vikslippymapsource.c
CommitLineData
e3a90009
GB
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking
a482007a 4 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
e3a90009
GB
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 */
dfc4db8b
GB
19
20 /**
21 * SECTION:vikslippymapsource
22 * @short_description: the class for SlippyMap oriented map sources
23 *
24 * The #VikSlippyMapSource class handles slippy map oriented map sources.
25 * The related service is tile oriented, à la Google.
26 *
27 * The tiles are in 'google spherical mercator', which is
28 * basically a mercator projection that assumes a spherical earth.
29 * http://docs.openlayers.org/library/spherical_mercator.html
30 *
31 * Such service is also a type of TMS (Tile Map Service) as defined in
32 * OSGeo's wiki.
33 * http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
34 * But take care that the Y axis is inverted, ie the origin is at top-left
35 * corner.
36 * Following this specification, the protocol handled by this class
37 * follows the global-mercator profile.
38 *
39 * You can also find many interesting information on the OSM's wiki.
40 * http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
41 * http://wiki.openstreetmap.org/wiki/Setting_up_TMS
42 */
43
e3a90009
GB
44#ifdef HAVE_CONFIG_H
45#include "config.h"
46#endif
47
48#ifdef HAVE_MATH_H
49#include <math.h>
50#endif
51
52#include "globals.h"
8eb12ac6 53#include "vikslippymapsource.h"
e3a90009
GB
54
55static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
56static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
825413ba
SW
57static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle );
58static void * _download_handle_init ( VikMapSource *self);
59static void _download_handle_cleanup ( VikMapSource *self, void *handle);
c81ded98 60static gboolean _supports_download_only_new (VikMapSource *self );
e3a90009 61
8eb12ac6
GB
62static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
63static gchar *_get_hostname( VikSlippyMapSource *self );
b529dc9c 64static DownloadMapOptions *_get_download_options( VikSlippyMapSource *self );
e3a90009 65
8eb12ac6
GB
66typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
67struct _VikSlippyMapSourcePrivate
68{
69 gchar *hostname;
70 gchar *url;
b529dc9c 71 DownloadMapOptions options;
8eb12ac6
GB
72};
73
74#define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
75
4e53f3dc
GB
76/* properties */
77enum
78{
79 PROP_0,
80
81 PROP_HOSTNAME,
82 PROP_URL,
c395c6a5
GB
83 PROP_REFERER,
84 PROP_FOLLOW_LOCATION,
0f08bd0d 85 PROP_CHECK_FILE_SERVER_TIME,
05dbd9ba 86 PROP_USE_ETAG,
4e53f3dc
GB
87};
88
f6411015 89G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
e3a90009
GB
90
91static void
3a84e537 92vik_slippy_map_source_init (VikSlippyMapSource *self)
e3a90009 93{
9cca9d93
GB
94 /* initialize the object here */
95 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
96
97 priv->hostname = NULL;
98 priv->url = NULL;
c395c6a5
GB
99 priv->options.referer = NULL;
100 priv->options.follow_location = 0;
101 priv->options.check_file = a_check_map_file;
6693f5f9 102 priv->options.check_file_server_time = FALSE;
9cca9d93
GB
103
104 g_object_set (G_OBJECT (self),
105 "tilesize-x", 256,
106 "tilesize-y", 256,
107 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
108 NULL);
e3a90009
GB
109}
110
111static void
8eb12ac6 112vik_slippy_map_source_finalize (GObject *object)
e3a90009 113{
9cca9d93
GB
114 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
115 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
116
117 g_free (priv->hostname);
118 priv->hostname = NULL;
119 g_free (priv->url);
120 priv->url = NULL;
c395c6a5
GB
121 g_free (priv->options.referer);
122 priv->options.referer = NULL;
e3a90009 123
9cca9d93 124 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
e3a90009
GB
125}
126
4e53f3dc
GB
127static void
128vik_slippy_map_source_set_property (GObject *object,
129 guint property_id,
130 const GValue *value,
131 GParamSpec *pspec)
132{
133 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
134 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
135
136 switch (property_id)
137 {
138 case PROP_HOSTNAME:
139 g_free (priv->hostname);
140 priv->hostname = g_value_dup_string (value);
141 break;
142
143 case PROP_URL:
144 g_free (priv->url);
145 priv->url = g_value_dup_string (value);
146 break;
147
c395c6a5
GB
148 case PROP_REFERER:
149 g_free (priv->options.referer);
150 priv->options.referer = g_value_dup_string (value);
151 break;
152
153 case PROP_FOLLOW_LOCATION:
154 priv->options.follow_location = g_value_get_long (value);
155 break;
156
0f08bd0d 157 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 158 priv->options.check_file_server_time = g_value_get_boolean (value);
0f08bd0d
GB
159 break;
160
05dbd9ba
JJ
161 case PROP_USE_ETAG:
162 priv->options.use_etag = g_value_get_boolean (value);
163 break;
164
4e53f3dc
GB
165 default:
166 /* We don't have any other property... */
167 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
168 break;
169 }
170}
171
172static void
173vik_slippy_map_source_get_property (GObject *object,
174 guint property_id,
175 GValue *value,
176 GParamSpec *pspec)
177{
178 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
179 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
180
181 switch (property_id)
182 {
183 case PROP_HOSTNAME:
184 g_value_set_string (value, priv->hostname);
185 break;
186
187 case PROP_URL:
188 g_value_set_string (value, priv->url);
189 break;
190
c395c6a5
GB
191 case PROP_REFERER:
192 g_value_set_string (value, priv->options.referer);
193 break;
194
195 case PROP_FOLLOW_LOCATION:
196 g_value_set_long (value, priv->options.follow_location);
197 break;
198
0f08bd0d 199 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 200 g_value_set_boolean (value, priv->options.check_file_server_time);
0f08bd0d
GB
201 break;
202
05dbd9ba
JJ
203 case PROP_USE_ETAG:
204 g_value_set_boolean (value, priv->options.use_etag);
205 break;
206
4e53f3dc
GB
207 default:
208 /* We don't have any other property... */
209 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
210 break;
211 }
212}
213
e3a90009 214static void
8eb12ac6 215vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
e3a90009
GB
216{
217 GObjectClass* object_class = G_OBJECT_CLASS (klass);
218 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
4e53f3dc
GB
219 GParamSpec *pspec = NULL;
220
221 object_class->set_property = vik_slippy_map_source_set_property;
222 object_class->get_property = vik_slippy_map_source_get_property;
e3a90009
GB
223
224 /* Overiding methods */
225 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
226 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
227 parent_class->download = _download;
825413ba
SW
228 parent_class->download_handle_init = _download_handle_init;
229 parent_class->download_handle_cleanup = _download_handle_cleanup;
c81ded98 230 parent_class->supports_download_only_new = _supports_download_only_new;
8eb12ac6
GB
231
232 /* Default implementation of methods */
233 klass->get_uri = _get_uri;
234 klass->get_hostname = _get_hostname;
235 klass->get_download_options = _get_download_options;
4e53f3dc
GB
236
237 pspec = g_param_spec_string ("hostname",
238 "Hostname",
239 "The hostname of the map server",
240 "<no-set>" /* default value */,
241 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
242 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
243
244 pspec = g_param_spec_string ("url",
245 "URL",
246 "The template of the tiles' URL",
247 "<no-set>" /* default value */,
248 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
249 g_object_class_install_property (object_class, PROP_URL, pspec);
c395c6a5
GB
250
251 pspec = g_param_spec_string ("referer",
252 "Referer",
253 "The REFERER string to use in HTTP request",
254 NULL /* default value */,
255 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
256 g_object_class_install_property (object_class, PROP_REFERER, pspec);
8eb12ac6 257
c395c6a5
GB
258 pspec = g_param_spec_long ("follow-location",
259 "Follow location",
260 "Specifies the number of retries to follow a redirect while downloading a page",
261 0 /* minimum value */,
262 G_MAXLONG /* maximum value */,
263 0 /* default value */,
264 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
265 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
0f08bd0d 266
6693f5f9
GB
267 pspec = g_param_spec_boolean ("check-file-server-time",
268 "Check file server time",
269 "Age of current cache before redownloading tile",
270 FALSE /* default value */,
271 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
0f08bd0d 272 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
c395c6a5 273
05dbd9ba
JJ
274 pspec = g_param_spec_boolean ("use-etag",
275 "Use etag values with server",
276 "Store etag in a file, and send it to server to check if we have the latest file",
277 FALSE /* default value */,
278 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
279 g_object_class_install_property (object_class, PROP_USE_ETAG, pspec);
280
8eb12ac6
GB
281 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
282
283 object_class->finalize = vik_slippy_map_source_finalize;
e3a90009
GB
284}
285
286/* 1 << (x) is like a 2**(x) */
a2833555 287#define GZ(x) ((1<<(x)))
e3a90009
GB
288
289static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
290 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
291
292static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
293
d1302dec
JJ
294static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
295static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
296
e3a90009 297#define ERROR_MARGIN 0.01
d1302dec 298static gint slippy_zoom ( gdouble mpp ) {
e3a90009
GB
299 gint i;
300 for ( i = 0; i < num_scales; i++ ) {
d1302dec 301 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
e3a90009 302 return i;
d1302dec 303 }
e3a90009 304 }
d1302dec
JJ
305 for ( i = 0; i < num_scales_neg; i++ ) {
306 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
307 return -i;
308 }
309 }
310
e3a90009
GB
311 return 255;
312}
313
314gchar *
8eb12ac6 315vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
e3a90009 316{
8eb12ac6 317 VikSlippyMapSourceClass *klass;
e3a90009 318 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
319 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
320 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
321
322 g_return_val_if_fail (klass->get_uri != NULL, 0);
323
324 return (*klass->get_uri)(self, src);
325}
326
327gchar *
8eb12ac6 328vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
e3a90009 329{
8eb12ac6 330 VikSlippyMapSourceClass *klass;
e3a90009 331 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
332 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
333 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
334
335 g_return_val_if_fail (klass->get_hostname != NULL, 0);
336
337 return (*klass->get_hostname)(self);
338}
339
b529dc9c 340DownloadMapOptions *
8eb12ac6 341vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
e3a90009 342{
8eb12ac6 343 VikSlippyMapSourceClass *klass;
e3a90009 344 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
345 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
346 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
347
348 g_return_val_if_fail (klass->get_download_options != NULL, 0);
349
350 return (*klass->get_download_options)(self);
351}
352
0f08bd0d 353gboolean
c81ded98 354_supports_download_only_new (VikMapSource *self)
0f08bd0d 355{
c81ded98 356 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
0f08bd0d 357
c81ded98 358 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
0f08bd0d 359
c81ded98 360 return priv->options.check_file_server_time || priv->options.use_etag;
0f08bd0d 361}
e3a90009
GB
362static gboolean
363_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
364{
365 g_assert ( src->mode == VIK_COORD_LATLON );
366
367 if ( xzoom != yzoom )
368 return FALSE;
369
370 dest->scale = slippy_zoom ( xzoom );
371 if ( dest->scale == 255 )
372 return FALSE;
373
374 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
375 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
376 dest->z = 0;
377
378 return TRUE;
379}
380
381static void
382_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
383{
d1302dec
JJ
384 gdouble socalled_mpp;
385 if (src->scale >= 0)
386 socalled_mpp = GZ(src->scale);
387 else
388 socalled_mpp = 1.0/GZ(-src->scale);
e3a90009
GB
389 dest->mode = VIK_COORD_LATLON;
390 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
391 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
392}
393
394static int
825413ba 395_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
e3a90009
GB
396{
397 int res;
8eb12ac6
GB
398 gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
399 gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
b529dc9c 400 DownloadMapOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
825413ba 401 res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
e3a90009
GB
402 g_free ( uri );
403 g_free ( host );
404 return res;
405}
8eb12ac6 406
825413ba
SW
407static void *
408_download_handle_init ( VikMapSource *self )
409{
410 return a_download_handle_init ();
411}
412
413
414static void
415_download_handle_cleanup ( VikMapSource *self, void *handle )
416{
417 return a_download_handle_cleanup ( handle );
418}
419
8eb12ac6
GB
420static gchar *
421_get_uri( VikSlippyMapSource *self, MapCoord *src )
422{
423 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
424
425 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
426 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
427 return uri;
428}
429
430static gchar *
431_get_hostname( VikSlippyMapSource *self )
432{
433 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
434
435 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
436 return g_strdup( priv->hostname );
437}
438
b529dc9c 439static DownloadMapOptions *
8eb12ac6
GB
440_get_download_options( VikSlippyMapSource *self )
441{
442 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
443
c395c6a5
GB
444 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
445 return &(priv->options);
8eb12ac6
GB
446}
447
448VikSlippyMapSource *
db03733a 449vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
8eb12ac6 450{
4e53f3dc 451 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
db03733a 452 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
c395c6a5 453}