]> git.street.me.uk Git - andy/viking.git/blob - src/vikslippymapsource.c
Allow clearing and copying of the information message in the statusbar.
[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 #include "maputils.h"
55
56 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
57 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
58
59 static gboolean _is_direct_file_access (VikMapSource *self );
60 static gboolean _is_mbtiles (VikMapSource *self );
61 static gboolean _supports_download_only_new (VikMapSource *self );
62
63 static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
64 static gchar *_get_hostname( VikMapSourceDefault *self );
65 static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
66
67 typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
68 struct _VikSlippyMapSourcePrivate
69 {
70   gchar *hostname;
71   gchar *url;
72   DownloadMapOptions options;
73   gboolean is_direct_file_access;
74   gboolean is_mbtiles;
75   // Mainly for ARCGIS Tile Server URL Layout // http://help.arcgis.com/EN/arcgisserver/10.0/apis/rest/tile.html
76   gboolean switch_xy;
77 };
78
79 #define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
80
81 /* properties */
82 enum
83 {
84   PROP_0,
85
86   PROP_HOSTNAME,
87   PROP_URL,
88   PROP_REFERER,
89   PROP_FOLLOW_LOCATION,
90   PROP_CHECK_FILE_SERVER_TIME,
91   PROP_USE_ETAG,
92   PROP_IS_DIRECT_FILE_ACCESS,
93   PROP_IS_MBTILES,
94   PROP_SWITCH_XY,
95 };
96
97 G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
98
99 static void
100 vik_slippy_map_source_init (VikSlippyMapSource *self)
101 {
102   /* initialize the object here */
103   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
104
105   priv->hostname = NULL;
106   priv->url = NULL;
107   priv->options.referer = NULL;
108   priv->options.follow_location = 0;
109   priv->options.check_file = a_check_map_file;
110   priv->options.check_file_server_time = FALSE;
111   priv->options.use_etag = FALSE;
112   priv->is_direct_file_access = FALSE;
113   priv->is_mbtiles = FALSE;
114   priv->switch_xy = FALSE;
115
116   g_object_set (G_OBJECT (self),
117                 "tilesize-x", 256,
118                 "tilesize-y", 256,
119                 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
120                 NULL);
121 }
122
123 static void
124 vik_slippy_map_source_finalize (GObject *object)
125 {
126   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
127   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
128
129   g_free (priv->hostname);
130   priv->hostname = NULL;
131   g_free (priv->url);
132   priv->url = NULL;
133   g_free (priv->options.referer);
134   priv->options.referer = NULL;
135
136   G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
137 }
138
139 static void
140 vik_slippy_map_source_set_property (GObject      *object,
141                                     guint         property_id,
142                                     const GValue *value,
143                                     GParamSpec   *pspec)
144 {
145   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
146   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
147
148   switch (property_id)
149     {
150     case PROP_HOSTNAME:
151       g_free (priv->hostname);
152       priv->hostname = g_value_dup_string (value);
153       break;
154
155     case PROP_URL:
156       g_free (priv->url);
157       priv->url = g_value_dup_string (value);
158       break;
159
160     case PROP_REFERER:
161       g_free (priv->options.referer);
162       priv->options.referer = g_value_dup_string (value);
163       break;
164
165     case PROP_FOLLOW_LOCATION:
166       priv->options.follow_location = g_value_get_long (value);
167       break;
168
169     case PROP_CHECK_FILE_SERVER_TIME:
170       priv->options.check_file_server_time = g_value_get_boolean (value);
171       break;
172
173     case PROP_USE_ETAG:
174       priv->options.use_etag = g_value_get_boolean (value);
175       break;
176
177     case PROP_IS_DIRECT_FILE_ACCESS:
178       priv->is_direct_file_access = g_value_get_boolean (value);
179       break;
180
181     case PROP_IS_MBTILES:
182       priv->is_mbtiles = g_value_get_boolean (value);
183       break;
184
185     case PROP_SWITCH_XY:
186       priv->switch_xy = g_value_get_boolean (value);
187       break;
188
189     default:
190       /* We don't have any other property... */
191       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
192       break;
193     }
194 }
195
196 static void
197 vik_slippy_map_source_get_property (GObject    *object,
198                                     guint       property_id,
199                                     GValue     *value,
200                                     GParamSpec *pspec)
201 {
202   VikSlippyMapSource *self = VIK_SLIPPY_MAP_SOURCE (object);
203   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
204
205   switch (property_id)
206     {
207     case PROP_HOSTNAME:
208       g_value_set_string (value, priv->hostname);
209       break;
210
211     case PROP_URL:
212       g_value_set_string (value, priv->url);
213       break;
214
215     case PROP_REFERER:
216       g_value_set_string (value, priv->options.referer);
217       break;
218
219     case PROP_FOLLOW_LOCATION:
220       g_value_set_long (value, priv->options.follow_location);
221       break;
222
223     case PROP_CHECK_FILE_SERVER_TIME:
224       g_value_set_boolean (value, priv->options.check_file_server_time);
225       break;
226           
227     case PROP_USE_ETAG:
228       g_value_set_boolean (value, priv->options.use_etag);
229       break;
230
231     case PROP_IS_DIRECT_FILE_ACCESS:
232       g_value_set_boolean (value, priv->is_direct_file_access);
233       break;
234
235     case PROP_IS_MBTILES:
236       g_value_set_boolean (value, priv->is_mbtiles);
237       break;
238
239     case PROP_SWITCH_XY:
240       g_value_set_boolean (value, priv->switch_xy);
241       break;
242
243     default:
244       /* We don't have any other property... */
245       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
246       break;
247     }
248 }
249
250 static void
251 vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
252 {
253         GObjectClass* object_class = G_OBJECT_CLASS (klass);
254         VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
255         VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
256         GParamSpec *pspec = NULL;
257                 
258         object_class->set_property = vik_slippy_map_source_set_property;
259     object_class->get_property = vik_slippy_map_source_get_property;
260
261         /* Overiding methods */
262         grandparent_class->coord_to_mapcoord =        _coord_to_mapcoord;
263         grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
264         grandparent_class->is_direct_file_access = _is_direct_file_access;
265         grandparent_class->is_mbtiles = _is_mbtiles;
266         grandparent_class->supports_download_only_new = _supports_download_only_new;
267
268         parent_class->get_uri = _get_uri;
269         parent_class->get_hostname = _get_hostname;
270         parent_class->get_download_options = _get_download_options;
271
272         pspec = g_param_spec_string ("hostname",
273                                      "Hostname",
274                                      "The hostname of the map server",
275                                      "<no-set>" /* default value */,
276                                      G_PARAM_READWRITE);
277         g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
278
279         pspec = g_param_spec_string ("url",
280                                      "URL",
281                                      "The template of the tiles' URL",
282                                      "<no-set>" /* default value */,
283                                      G_PARAM_READWRITE);
284         g_object_class_install_property (object_class, PROP_URL, pspec);
285
286         pspec = g_param_spec_string ("referer",
287                                      "Referer",
288                                      "The REFERER string to use in HTTP request",
289                                      NULL /* default value */,
290                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
291         g_object_class_install_property (object_class, PROP_REFERER, pspec);
292         
293         pspec = g_param_spec_long ("follow-location",
294                                    "Follow location",
295                                "Specifies the number of retries to follow a redirect while downloading a page",
296                                0  /* minimum value */,
297                                G_MAXLONG /* maximum value */,
298                                0  /* default value */,
299                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
300         g_object_class_install_property (object_class, PROP_FOLLOW_LOCATION, pspec);
301         
302         pspec = g_param_spec_boolean ("check-file-server-time",
303                                       "Check file server time",
304                                   "Age of current cache before redownloading tile",
305                                   FALSE  /* default value */,
306                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
307         g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
308
309         pspec = g_param_spec_boolean ("use-etag",
310                                       "Use etag values with server",
311                                   "Store etag in a file, and send it to server to check if we have the latest file",
312                                   FALSE  /* default value */,
313                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
314         g_object_class_install_property (object_class, PROP_USE_ETAG, pspec);
315
316         pspec = g_param_spec_boolean ("use-direct-file-access",
317                                       "Use direct file access",
318                                       "Use direct file access to OSM like tile images - no need for a webservice",
319                                   FALSE  /* default value */,
320                                   G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
321         g_object_class_install_property (object_class, PROP_IS_DIRECT_FILE_ACCESS, pspec);
322
323         pspec = g_param_spec_boolean ("is-mbtiles",
324                                       "Is an SQL MBTiles File",
325                                       "Use an SQL MBTiles File for the tileset - no need for a webservice",
326                                       FALSE  /* default value */,
327                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
328         g_object_class_install_property (object_class, PROP_IS_MBTILES, pspec);
329
330         pspec = g_param_spec_boolean ("switch-xy",
331                                       "Switch the order of x,y components in the URL",
332                                       "Switch the order of x,y components in the URL (such as used by ARCGIS Tile Server",
333                                       FALSE  /* default value */,
334                                       G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
335         g_object_class_install_property (object_class, PROP_SWITCH_XY, pspec);
336
337         g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
338         
339         object_class->finalize = vik_slippy_map_source_finalize;
340 }
341
342 static gboolean
343 _is_direct_file_access (VikMapSource *self)
344 {
345   g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
346
347   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
348
349   return priv->is_direct_file_access;
350 }
351
352 static gboolean
353 _is_mbtiles (VikMapSource *self)
354 {
355   g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
356
357   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
358
359   return priv->is_mbtiles;
360 }
361
362 static gboolean
363 _supports_download_only_new (VikMapSource *self)
364 {
365   g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
366         
367   VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
368         
369   return priv->options.check_file_server_time || priv->options.use_etag;
370 }
371
372 static gboolean
373 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
374 {
375   g_assert ( src->mode == VIK_COORD_LATLON );
376
377   if ( xzoom != yzoom )
378     return FALSE;
379
380   dest->scale = map_utils_mpp_to_scale ( xzoom );
381   if ( dest->scale == 255 )
382     return FALSE;
383
384   dest->x = (src->east_west + 180) / 360 * VIK_GZ(17) / xzoom;
385   dest->y = (180 - MERCLAT(src->north_south)) / 360 * VIK_GZ(17) / xzoom;
386   dest->z = 0;
387
388   return TRUE;
389 }
390
391 static void
392 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
393 {
394   gdouble socalled_mpp;
395   if (src->scale >= 0)
396     socalled_mpp = VIK_GZ(src->scale);
397   else
398     socalled_mpp = 1.0/VIK_GZ(-src->scale);
399   dest->mode = VIK_COORD_LATLON;
400   dest->east_west = ((src->x+0.5) / VIK_GZ(17) * socalled_mpp * 360) - 180;
401   dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / VIK_GZ(17) * socalled_mpp * 360));
402 }
403
404 static gchar *
405 _get_uri( VikMapSourceDefault *self, MapCoord *src )
406 {
407         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
408         
409         VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
410
411         gchar *uri = NULL;
412         if ( priv->switch_xy )
413                 // 'ARC GIS' Tile Server layout ordering
414                 uri = g_strdup_printf (priv->url, 17 - src->scale, src->y, src->x);
415         else
416                 // (Default) Standard OSM Tile Server layout ordering
417                 uri = g_strdup_printf (priv->url, 17 - src->scale, src->x, src->y);
418
419         return uri;
420
421
422 static gchar *
423 _get_hostname( VikMapSourceDefault *self )
424 {
425         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
426         
427     VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
428         return g_strdup( priv->hostname );
429 }
430
431 static DownloadMapOptions *
432 _get_download_options( VikMapSourceDefault *self )
433 {
434         g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
435         
436         VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
437         return &(priv->options);
438 }
439
440 VikSlippyMapSource *
441 vik_slippy_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
442 {
443         return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
444                             "id", id, "label", label, "hostname", hostname, "url", url, NULL);
445 }