]> git.street.me.uk Git - andy/viking.git/blame - src/bingmapsource.c
Fix GPX symbol names for Garmin Oregon 450
[andy/viking.git] / src / bingmapsource.c
CommitLineData
9f58c4b4
GB
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking
4 * Copyright (C) 2011, 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:bingmapsource
22 * @short_description: the class for Bing Maps
23 *
24 * The #BingMapSource class handles Bing map source.
25 *
26 * License and term of use are available here:
27 * http://wiki.openstreetmap.org/wiki/File:Bing_license.pdf
28 *
29 * Technical details are available here:
30 * http://msdn.microsoft.com/en-us/library/dd877180.aspx
31 */
32
33#ifdef HAVE_CONFIG_H
34#include "config.h"
35#endif
36
37#ifdef HAVE_MATH_H
38#include <math.h>
39#endif
40
41#ifdef HAVE_STDLIB_H
42#include <stdlib.h>
43#endif
44#include <glib.h>
45#include <glib/gstdio.h>
46#include <glib/gi18n.h>
47#include <gdk-pixbuf/gdk-pixdata.h>
48#include "globals.h"
9f58c4b4 49#include "bingmapsource.h"
e1dde2a6 50#include "maputils.h"
9f58c4b4
GB
51#include "bbox.h"
52#include "background.h"
53#include "icons/icons.h"
54
55/* Format for URL */
56#define URL_ATTR_FMT "http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial/0,0?zl=1&mapVersion=v1&key=%s&include=ImageryProviders&output=xml"
57
58static gchar *_get_uri ( VikMapSourceDefault *self, MapCoord *src );
2f806387 59static void _get_copyright (VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
9f58c4b4
GB
60static const GdkPixbuf *_get_logo ( VikMapSource *self );
61static int _load_attributions ( BingMapSource *self );
62static void _async_load_attributions ( BingMapSource *self );
63
64struct _Attribution
65{
66 gchar *attribution;
67 int minZoom;
68 int maxZoom;
69 LatLonBBox bounds;
70};
71
72typedef struct _BingMapSourcePrivate BingMapSourcePrivate;
73struct _BingMapSourcePrivate
74{
75 gchar *api_key;
76 GList *attributions;
77 /* Current attribution, when parsing */
78 gchar *attribution;
79};
80
81/* The pixbuf to store the logo */
82static GdkPixbuf *pixbuf = NULL;
83
84#define BING_MAP_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BING_TYPE_MAP_SOURCE, BingMapSourcePrivate))
85
86/* properties */
87enum
88{
89 PROP_0,
90
91 PROP_API_KEY,
92};
93
94G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
95
96static void
97bing_map_source_init (BingMapSource *self)
98{
99 /* initialize the object here */
100 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
101
102 priv->api_key = NULL;
103 priv->attributions = NULL;
104 priv->attribution = NULL;
105}
106
107static void
108bing_map_source_finalize (GObject *object)
109{
110 BingMapSource *self = BING_MAP_SOURCE (object);
111 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
112
113 g_free (priv->api_key);
114 priv->api_key = NULL;
115
116 G_OBJECT_CLASS (bing_map_source_parent_class)->finalize (object);
117}
118
119static void
120_set_property (GObject *object,
121 guint property_id,
122 const GValue *value,
123 GParamSpec *pspec)
124{
125 BingMapSource *self = BING_MAP_SOURCE (object);
126 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
127
128 switch (property_id)
129 {
130 case PROP_API_KEY:
131 priv->api_key = g_strdup (g_value_get_string (value));
132 break;
133
134 default:
135 /* We don't have any other property... */
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
137 break;
138 }
139}
140
141static void
142_get_property (GObject *object,
143 guint property_id,
144 GValue *value,
145 GParamSpec *pspec)
146{
147 BingMapSource *self = BING_MAP_SOURCE (object);
148 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
149
150 switch (property_id)
151 {
152 case PROP_API_KEY:
153 g_value_set_string (value, priv->api_key);
154 break;
155
156 default:
157 /* We don't have any other property... */
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
159 break;
160 }
161}
162
163
164static void
165bing_map_source_class_init (BingMapSourceClass *klass)
166{
167 GObjectClass* object_class = G_OBJECT_CLASS (klass);
168 VikMapSourceDefaultClass* grandparent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
169 VikMapSourceClass* base_class = VIK_MAP_SOURCE_CLASS (klass);
170 GParamSpec *pspec = NULL;
171
172 /* Overiding methods */
173 object_class->set_property = _set_property;
174 object_class->get_property = _get_property;
175 grandparent_class->get_uri = _get_uri;
176 base_class->get_logo = _get_logo;
177 base_class->get_copyright = _get_copyright;
178
179 pspec = g_param_spec_string ("api-key",
180 "API key",
181 "The API key to access Bing",
182 "<no-set>" /* default value */,
183 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
184 g_object_class_install_property (object_class, PROP_API_KEY, pspec);
185
186 g_type_class_add_private (klass, sizeof (BingMapSourcePrivate));
187
188 object_class->finalize = bing_map_source_finalize;
189
190 pixbuf = gdk_pixbuf_from_pixdata ( &bing_maps_pixbuf, TRUE, NULL );
191}
192
193static gchar *
194compute_quad_tree(int zoom, int tilex, int tiley)
195{
196 /* Picked from http://trac.openstreetmap.org/browser/applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java?rev=24486 */
197 gchar k[20];
198 int ik = 0;
199 int i = 0;
200 for(i = zoom; i > 0; i--)
201 {
202 char digit = 48;
203 int mask = 1 << (i - 1);
204 if ((tilex & mask) != 0) {
205 digit += 1;
206 }
207 if ((tiley & mask) != 0) {
208 digit += 2;
209 }
210 k[ik++] = digit;
211 }
212 k[ik] = '\0';
213 return g_strdup(k);
214}
215
216static gchar *
217_get_uri( VikMapSourceDefault *self, MapCoord *src )
218{
219 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
220
221 /* BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self); */
222 gchar *quadtree = compute_quad_tree (17 - src->scale, src->x, src->y);
223 gchar *uri = g_strdup_printf ( "/tiles/a%s.%s?g=587", quadtree, "jpeg");
224 g_free (quadtree);
225 return uri;
226}
227
228static const GdkPixbuf *
229_get_logo( VikMapSource *self )
230{
231 return pixbuf;
232}
233
234static void
2f806387 235_get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
9f58c4b4
GB
236{
237 g_return_if_fail (BING_IS_MAP_SOURCE(self));
238 g_debug("%s: looking for %g %g %g %g at %g", __FUNCTION__, bbox.south, bbox.north, bbox.east, bbox.west, zoom);
239
240 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
241
e1dde2a6 242 int level = map_utils_mpp_to_scale (zoom);
9f58c4b4
GB
243
244 /* Loop over all known attributions */
245 GList *attribution = priv->attributions;
246 if (attribution == NULL) {
247 _async_load_attributions (BING_MAP_SOURCE (self));
248 }
249 while (attribution != NULL) {
250 struct _Attribution *current = (struct _Attribution*)attribution->data;
251 /* g_debug("%s %g %g %g %g %d %d", __FUNCTION__, current->bounds.south, current->bounds.north, current->bounds.east, current->bounds.west, current->minZoom, current->maxZoom); */
252 if (BBOX_INTERSECT(bbox, current->bounds) &&
253 (17 - level) > current->minZoom &&
254 (17 - level) < current->maxZoom) {
255 (*fct)(data, current->attribution);
256 g_debug("%s: found match %s", __FUNCTION__, current->attribution);
257 }
258 attribution = attribution->next;
259 }
260}
261
262/* Called for open tags <foo bar="baz"> */
263static void
264_start_element (GMarkupParseContext *context,
265 const gchar *element_name,
266 const gchar **attribute_names,
267 const gchar **attribute_values,
268 gpointer user_data,
269 GError **error)
270{
271 BingMapSource *self = BING_MAP_SOURCE (user_data);
272 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
273 const gchar *element = g_markup_parse_context_get_element (context);
274 if (strcmp (element, "CoverageArea") == 0) {
275 /* New Attribution */
276 struct _Attribution *attribution = g_malloc (sizeof(struct _Attribution));
277 priv->attributions = g_list_append (priv->attributions, attribution);
278 attribution->attribution = g_strdup (priv->attribution);
279 }
280}
281
282/* Called for character data */
283/* text is not nul-terminated */
284static void
285_text (GMarkupParseContext *context,
286 const gchar *text,
287 gsize text_len,
288 gpointer user_data,
289 GError **error)
290{
291 BingMapSource *self = BING_MAP_SOURCE (user_data);
292 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
293
294 struct _Attribution *attribution = priv->attributions == NULL ? NULL : g_list_last (priv->attributions)->data;
295 const gchar *element = g_markup_parse_context_get_element (context);
296 gchar *textl = g_strndup (text, text_len);
297 const GSList *stack = g_markup_parse_context_get_element_stack (context);
298 int len = g_slist_length ((GSList *)stack);
299
300 const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
301
302 if (strcmp (element, "Attribution") == 0) {
303 g_free (priv->attribution);
304 priv->attribution = g_strdup (textl);
305 } else if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
306 if (strcmp (element, "ZoomMin") == 0) {
307 attribution->minZoom = atoi (textl);
308 } else if (strcmp (element, "ZoomMax") == 0) {
309 attribution->maxZoom = atoi (textl);
310 }
311 } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
312 if (strcmp (element, "SouthLatitude") == 0) {
313 attribution->bounds.south = g_ascii_strtod (textl, NULL);
314 } else if (strcmp (element, "WestLongitude") == 0) {
315 attribution->bounds.west = g_ascii_strtod (textl, NULL);
316 } else if (strcmp (element, "NorthLatitude") == 0) {
317 attribution->bounds.north = g_ascii_strtod (textl, NULL);
318 } else if (strcmp (element, "EastLongitude") == 0) {
319 attribution->bounds.east = g_ascii_strtod (textl, NULL);
320 }
321 }
322 if (attribution)
323 g_debug("Current attribution %s from %d to %d %g %g %g %g",
324 attribution->attribution,
325 attribution->minZoom, attribution->maxZoom,
326 attribution->bounds.south, attribution->bounds.north, attribution->bounds.west, attribution->bounds.east);
327
328 g_free(textl);
329}
330
331static gboolean
332_parse_file_for_attributions(BingMapSource *self, gchar *filename)
333{
334 GMarkupParser xml_parser;
335 GMarkupParseContext *xml_context = NULL;
336 GError *error = NULL;
337 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
338 g_return_val_if_fail(priv != NULL, FALSE);
339
340 FILE *file = g_fopen (filename, "r");
341 if (file == NULL)
342 /* TODO emit warning */
343 return FALSE;
344
345 /* setup context parse (ie callbacks) */
346 xml_parser.start_element = &_start_element;
347 xml_parser.end_element = NULL;
348 xml_parser.text = &_text;
349 xml_parser.passthrough = NULL;
350 xml_parser.error = NULL;
351
352 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
353
354 gchar buff[BUFSIZ];
355 size_t nb;
356 size_t offset = -1;
357 while (xml_context &&
358 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
359 {
360 if (offset == -1)
361 /* first run */
362 /* Avoid possible BOM at begining of the file */
363 offset = buff[0] == '<' ? 0 : 3;
364 else
365 /* reset offset */
366 offset = 0;
367 if (!g_markup_parse_context_parse(xml_context, buff+offset, nb-offset, &error))
368 {
369 fprintf(stderr, "%s: parsing error: %s.\n",
370 __FUNCTION__, error->message);
371 g_markup_parse_context_free(xml_context);
372 xml_context = NULL;
373 }
374 g_clear_error (&error);
375 }
376 /* cleanup */
377 if (xml_context &&
378 !g_markup_parse_context_end_parse(xml_context, &error))
379 fprintf(stderr, "%s: errors occurred while reading file: %s.\n",
380 __FUNCTION__, error->message);
381 g_clear_error (&error);
382
383 if (xml_context)
384 g_markup_parse_context_free(xml_context);
385 xml_context = NULL;
386 fclose (file);
387
388 return TRUE;
389}
390
391static int
392_load_attributions ( BingMapSource *self )
393{
9f58c4b4
GB
394 int ret = 0; /* OK */
395
396 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
e09b94fe 397 gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
9f58c4b4 398
e09b94fe 399 gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
9f58c4b4
GB
400
401 g_debug("%s: %s", __FUNCTION__, tmpname);
402 if (!_parse_file_for_attributions(self, tmpname)) {
403 ret = -1;
404 goto done;
405 }
406
407done:
408 g_free(uri);
409 g_remove(tmpname);
410 g_free(tmpname);
411 return ret;
412}
413
414static int
415_emit_update ( gpointer data )
416{
417 gdk_threads_enter();
418 /* TODO
419 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
420 */
421 gdk_threads_leave();
7407ebbc 422 return 0;
9f58c4b4
GB
423}
424
425static int
426_load_attributions_thread ( BingMapSource *self, gpointer threaddata )
427{
428 _load_attributions ( self );
429 int result = a_background_thread_progress ( threaddata, 1.0 );
430 if ( result != 0 )
431 return -1; /* Abort thread */
432
433 /* Emit update */
434 /* As we are on a download thread,
435 * it's better to fire the update from the main loop.
436 */
437 g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
438
439 return 0;
440}
441
442static void
443_async_load_attributions ( BingMapSource *self )
444{
445 a_background_thread ( /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
446 _("Bing attribution Loading"),
447 (vik_thr_func) _load_attributions_thread,
448 self,
449 NULL,
450 NULL,
451 1 );
452
453}
454
455/**
456 * bing_map_source_new_with_id:
457 * @id: internal identifier.
458 * @label: the label to display in map provider selector.
459 * @key: the API key to access Bing's services.
460 *
461 * Constructor for Bing map source.
462 *
463 * Returns: a newly allocated BingMapSource GObject.
464 */
465BingMapSource *
466bing_map_source_new_with_id (guint8 id, const gchar *label, const gchar *key)
467{
468 /* initialize settings here */
469 return g_object_new(BING_TYPE_MAP_SOURCE,
470 "id", id,
471 "label", label,
472 "hostname", "ecn.t2.tiles.virtualearth.net",
473 "api-key", key,
474 "check-file-server-time", TRUE,
475 "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
476 "license", "Microsoft Bing Maps Specific",
477 "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",
478 NULL);
479}