]> git.street.me.uk Git - andy/viking.git/blob - src/bingmapsource.c
Red Hat Bugzilla – Bug 1210403: Only download and process one Bing attribution list.
[andy/viking.git] / src / bingmapsource.c
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 2 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"
49 #include "bingmapsource.h"
50 #include "maputils.h"
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
58 static gchar *bget_uri ( VikMapSourceDefault *self, MapCoord *src );
59 static gchar *bget_hostname ( VikMapSourceDefault *self );
60 static void _get_copyright (VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
61 static const GdkPixbuf *_get_logo ( VikMapSource *self );
62 static int _load_attributions ( BingMapSource *self );
63 static void _async_load_attributions ( BingMapSource *self );
64
65 struct _Attribution
66 {
67         gchar *attribution;
68         int minZoom;
69         int maxZoom;
70         LatLonBBox bounds;
71 };
72
73 typedef struct _BingMapSourcePrivate BingMapSourcePrivate;
74 struct _BingMapSourcePrivate
75 {
76         gchar *hostname;
77         gchar *url;
78         gchar *api_key;
79         GList *attributions;
80         /* Current attribution, when parsing */
81         gchar *attribution;
82         gboolean loading_attributions;
83 };
84
85 /* The pixbuf to store the logo */
86 static GdkPixbuf *pixbuf = NULL;
87
88 #define BING_MAP_SOURCE_GET_PRIVATE(o)  (G_TYPE_INSTANCE_GET_PRIVATE ((o), BING_TYPE_MAP_SOURCE, BingMapSourcePrivate))
89
90 /* properties */
91 enum
92 {
93         PROP_0,
94
95         PROP_HOSTNAME,
96         PROP_URL,
97         PROP_API_KEY,
98 };
99
100 G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
101
102 static void
103 bing_map_source_init (BingMapSource *self)
104 {
105         /* initialize the object here */
106         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
107
108         priv->hostname = NULL;
109         priv->url = NULL;
110         priv->api_key = NULL;
111         priv->attributions = NULL;
112         priv->attribution = NULL;
113         priv->loading_attributions = FALSE;
114 }
115
116 static void
117 bing_map_source_finalize (GObject *object)
118 {
119         BingMapSource *self = BING_MAP_SOURCE (object);
120         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
121
122         g_free (priv->hostname);
123         priv->hostname = NULL;
124         g_free (priv->url);
125         priv->url = NULL;
126         g_free (priv->api_key);
127         priv->api_key = NULL;
128
129         G_OBJECT_CLASS (bing_map_source_parent_class)->finalize (object);
130 }
131
132 static void
133 _set_property (GObject      *object,
134                guint         property_id,
135                const GValue *value,
136                GParamSpec   *pspec)
137 {
138         BingMapSource *self = BING_MAP_SOURCE (object);
139         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
140
141         switch (property_id)
142         {
143         case PROP_HOSTNAME:
144                 g_free (priv->hostname);
145                 priv->hostname = g_value_dup_string (value);
146                 break;
147
148         case PROP_URL:
149                 g_free (priv->url);
150                 priv->url = g_value_dup_string (value);
151                 break;
152
153         case PROP_API_KEY:
154                 priv->api_key = g_strdup (g_value_get_string (value));
155                 break;
156
157         default:
158                 /* We don't have any other property... */
159                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
160                 break;
161         }
162 }
163
164 static void
165 _get_property (GObject    *object,
166                guint       property_id,
167                GValue     *value,
168                GParamSpec *pspec)
169 {
170         BingMapSource *self = BING_MAP_SOURCE (object);
171         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
172
173         switch (property_id)
174         {
175         case PROP_HOSTNAME:
176                 g_value_set_string (value, priv->hostname);
177                 break;
178
179         case PROP_URL:
180                 g_value_set_string (value, priv->url);
181                 break;
182
183         case PROP_API_KEY:
184                 g_value_set_string (value, priv->api_key);
185                 break;
186
187         default:
188                 /* We don't have any other property... */
189                 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
190                 break;
191         }
192 }
193
194
195 static void
196 bing_map_source_class_init (BingMapSourceClass *klass)
197 {
198         GObjectClass* object_class = G_OBJECT_CLASS (klass);
199         VikMapSourceDefaultClass* grandparent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
200         VikMapSourceClass* base_class = VIK_MAP_SOURCE_CLASS (klass);
201         GParamSpec *pspec = NULL;
202
203         /* Overiding methods */
204         object_class->set_property = _set_property;
205         object_class->get_property = _get_property;
206         grandparent_class->get_uri = bget_uri;
207         grandparent_class->get_hostname = bget_hostname;
208         base_class->get_logo      = _get_logo;
209         base_class->get_copyright = _get_copyright;
210
211         pspec = g_param_spec_string ("hostname",
212                                      "Hostname",
213                                      "The hostname of the map server",
214                                      "<no-set>" /* default value */,
215                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
216         g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
217
218         pspec = g_param_spec_string ("url",
219                                      "URL",
220                                      "The template of the tiles' URL",
221                                      "<no-set>" /* default value */,
222                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
223         g_object_class_install_property (object_class, PROP_URL, pspec);
224
225         pspec = g_param_spec_string ("api-key",
226                                      "API key",
227                                      "The API key to access Bing",
228                                      "<no-set>" /* default value */,
229                                      G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
230         g_object_class_install_property (object_class, PROP_API_KEY, pspec);
231
232         g_type_class_add_private (klass, sizeof (BingMapSourcePrivate));
233         
234         object_class->finalize = bing_map_source_finalize;
235
236         pixbuf = gdk_pixbuf_from_pixdata ( &bing_maps_pixbuf, TRUE, NULL );
237 }
238
239 static gchar *
240 compute_quad_tree(int zoom, int tilex, int tiley)
241 {
242         /* Picked from http://trac.openstreetmap.org/browser/applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java?rev=24486 */
243         gchar k[20];
244         int ik = 0;
245         int i = 0;
246         for(i = zoom; i > 0; i--)
247         {
248                 char digit = 48;
249                 int mask = 1 << (i - 1);
250                 if ((tilex & mask) != 0) {
251                         digit += 1;
252                 }
253                 if ((tiley & mask) != 0) {
254                         digit += 2;
255                 }
256                 k[ik++] = digit;
257         }
258         k[ik] = '\0';
259         return g_strdup(k);
260 }
261
262 static gchar *
263 bget_uri( VikMapSourceDefault *self, MapCoord *src )
264 {
265         g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
266
267         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
268         gchar *quadtree = compute_quad_tree (17 - src->scale, src->x, src->y);
269         gchar *uri = g_strdup_printf ( priv->url, quadtree );
270         g_free (quadtree);
271         return uri;
272
273
274 static gchar *
275 bget_hostname( VikMapSourceDefault *self )
276 {
277         g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
278
279         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
280         return g_strdup( priv->hostname );
281 }
282
283 static const GdkPixbuf *
284 _get_logo( VikMapSource *self )
285 {
286         return pixbuf;
287 }
288
289 static void
290 _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
291 {
292         g_return_if_fail (BING_IS_MAP_SOURCE(self));
293         g_debug("%s: looking for %g %g %g %g at %g", __FUNCTION__, bbox.south, bbox.north, bbox.east, bbox.west, zoom);
294
295         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
296
297         int level = map_utils_mpp_to_scale (zoom);
298
299         /* Loop over all known attributions */
300         GList *attribution = priv->attributions;
301         if (attribution == NULL && g_strcmp0 ("<no-set>", priv->api_key)) {
302                 if ( ! priv->loading_attributions )
303                         _async_load_attributions (BING_MAP_SOURCE (self));
304                 else
305                         // Wait until attributions loaded before processing them
306                         return;
307         }
308         while (attribution != NULL) {
309                 struct _Attribution *current = (struct _Attribution*)attribution->data;
310                 /* 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); */
311                 if (BBOX_INTERSECT(bbox, current->bounds) &&
312                     (17 - level) > current->minZoom &&
313                     (17 - level) < current->maxZoom) {
314                         (*fct)(data, current->attribution);
315                         g_debug("%s: found match %s", __FUNCTION__, current->attribution);
316                 }
317                 attribution = attribution->next;
318         }
319 }
320
321 /* Called for open tags <foo bar="baz"> */
322 static void
323 bstart_element (GMarkupParseContext *context,
324                 const gchar         *element_name,
325                 const gchar        **attribute_names,
326                 const gchar        **attribute_values,
327                 gpointer             user_data,
328                 GError             **error)
329 {
330         BingMapSource *self = BING_MAP_SOURCE (user_data);
331         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
332         const gchar *element = g_markup_parse_context_get_element (context);
333         if (strcmp (element, "CoverageArea") == 0) {
334                 /* New Attribution */
335                 struct _Attribution *attribution = g_malloc0 (sizeof(struct _Attribution));
336                 priv->attributions = g_list_append (priv->attributions, attribution);
337                 attribution->attribution = g_strdup (priv->attribution);
338         }
339 }
340
341 /* Called for character data */
342 /* text is not nul-terminated */
343 static void
344 btext (GMarkupParseContext *context,
345        const gchar         *text,
346        gsize                text_len,  
347        gpointer             user_data,
348        GError             **error)
349 {
350         BingMapSource *self = BING_MAP_SOURCE (user_data);
351         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
352
353         struct _Attribution *attribution = priv->attributions == NULL ? NULL : g_list_last (priv->attributions)->data;
354         const gchar *element = g_markup_parse_context_get_element (context);
355         gchar *textl = g_strndup (text, text_len);
356         const GSList *stack = g_markup_parse_context_get_element_stack (context);
357         int len = g_slist_length ((GSList *)stack);
358
359         const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
360         
361         if (strcmp (element, "Attribution") == 0) {
362                 g_free (priv->attribution);
363                 priv->attribution = g_strdup (textl);
364         } else if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
365                 if (strcmp (element, "ZoomMin") == 0) {
366                         attribution->minZoom = atoi (textl);
367                 } else if (strcmp (element, "ZoomMax") == 0) {
368                         attribution->maxZoom = atoi (textl);
369                 }
370         } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
371                 if (strcmp (element, "SouthLatitude") == 0) {
372                         attribution->bounds.south = g_ascii_strtod (textl, NULL);
373                 } else if (strcmp (element, "WestLongitude") == 0) {
374                         attribution->bounds.west = g_ascii_strtod (textl, NULL);
375                 } else if (strcmp (element, "NorthLatitude") == 0) {
376                         attribution->bounds.north = g_ascii_strtod (textl, NULL);
377                 } else if (strcmp (element, "EastLongitude") == 0) {
378                         attribution->bounds.east = g_ascii_strtod (textl, NULL);
379                 }
380         }
381         g_free(textl);
382 }
383
384 static gboolean
385 _parse_file_for_attributions(BingMapSource *self, gchar *filename)
386 {
387         GMarkupParser xml_parser;
388         GMarkupParseContext *xml_context = NULL;
389         GError *error = NULL;
390         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
391         g_return_val_if_fail(priv != NULL, FALSE);
392
393         FILE *file = g_fopen (filename, "r");
394         if (file == NULL)
395                 /* TODO emit warning */
396                 return FALSE;
397         
398         /* setup context parse (ie callbacks) */
399         xml_parser.start_element = &bstart_element;
400         xml_parser.end_element = NULL;
401         xml_parser.text = &btext;
402         xml_parser.passthrough = NULL;
403         xml_parser.error = NULL;
404         
405         xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
406
407         gchar buff[BUFSIZ];
408         size_t nb;
409         size_t offset = -1;
410         while (xml_context &&
411                (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
412         {
413                 if (offset == -1)
414                         /* first run */
415                         /* Avoid possible BOM at begining of the file */
416                         offset = buff[0] == '<' ? 0 : 3;
417                 else
418                         /* reset offset */
419                         offset = 0;
420                 if (!g_markup_parse_context_parse(xml_context, buff+offset, nb-offset, &error))
421                 {
422                         fprintf(stderr, "%s: parsing error: %s.\n",
423                                 __FUNCTION__, error->message);
424                         g_markup_parse_context_free(xml_context);
425                         xml_context = NULL;
426                 }
427                 g_clear_error (&error);
428         }
429         /* cleanup */
430         if (xml_context &&
431             !g_markup_parse_context_end_parse(xml_context, &error))
432                 fprintf(stderr, "%s: errors occurred while reading file: %s.\n",
433                         __FUNCTION__, error->message);
434         g_clear_error (&error);
435         
436         if (xml_context)
437                 g_markup_parse_context_free(xml_context);
438         xml_context = NULL;
439         fclose (file);
440
441         if (vik_debug) {
442                 GList *attribution = priv->attributions;
443                 while (attribution != NULL) {
444                         struct _Attribution *aa = (struct _Attribution*)attribution->data;
445                         g_debug ("Bing Attribution: %s from %d to %d %g %g %g %g", aa->attribution, aa->minZoom, aa->maxZoom, aa->bounds.south, aa->bounds.north, aa->bounds.east, aa->bounds.west);
446                         attribution = attribution->next;
447                 }
448         }
449
450         return TRUE;
451 }
452
453 static int
454 _load_attributions ( BingMapSource *self )
455 {
456         int ret = 0;  /* OK */
457
458         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
459         priv->loading_attributions = TRUE;
460         gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
461
462         gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
463
464         g_debug("%s: %s", __FUNCTION__, tmpname);
465         if (!_parse_file_for_attributions(self, tmpname)) {
466                 ret = -1;
467                 goto done;
468         }
469
470 done:
471         priv->loading_attributions = FALSE;
472         g_free(uri);
473         g_remove(tmpname);
474         g_free(tmpname);
475         return ret;
476 }
477
478 static int
479 _emit_update ( gpointer data )
480 {
481         gdk_threads_enter();
482         /* TODO
483         vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
484         */
485         gdk_threads_leave();
486         return 0;
487 }
488
489 static int
490 _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
491 {
492         _load_attributions ( self );
493         int result = a_background_thread_progress ( threaddata, 1.0 );
494         if ( result != 0 )
495                 return -1; /* Abort thread */
496
497         /* Emit update */
498         /* As we are on a download thread,
499          * it's better to fire the update from the main loop.
500          */
501         g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
502
503         return 0;
504 }
505
506 static void
507 _async_load_attributions ( BingMapSource *self )
508 {
509         a_background_thread ( BACKGROUND_POOL_REMOTE,
510                               /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
511                               _("Bing attribution Loading"),
512                               (vik_thr_func) _load_attributions_thread,
513                               self,
514                               NULL,
515                               NULL,
516                               1 );
517      
518 }
519
520 /**
521  * bing_map_source_new_with_id:
522  * @id: internal identifier.
523  * @label: the label to display in map provider selector.
524  * @key: the API key to access Bing's services.
525  *
526  * Constructor for Bing map source.
527  *
528  * Returns: a newly allocated BingMapSource GObject.
529  */
530 BingMapSource *
531 bing_map_source_new_with_id (guint16 id, const gchar *label, const gchar *key)
532 {
533         /* initialize settings here */
534         return g_object_new(BING_TYPE_MAP_SOURCE,
535                             "id", id,
536                                                 "label", label,
537                                                 "name", "Bing-Aerial",
538                                                 "hostname", "ecn.t2.tiles.virtualearth.net",
539                                                 "url", "/tiles/a%s.jpeg?g=587",
540                                                 "api-key", key,
541                                                 "check-file-server-time", TRUE,
542                                                 "zoom-min", 0,
543                                                 "zoom-max", 19, // NB: Might be regionally different rather than the same across the world
544                                                 "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
545                                                 "license", "Microsoft Bing Maps Specific",
546                                                 "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",
547                                                 NULL);
548 }