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