]> git.street.me.uk Git - andy/viking.git/blame - src/vikslippymapsource.c
Fix cycle map URL
[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
4 * Copyright (C) Guilhem Bonnefille 2009 <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 */
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);
0f08bd0d 60static gboolean _supports_if_modified_since (VikMapSource *self );
e3a90009 61
8eb12ac6
GB
62static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
63static gchar *_get_hostname( VikSlippyMapSource *self );
64static DownloadOptions *_get_download_options( VikSlippyMapSource *self );
e3a90009 65
8eb12ac6
GB
66typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
67struct _VikSlippyMapSourcePrivate
68{
69 gchar *hostname;
70 gchar *url;
c395c6a5 71 DownloadOptions 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,
4e53f3dc
GB
86};
87
f6411015 88G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
e3a90009
GB
89
90static void
3a84e537 91vik_slippy_map_source_init (VikSlippyMapSource *self)
e3a90009 92{
9cca9d93
GB
93 /* initialize the object here */
94 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
95
96 priv->hostname = NULL;
97 priv->url = NULL;
c395c6a5
GB
98 priv->options.referer = NULL;
99 priv->options.follow_location = 0;
100 priv->options.check_file = a_check_map_file;
6693f5f9 101 priv->options.check_file_server_time = FALSE;
9cca9d93
GB
102
103 g_object_set (G_OBJECT (self),
104 "tilesize-x", 256,
105 "tilesize-y", 256,
106 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
107 NULL);
e3a90009
GB
108}
109
110static void
8eb12ac6 111vik_slippy_map_source_finalize (GObject *object)
e3a90009 112{
9cca9d93
GB
113 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
114 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
115
116 g_free (priv->hostname);
117 priv->hostname = NULL;
118 g_free (priv->url);
119 priv->url = NULL;
c395c6a5
GB
120 g_free (priv->options.referer);
121 priv->options.referer = NULL;
e3a90009 122
9cca9d93 123 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
e3a90009
GB
124}
125
4e53f3dc
GB
126static void
127vik_slippy_map_source_set_property (GObject *object,
128 guint property_id,
129 const GValue *value,
130 GParamSpec *pspec)
131{
132 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
133 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
134
135 switch (property_id)
136 {
137 case PROP_HOSTNAME:
138 g_free (priv->hostname);
139 priv->hostname = g_value_dup_string (value);
140 break;
141
142 case PROP_URL:
143 g_free (priv->url);
144 priv->url = g_value_dup_string (value);
145 break;
146
c395c6a5
GB
147 case PROP_REFERER:
148 g_free (priv->options.referer);
149 priv->options.referer = g_value_dup_string (value);
150 break;
151
152 case PROP_FOLLOW_LOCATION:
153 priv->options.follow_location = g_value_get_long (value);
154 break;
155
0f08bd0d 156 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 157 priv->options.check_file_server_time = g_value_get_boolean (value);
0f08bd0d
GB
158 break;
159
4e53f3dc
GB
160 default:
161 /* We don't have any other property... */
162 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 break;
164 }
165}
166
167static void
168vik_slippy_map_source_get_property (GObject *object,
169 guint property_id,
170 GValue *value,
171 GParamSpec *pspec)
172{
173 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
174 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
175
176 switch (property_id)
177 {
178 case PROP_HOSTNAME:
179 g_value_set_string (value, priv->hostname);
180 break;
181
182 case PROP_URL:
183 g_value_set_string (value, priv->url);
184 break;
185
c395c6a5
GB
186 case PROP_REFERER:
187 g_value_set_string (value, priv->options.referer);
188 break;
189
190 case PROP_FOLLOW_LOCATION:
191 g_value_set_long (value, priv->options.follow_location);
192 break;
193
0f08bd0d 194 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 195 g_value_set_boolean (value, priv->options.check_file_server_time);
0f08bd0d
GB
196 break;
197
4e53f3dc
GB
198 default:
199 /* We don't have any other property... */
200 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
201 break;
202 }
203}
204
e3a90009 205static void
8eb12ac6 206vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
e3a90009
GB
207{
208 GObjectClass* object_class = G_OBJECT_CLASS (klass);
209 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
4e53f3dc
GB
210 GParamSpec *pspec = NULL;
211
212 object_class->set_property = vik_slippy_map_source_set_property;
213 object_class->get_property = vik_slippy_map_source_get_property;
e3a90009
GB
214
215 /* Overiding methods */
216 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
217 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
218 parent_class->download = _download;
825413ba
SW
219 parent_class->download_handle_init = _download_handle_init;
220 parent_class->download_handle_cleanup = _download_handle_cleanup;
0f08bd0d 221 parent_class->supports_if_modified_since = _supports_if_modified_since;
8eb12ac6
GB
222
223 /* Default implementation of methods */
224 klass->get_uri = _get_uri;
225 klass->get_hostname = _get_hostname;
226 klass->get_download_options = _get_download_options;
4e53f3dc
GB
227
228 pspec = g_param_spec_string ("hostname",
229 "Hostname",
230 "The hostname of the map server",
231 "<no-set>" /* default value */,
232 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
233 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
234
235 pspec = g_param_spec_string ("url",
236 "URL",
237 "The template of the tiles' URL",
238 "<no-set>" /* default value */,
239 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
240 g_object_class_install_property (object_class, PROP_URL, pspec);
c395c6a5
GB
241
242 pspec = g_param_spec_string ("referer",
243 "Referer",
244 "The REFERER string to use in HTTP request",
245 NULL /* default value */,
246 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
247 g_object_class_install_property (object_class, PROP_REFERER, pspec);
8eb12ac6 248
c395c6a5
GB
249 pspec = g_param_spec_long ("follow-location",
250 "Follow location",
251 "Specifies the number of retries to follow a redirect while downloading a page",
252 0 /* minimum value */,
253 G_MAXLONG /* maximum value */,
254 0 /* default value */,
255 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
256 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
0f08bd0d 257
6693f5f9
GB
258 pspec = g_param_spec_boolean ("check-file-server-time",
259 "Check file server time",
260 "Age of current cache before redownloading tile",
261 FALSE /* default value */,
262 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
0f08bd0d 263 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
c395c6a5 264
8eb12ac6
GB
265 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
266
267 object_class->finalize = vik_slippy_map_source_finalize;
e3a90009
GB
268}
269
270/* 1 << (x) is like a 2**(x) */
271#define GZ(x) ((1<<x))
272
273static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
274 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
275
276static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
277
d1302dec
JJ
278static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
279static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
280
e3a90009 281#define ERROR_MARGIN 0.01
d1302dec 282static gint slippy_zoom ( gdouble mpp ) {
e3a90009
GB
283 gint i;
284 for ( i = 0; i < num_scales; i++ ) {
d1302dec 285 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
e3a90009 286 return i;
d1302dec 287 }
e3a90009 288 }
d1302dec
JJ
289 for ( i = 0; i < num_scales_neg; i++ ) {
290 if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
291 return -i;
292 }
293 }
294
e3a90009
GB
295 return 255;
296}
297
298gchar *
8eb12ac6 299vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
e3a90009 300{
8eb12ac6 301 VikSlippyMapSourceClass *klass;
e3a90009 302 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
303 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
304 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
305
306 g_return_val_if_fail (klass->get_uri != NULL, 0);
307
308 return (*klass->get_uri)(self, src);
309}
310
311gchar *
8eb12ac6 312vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
e3a90009 313{
8eb12ac6 314 VikSlippyMapSourceClass *klass;
e3a90009 315 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
316 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
317 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
318
319 g_return_val_if_fail (klass->get_hostname != NULL, 0);
320
321 return (*klass->get_hostname)(self);
322}
323
324DownloadOptions *
8eb12ac6 325vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
e3a90009 326{
8eb12ac6 327 VikSlippyMapSourceClass *klass;
e3a90009 328 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
329 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
330 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
331
332 g_return_val_if_fail (klass->get_download_options != NULL, 0);
333
334 return (*klass->get_download_options)(self);
335}
336
0f08bd0d
GB
337gboolean
338_supports_if_modified_since (VikMapSource *self)
339{
340 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
341
342 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
343
6693f5f9 344 return priv->options.check_file_server_time;
0f08bd0d 345}
e3a90009
GB
346static gboolean
347_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
348{
349 g_assert ( src->mode == VIK_COORD_LATLON );
350
351 if ( xzoom != yzoom )
352 return FALSE;
353
354 dest->scale = slippy_zoom ( xzoom );
355 if ( dest->scale == 255 )
356 return FALSE;
357
358 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
359 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
360 dest->z = 0;
361
362 return TRUE;
363}
364
365static void
366_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
367{
d1302dec
JJ
368 gdouble socalled_mpp;
369 if (src->scale >= 0)
370 socalled_mpp = GZ(src->scale);
371 else
372 socalled_mpp = 1.0/GZ(-src->scale);
e3a90009
GB
373 dest->mode = VIK_COORD_LATLON;
374 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
375 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
376}
377
378static int
825413ba 379_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn, void *handle )
e3a90009
GB
380{
381 int res;
8eb12ac6
GB
382 gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
383 gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
384 DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
825413ba 385 res = a_http_download_get_url ( host, uri, dest_fn, options, handle );
e3a90009
GB
386 g_free ( uri );
387 g_free ( host );
388 return res;
389}
8eb12ac6 390
825413ba
SW
391static void *
392_download_handle_init ( VikMapSource *self )
393{
394 return a_download_handle_init ();
395}
396
397
398static void
399_download_handle_cleanup ( VikMapSource *self, void *handle )
400{
401 return a_download_handle_cleanup ( handle );
402}
403
8eb12ac6
GB
404static gchar *
405_get_uri( VikSlippyMapSource *self, MapCoord *src )
406{
407 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
408
409 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
410 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
411 return uri;
412}
413
414static gchar *
415_get_hostname( VikSlippyMapSource *self )
416{
417 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
418
419 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
420 return g_strdup( priv->hostname );
421}
422
423static DownloadOptions *
424_get_download_options( VikSlippyMapSource *self )
425{
426 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
427
c395c6a5
GB
428 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
429 return &(priv->options);
8eb12ac6
GB
430}
431
432VikSlippyMapSource *
db03733a 433vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
8eb12ac6 434{
4e53f3dc 435 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
db03733a 436 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
c395c6a5 437}