]>
Commit | Line | Data |
---|---|---|
90f15672 GB |
1 | /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ |
2 | /* | |
3 | * viking | |
4 | * Copyright (C) 2010, Guilhem Bonnefille <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 | ||
20 | #ifdef HAVE_CONFIG_H | |
21 | #include "config.h" | |
22 | #endif | |
23 | ||
24 | #ifdef HAVE_MATH_H | |
25 | #include <math.h> | |
26 | #endif | |
27 | ||
28 | #include "globals.h" | |
29 | #include "viktmsmapsource.h" | |
30 | ||
31 | static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ); | |
32 | static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest ); | |
33 | static gboolean _supports_download_only_new (VikMapSource *self ); | |
34 | ||
35 | static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src ); | |
36 | static gchar *_get_hostname( VikMapSourceDefault *self ); | |
37 | static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self ); | |
38 | ||
39 | typedef struct _VikTmsMapSourcePrivate VikTmsMapSourcePrivate; | |
40 | struct _VikTmsMapSourcePrivate | |
41 | { | |
42 | gchar *hostname; | |
43 | gchar *url; | |
44 | DownloadMapOptions options; | |
45 | }; | |
46 | ||
47 | #define VIK_TMS_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_TMS_MAP_SOURCE, VikTmsMapSourcePrivate)) | |
48 | ||
49 | /* properties */ | |
50 | enum | |
51 | { | |
52 | PROP_0, | |
53 | ||
54 | PROP_HOSTNAME, | |
55 | PROP_URL, | |
56 | PROP_REFERER, | |
57 | PROP_FOLLOW_LOCATION, | |
58 | PROP_CHECK_FILE_SERVER_TIME, | |
59 | }; | |
60 | ||
61 | G_DEFINE_TYPE (VikTmsMapSource, vik_tms_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT); | |
62 | ||
63 | static void | |
64 | vik_tms_map_source_init (VikTmsMapSource *self) | |
65 | { | |
66 | /* initialize the object here */ | |
67 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self); | |
68 | ||
69 | priv->hostname = NULL; | |
70 | priv->url = NULL; | |
71 | priv->options.referer = NULL; | |
72 | priv->options.follow_location = 0; | |
73 | priv->options.check_file = a_check_map_file; | |
74 | priv->options.check_file_server_time = FALSE; | |
75 | ||
76 | g_object_set (G_OBJECT (self), | |
77 | "tilesize-x", 256, | |
78 | "tilesize-y", 256, | |
79 | "drawmode", VIK_VIEWPORT_DRAWMODE_LATLON, | |
80 | NULL); | |
81 | } | |
82 | ||
83 | static void | |
84 | vik_tms_map_source_finalize (GObject *object) | |
85 | { | |
86 | VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object); | |
87 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self); | |
88 | ||
89 | g_free (priv->hostname); | |
90 | priv->hostname = NULL; | |
91 | g_free (priv->url); | |
92 | priv->url = NULL; | |
93 | g_free (priv->options.referer); | |
94 | priv->options.referer = NULL; | |
95 | ||
96 | G_OBJECT_CLASS (vik_tms_map_source_parent_class)->finalize (object); | |
97 | } | |
98 | ||
99 | static void | |
100 | vik_tms_map_source_set_property (GObject *object, | |
101 | guint property_id, | |
102 | const GValue *value, | |
103 | GParamSpec *pspec) | |
104 | { | |
105 | VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object); | |
106 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self); | |
107 | ||
108 | switch (property_id) | |
109 | { | |
110 | case PROP_HOSTNAME: | |
111 | g_free (priv->hostname); | |
112 | priv->hostname = g_value_dup_string (value); | |
113 | break; | |
114 | ||
115 | case PROP_URL: | |
116 | g_free (priv->url); | |
117 | priv->url = g_value_dup_string (value); | |
118 | break; | |
119 | ||
120 | case PROP_REFERER: | |
121 | g_free (priv->options.referer); | |
122 | priv->options.referer = g_value_dup_string (value); | |
123 | break; | |
124 | ||
125 | case PROP_FOLLOW_LOCATION: | |
126 | priv->options.follow_location = g_value_get_long (value); | |
127 | break; | |
128 | ||
129 | case PROP_CHECK_FILE_SERVER_TIME: | |
130 | priv->options.check_file_server_time = g_value_get_boolean (value); | |
131 | break; | |
132 | ||
133 | default: | |
134 | /* We don't have any other property... */ | |
135 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
136 | break; | |
137 | } | |
138 | } | |
139 | ||
140 | static void | |
141 | vik_tms_map_source_get_property (GObject *object, | |
142 | guint property_id, | |
143 | GValue *value, | |
144 | GParamSpec *pspec) | |
145 | { | |
146 | VikTmsMapSource *self = VIK_TMS_MAP_SOURCE (object); | |
147 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE (self); | |
148 | ||
149 | switch (property_id) | |
150 | { | |
151 | case PROP_HOSTNAME: | |
152 | g_value_set_string (value, priv->hostname); | |
153 | break; | |
154 | ||
155 | case PROP_URL: | |
156 | g_value_set_string (value, priv->url); | |
157 | break; | |
158 | ||
159 | case PROP_REFERER: | |
160 | g_value_set_string (value, priv->options.referer); | |
161 | break; | |
162 | ||
163 | case PROP_FOLLOW_LOCATION: | |
164 | g_value_set_long (value, priv->options.follow_location); | |
165 | break; | |
166 | ||
167 | case PROP_CHECK_FILE_SERVER_TIME: | |
168 | g_value_set_boolean (value, priv->options.check_file_server_time); | |
169 | break; | |
170 | ||
171 | default: | |
172 | /* We don't have any other property... */ | |
173 | G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); | |
174 | break; | |
175 | } | |
176 | } | |
177 | ||
178 | static void | |
179 | vik_tms_map_source_class_init (VikTmsMapSourceClass *klass) | |
180 | { | |
181 | GObjectClass* object_class = G_OBJECT_CLASS (klass); | |
182 | VikMapSourceClass* granparent_class = VIK_MAP_SOURCE_CLASS (klass); | |
183 | VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass); | |
184 | GParamSpec *pspec = NULL; | |
185 | ||
186 | object_class->set_property = vik_tms_map_source_set_property; | |
187 | object_class->get_property = vik_tms_map_source_get_property; | |
188 | ||
189 | /* Overiding methods */ | |
190 | granparent_class->coord_to_mapcoord = _coord_to_mapcoord; | |
191 | granparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord; | |
192 | granparent_class->supports_download_only_new = _supports_download_only_new; | |
193 | ||
194 | parent_class->get_uri = _get_uri; | |
195 | parent_class->get_hostname = _get_hostname; | |
196 | parent_class->get_download_options = _get_download_options; | |
197 | ||
198 | pspec = g_param_spec_string ("hostname", | |
199 | "Hostname", | |
200 | "The hostname of the map server", | |
201 | "<no-set>" /* default value */, | |
202 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
203 | g_object_class_install_property (object_class, PROP_HOSTNAME, pspec); | |
204 | ||
205 | pspec = g_param_spec_string ("url", | |
206 | "URL", | |
207 | "The template of the tiles' URL", | |
208 | "<no-set>" /* default value */, | |
209 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
210 | g_object_class_install_property (object_class, PROP_URL, pspec); | |
211 | ||
212 | pspec = g_param_spec_string ("referer", | |
213 | "Referer", | |
214 | "The REFERER string to use in HTTP request", | |
215 | NULL /* default value */, | |
216 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
217 | g_object_class_install_property (object_class, PROP_REFERER, pspec); | |
218 | ||
219 | pspec = g_param_spec_long ("follow-location", | |
220 | "Follow location", | |
221 | "Specifies the number of retries to follow a redirect while downloading a page", | |
222 | 0 /* minimum value */, | |
223 | G_MAXLONG /* maximum value */, | |
224 | 0 /* default value */, | |
225 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
226 | g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec); | |
227 | ||
228 | pspec = g_param_spec_boolean ("check-file-server-time", | |
229 | "Check file server time", | |
230 | "Age of current cache before redownloading tile", | |
231 | FALSE /* default value */, | |
232 | G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE); | |
233 | g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec); | |
234 | ||
235 | g_type_class_add_private (klass, sizeof (VikTmsMapSourcePrivate)); | |
236 | ||
237 | object_class->finalize = vik_tms_map_source_finalize; | |
238 | } | |
239 | ||
240 | /* 1 << (x) is like a 2**(x) */ | |
241 | #define GZ(x) ((1<<(x))) | |
242 | ||
243 | 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), | |
244 | GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) }; | |
245 | ||
246 | static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])); | |
247 | ||
248 | static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) }; | |
249 | static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0])); | |
250 | ||
251 | #define ERROR_MARGIN 0.01 | |
252 | static gint tms_zoom ( gdouble mpp ) { | |
253 | gint i; | |
254 | for ( i = 0; i < num_scales; i++ ) { | |
255 | if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) { | |
256 | return i; | |
257 | } | |
258 | } | |
259 | for ( i = 0; i < num_scales_neg; i++ ) { | |
260 | if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) { | |
261 | return -i; | |
262 | } | |
263 | } | |
264 | ||
265 | return 255; | |
266 | } | |
267 | ||
268 | gboolean | |
269 | _supports_download_only_new (VikMapSource *self) | |
270 | { | |
271 | g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), FALSE); | |
272 | ||
273 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self); | |
274 | ||
275 | return priv->options.check_file_server_time; | |
276 | } | |
277 | ||
278 | static gboolean | |
279 | _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest ) | |
280 | { | |
281 | g_assert ( src->mode == VIK_COORD_LATLON ); | |
282 | ||
283 | if ( xzoom != yzoom ) | |
284 | return FALSE; | |
285 | ||
286 | dest->scale = tms_zoom ( xzoom ); | |
287 | if ( dest->scale == 255 ) | |
288 | return FALSE; | |
289 | ||
290 | /* Note : GZ(17) / xzoom / 2 = number of tile on Y axis */ | |
291 | g_debug("%s: xzoom=%f yzoom=%f -> %f", __FUNCTION__, | |
292 | xzoom, yzoom, GZ(17) / xzoom / 2); | |
293 | dest->x = floor((src->east_west + 180) / 180 * GZ(17) / xzoom / 2); | |
294 | /* We should restore logic of viking: | |
295 | * tile index on Y axis follow a screen logic (top -> down) | |
296 | */ | |
297 | dest->y = floor((180 - (src->north_south + 90)) / 180 * GZ(17) / xzoom / 2); | |
298 | dest->z = 0; | |
299 | g_debug("%s: %f,%f -> %d,%d", __FUNCTION__, | |
300 | src->east_west, src->north_south, dest->x, dest->y); | |
301 | return TRUE; | |
302 | } | |
303 | ||
304 | static void | |
305 | _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest ) | |
306 | { | |
307 | gdouble socalled_mpp; | |
308 | if (src->scale >= 0) | |
309 | socalled_mpp = GZ(src->scale); | |
310 | else | |
311 | socalled_mpp = 1.0/GZ(-src->scale); | |
312 | dest->mode = VIK_COORD_LATLON; | |
313 | dest->east_west = (src->x+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 180; | |
314 | /* We should restore logic of viking: | |
315 | * tile index on Y axis follow a screen logic (top -> down) | |
316 | */ | |
317 | dest->north_south = -((src->y+0.5) * 180 / GZ(17) * socalled_mpp * 2 - 90); | |
318 | g_debug("%s: %d,%d -> %f,%f", __FUNCTION__, | |
319 | src->x, src->y, dest->east_west, dest->north_south); | |
320 | } | |
321 | ||
322 | static gchar * | |
323 | _get_uri( VikMapSourceDefault *self, MapCoord *src ) | |
324 | { | |
325 | g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL); | |
326 | ||
327 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self); | |
328 | gdouble socalled_mpp; | |
329 | if (src->scale >= 0) | |
330 | socalled_mpp = GZ(src->scale); | |
331 | else | |
332 | socalled_mpp = 1.0/GZ(-src->scale); | |
333 | /* We should restore logic of viking: | |
334 | * tile index on Y axis follow a screen logic (top -> down) | |
335 | */ | |
336 | ||
337 | /* Note : nb tiles on Y axis */ | |
338 | gint nb_tiles = GZ(17 - src->scale - 1); | |
339 | ||
340 | gchar *uri = g_strdup_printf (priv->url, 17 - src->scale - 1, src->x, nb_tiles - src->y - 1); | |
341 | ||
342 | return uri; | |
343 | } | |
344 | ||
345 | static gchar * | |
346 | _get_hostname( VikMapSourceDefault *self ) | |
347 | { | |
348 | g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL); | |
349 | ||
350 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self); | |
351 | return g_strdup( priv->hostname ); | |
352 | } | |
353 | ||
354 | static DownloadMapOptions * | |
355 | _get_download_options( VikMapSourceDefault *self ) | |
356 | { | |
357 | g_return_val_if_fail (VIK_IS_TMS_MAP_SOURCE(self), NULL); | |
358 | ||
359 | VikTmsMapSourcePrivate *priv = VIK_TMS_MAP_SOURCE_PRIVATE(self); | |
360 | return &(priv->options); | |
361 | } | |
362 | ||
363 | VikTmsMapSource * | |
364 | vik_tms_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url) | |
365 | { | |
366 | return g_object_new(VIK_TYPE_TMS_MAP_SOURCE, | |
367 | "id", id, "label", label, "hostname", hostname, "url", url, NULL); | |
368 | } |