]> git.street.me.uk Git - andy/viking.git/blame - src/vikslippymapsource.c
Use combobox to select map type
[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 */
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#ifdef HAVE_MATH_H
24#include <math.h>
25#endif
26
27#include "globals.h"
8eb12ac6 28#include "vikslippymapsource.h"
e3a90009
GB
29
30static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
31static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
32static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn );
0f08bd0d 33static gboolean _supports_if_modified_since (VikMapSource *self );
e3a90009 34
8eb12ac6
GB
35static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
36static gchar *_get_hostname( VikSlippyMapSource *self );
37static DownloadOptions *_get_download_options( VikSlippyMapSource *self );
e3a90009 38
8eb12ac6
GB
39typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
40struct _VikSlippyMapSourcePrivate
41{
42 gchar *hostname;
43 gchar *url;
c395c6a5 44 DownloadOptions options;
8eb12ac6
GB
45};
46
47#define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
48
4e53f3dc
GB
49/* properties */
50enum
51{
52 PROP_0,
53
54 PROP_HOSTNAME,
55 PROP_URL,
c395c6a5
GB
56 PROP_REFERER,
57 PROP_FOLLOW_LOCATION,
0f08bd0d 58 PROP_CHECK_FILE_SERVER_TIME,
4e53f3dc
GB
59};
60
8eb12ac6 61G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
e3a90009
GB
62
63static void
3a84e537 64vik_slippy_map_source_init (VikSlippyMapSource *self)
e3a90009 65{
9cca9d93
GB
66 /* initialize the object here */
67 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
68
69 priv->hostname = NULL;
70 priv->url = NULL;
c395c6a5
GB
71 priv->options.referer = NULL;
72 priv->options.follow_location = 0;
73 priv->options.check_file = a_check_map_file;
0f08bd0d 74 priv->options.check_file_server_time = 0;
9cca9d93
GB
75
76 g_object_set (G_OBJECT (self),
77 "tilesize-x", 256,
78 "tilesize-y", 256,
79 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
80 NULL);
e3a90009
GB
81}
82
83static void
8eb12ac6 84vik_slippy_map_source_finalize (GObject *object)
e3a90009 85{
9cca9d93
GB
86 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
87 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
88
89 g_free (priv->hostname);
90 priv->hostname = NULL;
91 g_free (priv->url);
92 priv->url = NULL;
c395c6a5
GB
93 g_free (priv->options.referer);
94 priv->options.referer = NULL;
e3a90009 95
9cca9d93 96 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
e3a90009
GB
97}
98
4e53f3dc
GB
99static void
100vik_slippy_map_source_set_property (GObject *object,
101 guint property_id,
102 const GValue *value,
103 GParamSpec *pspec)
104{
105 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
106 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
107
108 switch (property_id)
109 {
110 case PROP_HOSTNAME:
111 g_free (priv->hostname);
112 priv->hostname = g_value_dup_string (value);
113 break;
114
115 case PROP_URL:
116 g_free (priv->url);
117 priv->url = g_value_dup_string (value);
118 break;
119
c395c6a5
GB
120 case PROP_REFERER:
121 g_free (priv->options.referer);
122 priv->options.referer = g_value_dup_string (value);
123 break;
124
125 case PROP_FOLLOW_LOCATION:
126 priv->options.follow_location = g_value_get_long (value);
127 break;
128
0f08bd0d
GB
129 case PROP_CHECK_FILE_SERVER_TIME:
130 priv->options.check_file_server_time = g_value_get_uint (value);
131 break;
132
4e53f3dc
GB
133 default:
134 /* We don't have any other property... */
135 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
136 break;
137 }
138}
139
140static void
141vik_slippy_map_source_get_property (GObject *object,
142 guint property_id,
143 GValue *value,
144 GParamSpec *pspec)
145{
146 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
147 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
148
149 switch (property_id)
150 {
151 case PROP_HOSTNAME:
152 g_value_set_string (value, priv->hostname);
153 break;
154
155 case PROP_URL:
156 g_value_set_string (value, priv->url);
157 break;
158
c395c6a5
GB
159 case PROP_REFERER:
160 g_value_set_string (value, priv->options.referer);
161 break;
162
163 case PROP_FOLLOW_LOCATION:
164 g_value_set_long (value, priv->options.follow_location);
165 break;
166
0f08bd0d
GB
167 case PROP_CHECK_FILE_SERVER_TIME:
168 g_value_set_uint (value, priv->options.check_file_server_time);
169 break;
170
4e53f3dc
GB
171 default:
172 /* We don't have any other property... */
173 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
174 break;
175 }
176}
177
e3a90009 178static void
8eb12ac6 179vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
e3a90009
GB
180{
181 GObjectClass* object_class = G_OBJECT_CLASS (klass);
182 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
4e53f3dc
GB
183 GParamSpec *pspec = NULL;
184
185 object_class->set_property = vik_slippy_map_source_set_property;
186 object_class->get_property = vik_slippy_map_source_get_property;
e3a90009
GB
187
188 /* Overiding methods */
189 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
190 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
191 parent_class->download = _download;
0f08bd0d 192 parent_class->supports_if_modified_since = _supports_if_modified_since;
8eb12ac6
GB
193
194 /* Default implementation of methods */
195 klass->get_uri = _get_uri;
196 klass->get_hostname = _get_hostname;
197 klass->get_download_options = _get_download_options;
4e53f3dc
GB
198
199 pspec = g_param_spec_string ("hostname",
200 "Hostname",
201 "The hostname of the map server",
202 "<no-set>" /* default value */,
203 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
204 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
205
206 pspec = g_param_spec_string ("url",
207 "URL",
208 "The template of the tiles' URL",
209 "<no-set>" /* default value */,
210 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
211 g_object_class_install_property (object_class, PROP_URL, pspec);
c395c6a5
GB
212
213 pspec = g_param_spec_string ("referer",
214 "Referer",
215 "The REFERER string to use in HTTP request",
216 NULL /* default value */,
217 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
218 g_object_class_install_property (object_class, PROP_REFERER, pspec);
8eb12ac6 219
c395c6a5
GB
220 pspec = g_param_spec_long ("follow-location",
221 "Follow location",
222 "Specifies the number of retries to follow a redirect while downloading a page",
223 0 /* minimum value */,
224 G_MAXLONG /* maximum value */,
225 0 /* default value */,
226 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
227 g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
0f08bd0d
GB
228
229 pspec = g_param_spec_uint ("check-file-server-time",
230 "Check file server time",
231 "Age of current cache before redownloading tile",
232 0 /* minimum value */,
233 G_MAXUINT16 /* maximum value */,
234 0 /* default value */,
235 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
236 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
c395c6a5 237
8eb12ac6
GB
238 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
239
240 object_class->finalize = vik_slippy_map_source_finalize;
e3a90009
GB
241}
242
243/* 1 << (x) is like a 2**(x) */
244#define GZ(x) ((1<<x))
245
246static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
247 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
248
249static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
250
251#define ERROR_MARGIN 0.01
252static guint8 slippy_zoom ( gdouble mpp ) {
253 gint i;
254 for ( i = 0; i < num_scales; i++ ) {
255 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
256 return i;
257 }
258 return 255;
259}
260
261gchar *
8eb12ac6 262vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
e3a90009 263{
8eb12ac6 264 VikSlippyMapSourceClass *klass;
e3a90009 265 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
266 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
267 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
268
269 g_return_val_if_fail (klass->get_uri != NULL, 0);
270
271 return (*klass->get_uri)(self, src);
272}
273
274gchar *
8eb12ac6 275vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
e3a90009 276{
8eb12ac6 277 VikSlippyMapSourceClass *klass;
e3a90009 278 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
279 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
280 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
281
282 g_return_val_if_fail (klass->get_hostname != NULL, 0);
283
284 return (*klass->get_hostname)(self);
285}
286
287DownloadOptions *
8eb12ac6 288vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
e3a90009 289{
8eb12ac6 290 VikSlippyMapSourceClass *klass;
e3a90009 291 g_return_val_if_fail (self != NULL, 0);
8eb12ac6
GB
292 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
293 klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
e3a90009
GB
294
295 g_return_val_if_fail (klass->get_download_options != NULL, 0);
296
297 return (*klass->get_download_options)(self);
298}
299
0f08bd0d
GB
300gboolean
301_supports_if_modified_since (VikMapSource *self)
302{
303 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
304
305 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
306
307 g_debug ("%s: priv->options.check_file_server_time = %d", __FUNCTION__, priv->options.check_file_server_time);
308
309 return priv->options.check_file_server_time != 0;
310}
e3a90009
GB
311static gboolean
312_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
313{
314 g_assert ( src->mode == VIK_COORD_LATLON );
315
316 if ( xzoom != yzoom )
317 return FALSE;
318
319 dest->scale = slippy_zoom ( xzoom );
320 if ( dest->scale == 255 )
321 return FALSE;
322
323 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
324 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
325 dest->z = 0;
326
327 return TRUE;
328}
329
330static void
331_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
332{
333 gdouble socalled_mpp = GZ(src->scale);
334 dest->mode = VIK_COORD_LATLON;
335 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
336 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
337}
338
339static int
340_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
341{
342 int res;
8eb12ac6
GB
343 gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
344 gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
345 DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
e3a90009
GB
346 res = a_http_download_get_url ( host, uri, dest_fn, options );
347 g_free ( uri );
348 g_free ( host );
349 return res;
350}
8eb12ac6
GB
351
352static gchar *
353_get_uri( VikSlippyMapSource *self, MapCoord *src )
354{
355 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
356
357 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
358 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
359 return uri;
360}
361
362static gchar *
363_get_hostname( VikSlippyMapSource *self )
364{
365 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
366
367 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
368 return g_strdup( priv->hostname );
369}
370
371static DownloadOptions *
372_get_download_options( VikSlippyMapSource *self )
373{
374 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
375
c395c6a5
GB
376 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
377 return &(priv->options);
8eb12ac6
GB
378}
379
380VikSlippyMapSource *
db03733a 381vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
8eb12ac6 382{
4e53f3dc 383 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
db03733a 384 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
c395c6a5 385}