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