]> git.street.me.uk Git - andy/viking.git/blame - src/vikslippymapsource.c
Remove unnecessary subdomain in some OSM tileserver hostnames.
[andy/viking.git] / src / vikslippymapsource.c
CommitLineData
e3a90009
GB
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking
a482007a 4 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
e3a90009
GB
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 */
dfc4db8b
GB
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
e3a90009
GB
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"
8eb12ac6 53#include "vikslippymapsource.h"
e1dde2a6 54#include "maputils.h"
e3a90009
GB
55
56static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
57static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
f66ea3ec 58
2673b29d 59static gboolean _is_direct_file_access (VikMapSource *self );
0fb11294 60static gboolean _is_mbtiles (VikMapSource *self );
c81ded98 61static gboolean _supports_download_only_new (VikMapSource *self );
e3a90009 62
f66ea3ec
GB
63static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
64static gchar *_get_hostname( VikMapSourceDefault *self );
65static DownloadMapOptions *_get_download_options( VikMapSourceDefault *self );
e3a90009 66
8eb12ac6
GB
67typedef struct _VikSlippyMapSourcePrivate VikSlippyMapSourcePrivate;
68struct _VikSlippyMapSourcePrivate
69{
70 gchar *hostname;
71 gchar *url;
b529dc9c 72 DownloadMapOptions options;
2673b29d 73 gboolean is_direct_file_access;
0fb11294 74 gboolean is_mbtiles;
f8964a2b
RN
75 // Mainly for ARCGIS Tile Server URL Layout // http://help.arcgis.com/EN/arcgisserver/10.0/apis/rest/tile.html
76 gboolean switch_xy;
8eb12ac6
GB
77};
78
79#define VIK_SLIPPY_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VIK_TYPE_SLIPPY_MAP_SOURCE, VikSlippyMapSourcePrivate))
80
4e53f3dc
GB
81/* properties */
82enum
83{
84 PROP_0,
85
86 PROP_HOSTNAME,
87 PROP_URL,
c395c6a5
GB
88 PROP_REFERER,
89 PROP_FOLLOW_LOCATION,
0f08bd0d 90 PROP_CHECK_FILE_SERVER_TIME,
05dbd9ba 91 PROP_USE_ETAG,
2673b29d 92 PROP_IS_DIRECT_FILE_ACCESS,
0fb11294 93 PROP_IS_MBTILES,
f8964a2b 94 PROP_SWITCH_XY,
4e53f3dc
GB
95};
96
f6411015 97G_DEFINE_TYPE (VikSlippyMapSource, vik_slippy_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
e3a90009
GB
98
99static void
3a84e537 100vik_slippy_map_source_init (VikSlippyMapSource *self)
e3a90009 101{
9cca9d93
GB
102 /* initialize the object here */
103 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE (self);
104
105 priv->hostname = NULL;
106 priv->url = NULL;
c395c6a5
GB
107 priv->options.referer = NULL;
108 priv->options.follow_location = 0;
109 priv->options.check_file = a_check_map_file;
6693f5f9 110 priv->options.check_file_server_time = FALSE;
2673b29d
RN
111 priv->options.use_etag = FALSE;
112 priv->is_direct_file_access = FALSE;
0fb11294 113 priv->is_mbtiles = FALSE;
f8964a2b 114 priv->switch_xy = FALSE;
9cca9d93
GB
115
116 g_object_set (G_OBJECT (self),
117 "tilesize-x", 256,
118 "tilesize-y", 256,
119 "drawmode", VIK_VIEWPORT_DRAWMODE_MERCATOR,
120 NULL);
e3a90009
GB
121}
122
123static void
8eb12ac6 124vik_slippy_map_source_finalize (GObject *object)
e3a90009 125{
9cca9d93
GB
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;
c395c6a5
GB
133 g_free (priv->options.referer);
134 priv->options.referer = NULL;
e3a90009 135
9cca9d93 136 G_OBJECT_CLASS (vik_slippy_map_source_parent_class)->finalize (object);
e3a90009
GB
137}
138
4e53f3dc
GB
139static void
140vik_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
c395c6a5
GB
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
0f08bd0d 169 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 170 priv->options.check_file_server_time = g_value_get_boolean (value);
0f08bd0d
GB
171 break;
172
05dbd9ba
JJ
173 case PROP_USE_ETAG:
174 priv->options.use_etag = g_value_get_boolean (value);
175 break;
176
2673b29d
RN
177 case PROP_IS_DIRECT_FILE_ACCESS:
178 priv->is_direct_file_access = g_value_get_boolean (value);
179 break;
180
0fb11294
RN
181 case PROP_IS_MBTILES:
182 priv->is_mbtiles = g_value_get_boolean (value);
183 break;
184
f8964a2b
RN
185 case PROP_SWITCH_XY:
186 priv->switch_xy = g_value_get_boolean (value);
187 break;
188
4e53f3dc
GB
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
196static void
197vik_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
c395c6a5
GB
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
0f08bd0d 223 case PROP_CHECK_FILE_SERVER_TIME:
6693f5f9 224 g_value_set_boolean (value, priv->options.check_file_server_time);
0f08bd0d
GB
225 break;
226
05dbd9ba
JJ
227 case PROP_USE_ETAG:
228 g_value_set_boolean (value, priv->options.use_etag);
229 break;
230
2673b29d
RN
231 case PROP_IS_DIRECT_FILE_ACCESS:
232 g_value_set_boolean (value, priv->is_direct_file_access);
233 break;
234
0fb11294
RN
235 case PROP_IS_MBTILES:
236 g_value_set_boolean (value, priv->is_mbtiles);
237 break;
238
f8964a2b
RN
239 case PROP_SWITCH_XY:
240 g_value_set_boolean (value, priv->switch_xy);
241 break;
242
4e53f3dc
GB
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
e3a90009 250static void
8eb12ac6 251vik_slippy_map_source_class_init (VikSlippyMapSourceClass *klass)
e3a90009
GB
252{
253 GObjectClass* object_class = G_OBJECT_CLASS (klass);
de674329 254 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
f66ea3ec 255 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
4e53f3dc
GB
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;
e3a90009
GB
260
261 /* Overiding methods */
de674329
RN
262 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
263 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
2673b29d 264 grandparent_class->is_direct_file_access = _is_direct_file_access;
0fb11294 265 grandparent_class->is_mbtiles = _is_mbtiles;
de674329 266 grandparent_class->supports_download_only_new = _supports_download_only_new;
f66ea3ec
GB
267
268 parent_class->get_uri = _get_uri;
269 parent_class->get_hostname = _get_hostname;
270 parent_class->get_download_options = _get_download_options;
4e53f3dc
GB
271
272 pspec = g_param_spec_string ("hostname",
273 "Hostname",
274 "The hostname of the map server",
275 "<no-set>" /* default value */,
9f58c4b4 276 G_PARAM_READWRITE);
4e53f3dc
GB
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 */,
9f58c4b4 283 G_PARAM_READWRITE);
4e53f3dc 284 g_object_class_install_property (object_class, PROP_URL, pspec);
c395c6a5
GB
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);
8eb12ac6 292
c395c6a5
GB
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);
0f08bd0d 301
6693f5f9
GB
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);
0f08bd0d 307 g_object_class_install_property (object_class, PROP_CHECK_FILE_SERVER_TIME, pspec);
c395c6a5 308
05dbd9ba
JJ
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
2673b29d
RN
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
0fb11294
RN
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);
f8964a2b
RN
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
8eb12ac6
GB
337 g_type_class_add_private (klass, sizeof (VikSlippyMapSourcePrivate));
338
339 object_class->finalize = vik_slippy_map_source_finalize;
e3a90009
GB
340}
341
cf65dac0 342static gboolean
2673b29d
RN
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
0fb11294
RN
352static 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
cf65dac0 362static gboolean
c81ded98 363_supports_download_only_new (VikMapSource *self)
0f08bd0d 364{
c81ded98 365 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), FALSE);
0f08bd0d 366
c81ded98 367 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
0f08bd0d 368
c81ded98 369 return priv->options.check_file_server_time || priv->options.use_etag;
0f08bd0d 370}
f66ea3ec 371
e3a90009
GB
372static 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
e1dde2a6 380 dest->scale = map_utils_mpp_to_scale ( xzoom );
e3a90009
GB
381 if ( dest->scale == 255 )
382 return FALSE;
383
e1dde2a6
RN
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;
e3a90009
GB
386 dest->z = 0;
387
388 return TRUE;
389}
390
391static void
392_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
393{
d1302dec
JJ
394 gdouble socalled_mpp;
395 if (src->scale >= 0)
e1dde2a6 396 socalled_mpp = VIK_GZ(src->scale);
d1302dec 397 else
e1dde2a6 398 socalled_mpp = 1.0/VIK_GZ(-src->scale);
e3a90009 399 dest->mode = VIK_COORD_LATLON;
e1dde2a6
RN
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));
e3a90009
GB
402}
403
8eb12ac6 404static gchar *
f66ea3ec 405_get_uri( VikMapSourceDefault *self, MapCoord *src )
8eb12ac6
GB
406{
407 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
408
f8964a2b
RN
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
8eb12ac6
GB
419 return uri;
420}
421
422static gchar *
f66ea3ec 423_get_hostname( VikMapSourceDefault *self )
8eb12ac6
GB
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
b529dc9c 431static DownloadMapOptions *
f66ea3ec 432_get_download_options( VikMapSourceDefault *self )
8eb12ac6
GB
433{
434 g_return_val_if_fail (VIK_IS_SLIPPY_MAP_SOURCE(self), NULL);
435
c395c6a5
GB
436 VikSlippyMapSourcePrivate *priv = VIK_SLIPPY_MAP_SOURCE_PRIVATE(self);
437 return &(priv->options);
8eb12ac6
GB
438}
439
440VikSlippyMapSource *
d7e495b2 441vik_slippy_map_source_new_with_id (guint16 id, const gchar *label, const gchar *hostname, const gchar *url)
8eb12ac6 442{
4e53f3dc 443 return g_object_new(VIK_TYPE_SLIPPY_MAP_SOURCE,
db03733a 444 "id", id, "label", label, "hostname", hostname, "url", url, NULL);
c395c6a5 445}