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