]> git.street.me.uk Git - andy/viking.git/blob - src/vikslippymapsource.c
Merge branch 'gobjectify-map-type' into gobject-builder
[andy/viking.git] / src / vikslippymapsource.c
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"
28 #include "vikslippymapsource.h"
29
30 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
31 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
32 static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn );
33
34 static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
35 static gchar *_get_hostname( VikSlippyMapSource *self );
36 static DownloadOptions *_get_download_options( VikSlippyMapSource *self );
37
38 /* FIXME Huge gruik */
39 static DownloadOptions slippy_options = { NULL, 0, a_check_map_file };
40
41 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
42 struct _VikSlippyMapSourcePrivate
43 {
44   gchar *hostname;
45   gchar *url;
46 };
47
48 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
49
50 /* properties */
51 enum
52 {
53   PROP_0,
54
55   PROP_HOSTNAME,
56   PROP_URL,
57 };
58
59 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
60
61 static void
62 vik_slippy_map_source_init (VikSlippyMapSource *self)
63 {
64   /* initialize the object here */
65   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
66
67   priv->hostname = NULL;
68   priv->url = NULL;
69
70   g_object_set (G_OBJECT (self),
71                 "tilesize-x", 256,
72                 "tilesize-y", 256,
73                 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
74                 NULL);
75 }
76
77 static void
78 vik_slippy_map_source_finalize (GObject *object)
79 {
80   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
81   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
82
83   g_free (priv->hostname);
84   priv->hostname = NULL;
85   g_free (priv->url);
86   priv->url = NULL;
87
88   G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
89 }
90
91 static void
92 vik_slippy_map_source_set_property (GObject      *object,
93                                     guint         property_id,
94                                     const GValue *value,
95                                     GParamSpec   *pspec)
96 {
97   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
98   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
99
100   switch (property_id)
101     {
102     case PROP_HOSTNAME:
103       g_free (priv->hostname);
104       priv->hostname = g_value_dup_string (value);
105       break;
106
107     case PROP_URL:
108       g_free (priv->url);
109       priv->url = g_value_dup_string (value);
110       break;
111
112     default:
113       /* We don't have any other property... */
114       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115       break;
116     }
117 }
118
119 static void
120 vik_slippy_map_source_get_property (GObject    *object,
121                                     guint       property_id,
122                                     GValue     *value,
123                                     GParamSpec *pspec)
124 {
125   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
126   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
127
128   switch (property_id)
129     {
130     case PROP_HOSTNAME:
131       g_value_set_string (value, priv->hostname);
132       break;
133
134     case PROP_URL:
135       g_value_set_string (value, priv->url);
136       break;
137
138     default:
139       /* We don't have any other property... */
140       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141       break;
142     }
143 }
144
145 static void
146 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
147 {
148         GObjectClass* object_class = G_OBJECT_CLASS (klass);
149         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
150         GParamSpec *pspec = NULL;
151                 
152         object_class->set_property = vik_slippy_map_source_set_property;
153     object_class->get_property = vik_slippy_map_source_get_property;
154
155         /* Overiding methods */
156         parent_class->coord_to_mapcoord =        _coord_to_mapcoord;
157         parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
158         parent_class->download =                 _download;
159         
160         /* Default implementation of methods */
161         klass->get_uri = _get_uri;
162         klass->get_hostname = _get_hostname;
163         klass->get_download_options = _get_download_options;
164
165         pspec = g_param_spec_string ("hostname",
166                                      "Hostname",
167                                      "The hostname of the map server",
168                                      "<no-set>" /* default value */,
169                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
170         g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
171
172         pspec = g_param_spec_string ("url",
173                                      "URL",
174                                      "The template of the tiles' URL",
175                                      "<no-set>" /* default value */,
176                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
177         g_object_class_install_property (object_class, PROP_URL, pspec);
178         
179         g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
180         
181         object_class->finalize = vik_slippy_map_source_finalize;
182 }
183
184 /* 1 << (x) is like a 2**(x) */
185 #define GZ(x) ((1<<x))
186
187 static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
188                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
189
190 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
191
192 #define ERROR_MARGIN 0.01
193 static guint8 slippy_zoom ( gdouble mpp ) {
194   gint i;
195   for ( i = 0; i < num_scales; i++ ) {
196     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
197       return i;
198   }
199   return 255;
200 }
201
202 gchar *
203 vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
204 {
205         VikSlippyMapSourceClass *klass;
206         g_return_val_if_fail (self != NULL, 0);
207         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
208         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
209
210         g_return_val_if_fail (klass->get_uri != NULL, 0);
211
212         return (*klass->get_uri)(self, src);
213 }
214
215 gchar *
216 vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
217 {
218         VikSlippyMapSourceClass *klass;
219         g_return_val_if_fail (self != NULL, 0);
220         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
221         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
222
223         g_return_val_if_fail (klass->get_hostname != NULL, 0);
224
225         return (*klass->get_hostname)(self);
226 }
227
228 DownloadOptions *
229 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
230 {
231         VikSlippyMapSourceClass *klass;
232         g_return_val_if_fail (self != NULL, 0);
233         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
234         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
235
236         g_return_val_if_fail (klass->get_download_options != NULL, 0);
237
238         return (*klass->get_download_options)(self);
239 }
240
241 static gboolean
242 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
243 {
244   g_assert ( src->mode == VIK_COORD_LATLON );
245
246   if ( xzoom != yzoom )
247     return FALSE;
248
249   dest->scale = slippy_zoom ( xzoom );
250   if ( dest->scale == 255 )
251     return FALSE;
252
253   dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
254   dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
255   dest->z = 0;
256
257   return TRUE;
258 }
259
260 static void
261 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
262 {
263   gdouble socalled_mpp = GZ(src->scale);
264   dest->mode = VIK_COORD_LATLON;
265   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
266   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
267 }
268
269 static int
270 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
271 {
272    int res;
273    gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
274    gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
275    DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
276    res = a_http_download_get_url ( host, uri, dest_fn, options );
277    g_free ( uri );
278    g_free ( host );
279    return res;
280 }
281
282 static gchar *
283 _get_uri( VikSlippyMapSource *self, MapCoord *src )
284 {
285         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
286         
287     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
288         gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
289         return uri;
290
291
292 static gchar *
293 _get_hostname( VikSlippyMapSource *self )
294 {
295         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
296         
297     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
298         return g_strdup( priv->hostname );
299 }
300
301 static DownloadOptions *
302 _get_download_options( VikSlippyMapSource *self )
303 {
304         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
305         
306         return &slippy_options;
307 }
308
309 VikSlippyMapSource *
310 vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
311 {
312         return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
313                             "id", id, "label", label, "hostname", hostname, "url", url, NULL);
314 }