]> git.street.me.uk Git - andy/viking.git/blob - src/vikslippymapsource.c
Remove dependencies to gob2
[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 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
51
52 static void
53 vik_slippy_map_source_init (VikSlippyMapSource *object)
54 {
55         /* initialize the object here */
56         // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_x = 256;
57         // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_y = 256;
58         // FIXME VIK_MAP_SOURCE_DEFAULT(self)->drawmode = VIK_VIEWPORT_DRAWMODE_MERCATOR;
59 }
60
61 static void
62 vik_slippy_map_source_finalize (GObject *object)
63 {
64         /* TODO: Add deinitalization code here */
65
66         G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
67 }
68
69 static void
70 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
71 {
72         GObjectClass* object_class = G_OBJECT_CLASS (klass);
73         VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
74
75         /* Overiding methods */
76         parent_class->coord_to_mapcoord =        _coord_to_mapcoord;
77         parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
78         parent_class->download =                 _download;
79         
80         /* Default implementation of methods */
81         klass->get_uri = _get_uri;
82         klass->get_hostname = _get_hostname;
83         klass->get_download_options = _get_download_options;
84         
85         g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
86         
87         object_class->finalize = vik_slippy_map_source_finalize;
88 }
89
90 /* 1 << (x) is like a 2**(x) */
91 #define GZ(x) ((1<<x))
92
93 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),
94                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
95
96 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
97
98 #define ERROR_MARGIN 0.01
99 static guint8 slippy_zoom ( gdouble mpp ) {
100   gint i;
101   for ( i = 0; i < num_scales; i++ ) {
102     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
103       return i;
104   }
105   return 255;
106 }
107
108 gchar *
109 vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
110 {
111         VikSlippyMapSourceClass *klass;
112         g_return_val_if_fail (self != NULL, 0);
113         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
114         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
115
116         g_return_val_if_fail (klass->get_uri != NULL, 0);
117
118         return (*klass->get_uri)(self, src);
119 }
120
121 gchar *
122 vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
123 {
124         VikSlippyMapSourceClass *klass;
125         g_return_val_if_fail (self != NULL, 0);
126         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
127         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
128
129         g_return_val_if_fail (klass->get_hostname != NULL, 0);
130
131         return (*klass->get_hostname)(self);
132 }
133
134 DownloadOptions *
135 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
136 {
137         VikSlippyMapSourceClass *klass;
138         g_return_val_if_fail (self != NULL, 0);
139         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE (self), 0);
140         klass = VIK_SLIPPY_MAP_SOURCE_GET_CLASS(self);
141
142         g_return_val_if_fail (klass->get_download_options != NULL, 0);
143
144         return (*klass->get_download_options)(self);
145 }
146
147 static gboolean
148 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
149 {
150   g_assert ( src->mode == VIK_COORD_LATLON );
151
152   if ( xzoom != yzoom )
153     return FALSE;
154
155   dest->scale = slippy_zoom ( xzoom );
156   if ( dest->scale == 255 )
157     return FALSE;
158
159   dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
160   dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
161   dest->z = 0;
162
163   return TRUE;
164 }
165
166 static void
167 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
168 {
169   gdouble socalled_mpp = GZ(src->scale);
170   dest->mode = VIK_COORD_LATLON;
171   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
172   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
173 }
174
175 static int
176 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
177 {
178    int res;
179    gchar *uri = vik_slippy_map_source_get_uri(VIK_SLIPPY_MAP_SOURCE(self), src);
180    gchar *host = vik_slippy_map_source_get_hostname(VIK_SLIPPY_MAP_SOURCE(self));
181    DownloadOptions *options = vik_slippy_map_source_get_download_options(VIK_SLIPPY_MAP_SOURCE(self));
182    res = a_http_download_get_url ( host, uri, dest_fn, options );
183    g_free ( uri );
184    g_free ( host );
185    return res;
186 }
187
188 static gchar *
189 _get_uri( VikSlippyMapSource *self, MapCoord *src )
190 {
191         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
192         
193     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
194         gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
195         return uri;
196
197
198 static gchar *
199 _get_hostname( VikSlippyMapSource *self )
200 {
201         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
202         
203     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
204         return g_strdup( priv->hostname );
205 }
206
207 static DownloadOptions *
208 _get_download_options( VikSlippyMapSource *self )
209 {
210         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
211         
212         return &slippy_options;
213 }
214
215 VikSlippyMapSource *
216 vik_slippy_map_source_new_with_id (guint8 id, const gchar *hostname, const gchar *url)
217 {
218         VikSlippyMapSource *ret = g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE, NULL);
219         
220     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(ret);
221         // FIXME VIK_MAP_SOURCE_DEFAULT(ret)->uniq_id = id;
222         priv->hostname = g_strdup(hostname);
223         priv->url = g_strdup(url);
224         return ret;
225 }