Fix URL formatting issue
[viking/guyou.git] / src / vikslippymapsource.c
blob6736e72cad965d79efc775441396b2f006d9a0ed
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.
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.
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/>.
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #ifdef HAVE_MATH_H
24 #include <math.h>
25 #endif
27 #include "globals.h"
28 #include "vikslippymapsource.h"
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 );
34 static gchar *_get_uri( VikSlippyMapSource *self, MapCoord *src );
35 static gchar *_get_hostname( VikSlippyMapSource *self );
36 static DownloadOptions *_get_download_options( VikSlippyMapSource *self );
38 /* FIXME Huge gruik */
39 static DownloadOptions slippy_options = { NULL, 0, a_check_map_file };
41 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
42 struct _VikSlippyMapSourcePrivate
44 gchar *hostname;
45 gchar *url;
48 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
50 /* properties */
51 enum
53 PROP_0,
55 PROP_HOSTNAME,
56 PROP_URL,
59 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
61 static void
62 vik_slippy_map_source_init (VikSlippyMapSource *self)
64 /* initialize the object here */
65 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
67 priv->hostname = NULL;
68 priv->url = NULL;
70 g_object_set (G_OBJECT (self),
71 "tilesize-x", 256,
72 "tilesize-y", 256,
73 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
74 NULL);
77 static void
78 vik_slippy_map_source_finalize (GObject *object)
80 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
81 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
83 g_free (priv->hostname);
84 priv->hostname = NULL;
85 g_free (priv->url);
86 priv->url = NULL;
88 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
91 static void
92 vik_slippy_map_source_set_property (GObject *object,
93 guint property_id,
94 const GValue *value,
95 GParamSpec *pspec)
97 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
98 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
100 switch (property_id)
102 case PROP_HOSTNAME:
103 g_free (priv->hostname);
104 priv->hostname = g_value_dup_string (value);
105 break;
107 case PROP_URL:
108 g_free (priv->url);
109 priv->url = g_value_dup_string (value);
110 break;
112 default:
113 /* We don't have any other property... */
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
115 break;
119 static void
120 vik_slippy_map_source_get_property (GObject *object,
121 guint property_id,
122 GValue *value,
123 GParamSpec *pspec)
125 VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
126 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
128 switch (property_id)
130 case PROP_HOSTNAME:
131 g_value_set_string (value, priv->hostname);
132 break;
134 case PROP_URL:
135 g_value_set_string (value, priv->url);
136 break;
138 default:
139 /* We don't have any other property... */
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
141 break;
145 static void
146 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
148 GObjectClass* object_class = G_OBJECT_CLASS (klass);
149 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
150 GParamSpec *pspec = NULL;
152 object_class->set_property = vik_slippy_map_source_set_property;
153 object_class->get_property = vik_slippy_map_source_get_property;
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;
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;
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);
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);
179 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
181 object_class->finalize = vik_slippy_map_source_finalize;
184 /* 1 << (x) is like a 2**(x) */
185 #define GZ(x) ((1<<x))
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) };
190 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
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;
199 return 255;
202 gchar *
203 vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
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);
210 g_return_val_if_fail (klass->get_uri != NULL, 0);
212 return (*klass->get_uri)(self, src);
215 gchar *
216 vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
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);
223 g_return_val_if_fail (klass->get_hostname != NULL, 0);
225 return (*klass->get_hostname)(self);
228 DownloadOptions *
229 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
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);
236 g_return_val_if_fail (klass->get_download_options != NULL, 0);
238 return (*klass->get_download_options)(self);
241 static gboolean
242 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
244 g_assert ( src->mode == VIK_COORD_LATLON );
246 if ( xzoom != yzoom )
247 return FALSE;
249 dest->scale = slippy_zoom ( xzoom );
250 if ( dest->scale == 255 )
251 return FALSE;
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;
257 return TRUE;
260 static void
261 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
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));
269 static int
270 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
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;
282 static gchar *
283 _get_uri( VikSlippyMapSource *self, MapCoord *src )
285 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
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;
292 static gchar *
293 _get_hostname( VikSlippyMapSource *self )
295 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
297 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
298 return g_strdup( priv->hostname );
301 static DownloadOptions *
302 _get_download_options( VikSlippyMapSource *self )
304 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
306 return &slippy_options;
309 VikSlippyMapSource *
310 vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
312 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
313 "id", id, "label", label, "hostname", hostname, "url", url, NULL);