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))
59 G_DEFINE_TYPE_EXTENDED (VikSlippyMapSource
, vik_slippy_map_source
, VIK_TYPE_MAP_SOURCE_DEFAULT
, (GTypeFlags
)0,);
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
;
70 g_object_set (G_OBJECT (self
),
73 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR
,
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
;
88 G_OBJECT_CLASS (vik_slippy_map_source_parent_class
)->finalize (object
);
92 vik_slippy_map_source_set_property (GObject
*object
,
97 VikSlippyMapSource
*self
= VIK_SLIPPY_MAP_SOURCE (object
);
98 VikSlippyMapSourcePrivate
*priv
= VIK_SLIPPY_MAP_SOURCE_PRIVATE (self
);
103 g_free (priv
->hostname
);
104 priv
->hostname
= g_value_dup_string (value
);
109 priv
->url
= g_value_dup_string (value
);
113 /* We don't have any other property... */
114 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
120 vik_slippy_map_source_get_property (GObject
*object
,
125 VikSlippyMapSource
*self
= VIK_SLIPPY_MAP_SOURCE (object
);
126 VikSlippyMapSourcePrivate
*priv
= VIK_SLIPPY_MAP_SOURCE_PRIVATE (self
);
131 g_value_set_string (value
, priv
->hostname
);
135 g_value_set_string (value
, priv
->url
);
139 /* We don't have any other property... */
140 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
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",
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",
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
) {
195 for ( i
= 0; i
< num_scales
; i
++ ) {
196 if ( ABS(scale_mpps
[i
] - mpp
) < ERROR_MARGIN
)
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
);
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
);
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
);
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
)
249 dest
->scale
= slippy_zoom ( xzoom
);
250 if ( dest
->scale
== 255 )
253 dest
->x
= (src
->east_west
+ 180) / 360 * GZ(17) / xzoom
;
254 dest
->y
= (180 - MERCLAT(src
->north_south
)) / 360 * GZ(17) / xzoom
;
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));
270 _download ( VikMapSource
*self
, MapCoord
*src
, const gchar
*dest_fn
)
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
);
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
);
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
;
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
);