]> git.street.me.uk Git - andy/viking.git/blob - src/vikslippymapsource.c
Decide the 'feature' type is more useful than seeing the country code.
[andy/viking.git] / src / vikslippymapsource.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking
4  * Copyright (C) 2009, 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  /**
21   * SECTION:vikslippymapsource
22   * @short_description: the class for SlippyMap oriented map sources
23   * 
24   * The #VikSlippyMapSource class handles slippy map oriented map sources.
25   * The related service is tile oriented, à la Google.
26   * 
27   * The tiles are in 'google spherical mercator', which is
28   * basically a mercator projection that assumes a spherical earth.
29   * http://docs.openlayers.org/library/spherical_mercator.html
30   * 
31   * Such service is also a type of TMS (Tile Map Service) as defined in
32   * OSGeo's wiki.
33   * http://wiki.osgeo.org/wiki/Tile_Map_Service_Specification
34   * But take care that the Y axis is inverted, ie the origin is at top-left
35   * corner.
36   * Following this specification, the protocol handled by this class
37   * follows the global-mercator profile.
38   * 
39   * You can also find many interesting information on the OSM's wiki.
40   * http://wiki.openstreetmap.org/wiki/Slippy_map_tilenames
41   * http://wiki.openstreetmap.org/wiki/Setting_up_TMS
42   */
43   
44 #ifdef HAVE_CONFIG_H
45 #include "config.h"
46 #endif
47
48 #ifdef HAVE_MATH_H
49 #include <math.h>
50 #endif
51
52 #include "globals.h"
53 #include "vikslippymapsource.h"
54
55 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
56 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
57
58 static gboolean _is_direct_file_access (VikMapSource *self );
59 static gboolean _supports_download_only_new (VikMapSource *self );
60
61 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
62 static gchar *_get_hostname( VikMapSourceDefault *self );
63 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
64
65 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
66 struct _VikSlippyMapSourcePrivate
67 {
68   gchar *hostname;
69   gchar *url;
70   DownloadMapOptions options;
71   gboolean is_direct_file_access;
72 };
73
74 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
75
76 /* properties */
77 enum
78 {
79   PROP_0,
80
81   PROP_HOSTNAME,
82   PROP_URL,
83   PROP_REFERER,
84   PROP_FOLLOW_LOCATION,
85   PROP_CHECK_FILE_SERVER_TIME,
86   PROP_USE_ETAG,
87   PROP_IS_DIRECT_FILE_ACCESS,
88 };
89
90 G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
91
92 static void
93 vik_slippy_map_source_init (VikSlippyMapSource *self)
94 {
95   /* initialize the object here */
96   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
97
98   priv->hostname = NULL;
99   priv->url = NULL;
100   priv->options.referer = NULL;
101   priv->options.follow_location = 0;
102   priv->options.check_file = a_check_map_file;
103   priv->options.check_file_server_time = FALSE;
104   priv->options.use_etag = FALSE;
105   priv->is_direct_file_access = FALSE;
106
107   g_object_set (G_OBJECT (self),
108                 "tilesize-x", 256,
109                 "tilesize-y", 256,
110                 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
111                 NULL);
112 }
113
114 static void
115 vik_slippy_map_source_finalize (GObject *object)
116 {
117   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
118   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
119
120   g_free (priv->hostname);
121   priv->hostname = NULL;
122   g_free (priv->url);
123   priv->url = NULL;
124   g_free (priv->options.referer);
125   priv->options.referer = NULL;
126
127   G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
128 }
129
130 static void
131 vik_slippy_map_source_set_property (GObject      *object,
132                                     guint         property_id,
133                                     const GValue *value,
134                                     GParamSpec   *pspec)
135 {
136   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
137   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
138
139   switch (property_id)
140     {
141     case PROP_HOSTNAME:
142       g_free (priv->hostname);
143       priv->hostname = g_value_dup_string (value);
144       break;
145
146     case PROP_URL:
147       g_free (priv->url);
148       priv->url = g_value_dup_string (value);
149       break;
150
151     case PROP_REFERER:
152       g_free (priv->options.referer);
153       priv->options.referer = g_value_dup_string (value);
154       break;
155
156     case PROP_FOLLOW_LOCATION:
157       priv->options.follow_location = g_value_get_long (value);
158       break;
159
160     case PROP_CHECK_FILE_SERVER_TIME:
161       priv->options.check_file_server_time = g_value_get_boolean (value);
162       break;
163
164     case PROP_USE_ETAG:
165       priv->options.use_etag = g_value_get_boolean (value);
166       break;
167
168     case PROP_IS_DIRECT_FILE_ACCESS:
169       priv->is_direct_file_access = g_value_get_boolean (value);
170       break;
171
172     default:
173       /* We don't have any other property... */
174       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
175       break;
176     }
177 }
178
179 static void
180 vik_slippy_map_source_get_property (GObject    *object,
181                                     guint       property_id,
182                                     GValue     *value,
183                                     GParamSpec *pspec)
184 {
185   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
186   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
187
188   switch (property_id)
189     {
190     case PROP_HOSTNAME:
191       g_value_set_string (value, priv->hostname);
192       break;
193
194     case PROP_URL:
195       g_value_set_string (value, priv->url);
196       break;
197
198     case PROP_REFERER:
199       g_value_set_string (value, priv->options.referer);
200       break;
201
202     case PROP_FOLLOW_LOCATION:
203       g_value_set_long (value, priv->options.follow_location);
204       break;
205
206     case PROP_CHECK_FILE_SERVER_TIME:
207       g_value_set_boolean (value, priv->options.check_file_server_time);
208       break;
209           
210     case PROP_USE_ETAG:
211       g_value_set_boolean (value, priv->options.use_etag);
212       break;
213
214     case PROP_IS_DIRECT_FILE_ACCESS:
215       g_value_set_boolean (value, priv->is_direct_file_access);
216       break;
217
218     default:
219       /* We don't have any other property... */
220       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
221       break;
222     }
223 }
224
225 static void
226 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
227 {
228         GObjectClass* object_class = G_OBJECT_CLASS (klass);
229         VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
230         VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
231         GParamSpec *pspec = NULL;
232                 
233         object_class->set_property = vik_slippy_map_source_set_property;
234     object_class->get_property = vik_slippy_map_source_get_property;
235
236         /* Overiding methods */
237         grandparent_class->coord_to_mapcoord =        _coord_to_mapcoord;
238         grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
239         grandparent_class->is_direct_file_access = _is_direct_file_access;
240         grandparent_class->supports_download_only_new = _supports_download_only_new;
241
242         parent_class->get_uri = _get_uri;
243         parent_class->get_hostname = _get_hostname;
244         parent_class->get_download_options = _get_download_options;
245
246         pspec = g_param_spec_string ("hostname",
247                                      "Hostname",
248                                      "The hostname of the map server",
249                                      "<no-set>" /* default value */,
250                                      G_PARAM_READWRITE);
251         g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
252
253         pspec = g_param_spec_string ("url",
254                                      "URL",
255                                      "The template of the tiles' URL",
256                                      "<no-set>" /* default value */,
257                                      G_PARAM_READWRITE);
258         g_object_class_install_property (object_class, PROP_URL, pspec);
259
260         pspec = g_param_spec_string ("referer",
261                                      "Referer",
262                                      "The REFERER string to use in HTTP request",
263                                      NULL /* default value */,
264                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
265         g_object_class_install_property (object_class, PROP_REFERER, pspec);
266         
267         pspec = g_param_spec_long ("follow-location",
268                                    "Follow location",
269                                "Specifies the number of retries to follow a redirect while downloading a page",
270                                0  /* minimum value */,
271                                G_MAXLONG /* maximum value */,
272                                0  /* default value */,
273                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
274         g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
275         
276         pspec = g_param_spec_boolean ("check-file-server-time",
277                                       "Check file server time",
278                                   "Age of current cache before redownloading tile",
279                                   FALSE  /* default value */,
280                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
281         g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
282
283         pspec = g_param_spec_boolean ("use-etag",
284                                       "Use etag values with server",
285                                   "Store etag in a file, and send it to server to check if we have the latest file",
286                                   FALSE  /* default value */,
287                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
288         g_object_class_install_property (object_class, PROP_USE_ETAG, pspec);
289
290         pspec = g_param_spec_boolean ("use-direct-file-access",
291                                       "Use direct file access",
292                                       "Use direct file access to OSM like tile images - no need for a webservice",
293                                   FALSE  /* default value */,
294                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
295         g_object_class_install_property (object_class, PROP_IS_DIRECT_FILE_ACCESS, pspec);
296
297         g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
298         
299         object_class->finalize = vik_slippy_map_source_finalize;
300 }
301
302 /* 1 << (x) is like a 2**(x) */
303 #define GZ(x) ((1<<(x)))
304
305 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),
306                                            GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
307
308 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
309
310 static const gdouble scale_neg_mpps[] = { 1.0/GZ(0), 1.0/GZ(1), 1.0/GZ(2), 1.0/GZ(3) };
311 static const gint num_scales_neg = (sizeof(scale_neg_mpps) / sizeof(scale_neg_mpps[0]));
312
313 #define ERROR_MARGIN 0.01
314 gint
315 vik_slippy_map_source_zoom_to_scale ( gdouble mpp ) {
316   gint i;
317   for ( i = 0; i < num_scales; i++ ) {
318     if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
319       return i;
320     }
321   }
322   for ( i = 0; i < num_scales_neg; i++ ) {
323     if ( ABS(scale_neg_mpps[i] - mpp) < 0.000001 ) {
324       return -i;
325     }
326   }
327
328   return 255;
329 }
330
331 gboolean
332 _is_direct_file_access (VikMapSource *self)
333 {
334   g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
335
336   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
337
338   return priv->is_direct_file_access;
339 }
340
341 gboolean
342 _supports_download_only_new (VikMapSource *self)
343 {
344   g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
345         
346   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
347         
348   return priv->options.check_file_server_time || priv->options.use_etag;
349 }
350
351 static gboolean
352 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
353 {
354   g_assert ( src->mode == VIK_COORD_LATLON );
355
356   if ( xzoom != yzoom )
357     return FALSE;
358
359   dest->scale = vik_slippy_map_source_zoom_to_scale ( xzoom );
360   if ( dest->scale == 255 )
361     return FALSE;
362
363   dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
364   dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
365   dest->z = 0;
366
367   return TRUE;
368 }
369
370 static void
371 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
372 {
373   gdouble socalled_mpp;
374   if (src->scale >= 0)
375     socalled_mpp = GZ(src->scale);
376   else
377     socalled_mpp = 1.0/GZ(-src->scale);
378   dest->mode = VIK_COORD_LATLON;
379   dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
380   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
381 }
382
383 static gchar *
384 _get_uri( VikMapSourceDefault *self, MapCoord *src )
385 {
386         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
387         
388     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
389         gchar *uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
390         return uri;
391
392
393 static gchar *
394 _get_hostname( VikMapSourceDefault *self )
395 {
396         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
397         
398     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
399         return g_strdup( priv->hostname );
400 }
401
402 static DownloadMapOptions *
403 _get_download_options( VikMapSourceDefault *self )
404 {
405         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
406         
407         VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
408         return &(priv->options);
409 }
410
411 VikSlippyMapSource *
412 vik_slippy_map_source_new_with_id (guint8 id, const gchar *label, const gchar *hostname, const gchar *url)
413 {
414         return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
415                             "id", id, "label", label, "hostname", hostname, "url", url, NULL);
416 }