1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Guilhem Bonnefille 2009 <guilhem.bonnefille@gmail.com>
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/>.
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
48 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
50 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
53 vik_slippy_map_source_init (VikSlippyMapSource *object)
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;
62 vik_slippy_map_source_finalize (GObject *object)
64 /* TODO: Add deinitalization code here */
66 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
70 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
72 GObjectClass* object_class = G_OBJECT_CLASS (klass);
73 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
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;
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;
85 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
87 object_class->finalize = vik_slippy_map_source_finalize;
90 /* 1 << (x) is like a 2**(x) */
91 #define GZ(x) ((1<<x))
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) };
96 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
98 #define ERROR_MARGIN 0.01
99 static guint8 slippy_zoom ( gdouble mpp ) {
101 for ( i = 0; i < num_scales; i++ ) {
102 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
109 vik_slippy_map_source_get_uri( VikSlippyMapSource *self, MapCoord *src )
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);
116 g_return_val_if_fail (klass->get_uri != NULL, 0);
118 return (*klass->get_uri)(self, src);
122 vik_slippy_map_source_get_hostname( VikSlippyMapSource *self )
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);
129 g_return_val_if_fail (klass->get_hostname != NULL, 0);
131 return (*klass->get_hostname)(self);
135 vik_slippy_map_source_get_download_options( VikSlippyMapSource *self )
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);
142 g_return_val_if_fail (klass->get_download_options != NULL, 0);
144 return (*klass->get_download_options)(self);
148 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
150 g_assert ( src->mode == VIK_COORD_LATLON );
152 if ( xzoom != yzoom )
155 dest->scale = slippy_zoom ( xzoom );
156 if ( dest->scale == 255 )
159 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
160 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
167 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
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));
176 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
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 );
189 _get_uri( VikSlippyMapSource *self, MapCoord *src )
191 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
193 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
194 gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
199 _get_hostname( VikSlippyMapSource *self )
201 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
203 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
204 return g_strdup( priv->hostname );
207 static DownloadOptions *
208 _get_download_options( VikSlippyMapSource *self )
210 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
212 return &slippy_options;
216 vik_slippy_map_source_new_with_id (guint8 id, const gchar *hostname, const gchar *url)
218 VikSlippyMapSource *ret = g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE, NULL);
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);