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