]> git.street.me.uk Git - andy/viking.git/blob - src/bingmapsource.c
Replace 'Add Track' tool with 'Create Route' tool.
[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 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"
49 #include "curl_download.h"
50 #include "bingmapsource.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 *_get_uri ( VikMapSourceDefault *self, MapCoord *src );
59 static void _get_copyright (VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data);
60 static const GdkPixbuf *_get_logo ( VikMapSource *self );
61 static int _load_attributions ( BingMapSource *self );
62 static void _async_load_attributions ( BingMapSource *self );
63
64 struct _Attribution
65 {
66         gchar *attribution;
67         int minZoom;
68         int maxZoom;
69         LatLonBBox bounds;
70 };
71
72 typedef struct _BingMapSourcePrivate BingMapSourcePrivate;
73 struct _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 */
82 static 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 */
87 enum
88 {
89         PROP_0,
90
91         PROP_API_KEY,
92 };
93
94 G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
95
96 static void
97 bing_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
107 static void
108 bing_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
119 static 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
141 static 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
164 static void
165 bing_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
193 static gchar *
194 compute_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
216 static 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
228 static const GdkPixbuf *
229 _get_logo( VikMapSource *self )
230 {
231         return pixbuf;
232 }
233
234 static void
235 _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
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
242         int level = vik_slippy_map_source_zoom_to_scale (zoom);
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"> */
263 static 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 */
284 static 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
331 static 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
391 static int
392 _load_attributions ( BingMapSource *self )
393 {
394         FILE *tmp_file;
395         int tmp_fd;
396         gchar *tmpname;
397         gchar *uri;
398         int ret = 0;  /* OK */
399
400         BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
401
402         if ((tmp_fd = g_file_open_tmp ("vik-bing.XXXXXX", &tmpname, NULL)) == -1) {
403                 g_critical(_("couldn't open temp file"));
404                 return -1;
405         }
406
407         tmp_file = fdopen(tmp_fd, "r+");
408         uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
409
410         /* TODO: curl may not be available */
411         if (curl_download_uri(uri, tmp_file, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)), 0, NULL)) {  /* error */
412                 fclose(tmp_file);
413                 tmp_file = NULL;
414                 ret = -1;
415                 goto done;
416         }
417
418         fclose(tmp_file);
419         tmp_file = NULL;
420
421         g_debug("%s: %s", __FUNCTION__, tmpname);
422         if (!_parse_file_for_attributions(self, tmpname)) {
423                 ret = -1;
424                 goto done;
425         }
426
427 done:
428         g_free(uri);
429         g_remove(tmpname);
430         g_free(tmpname);
431         return ret;
432 }
433
434 static int
435 _emit_update ( gpointer data )
436 {
437         gdk_threads_enter();
438         /* TODO
439         vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
440         */
441         gdk_threads_leave();
442         return 0;
443 }
444
445 static int
446 _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
447 {
448         _load_attributions ( self );
449         int result = a_background_thread_progress ( threaddata, 1.0 );
450         if ( result != 0 )
451                 return -1; /* Abort thread */
452
453         /* Emit update */
454         /* As we are on a download thread,
455          * it's better to fire the update from the main loop.
456          */
457         g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
458
459         return 0;
460 }
461
462 static void
463 _async_load_attributions ( BingMapSource *self )
464 {
465         a_background_thread ( /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
466                             _("Bing attribution Loading"),
467                             (vik_thr_func) _load_attributions_thread,
468                             self,
469                             NULL,
470                             NULL,
471                             1 );
472      
473 }
474
475 /**
476  * bing_map_source_new_with_id:
477  * @id: internal identifier.
478  * @label: the label to display in map provider selector.
479  * @key: the API key to access Bing's services.
480  *
481  * Constructor for Bing map source.
482  *
483  * Returns: a newly allocated BingMapSource GObject.
484  */
485 BingMapSource *
486 bing_map_source_new_with_id (guint8 id, const gchar *label, const gchar *key)
487 {
488         /* initialize settings here */
489         return g_object_new(BING_TYPE_MAP_SOURCE,
490                             "id", id,
491                                                 "label", label,
492                                                 "hostname", "ecn.t2.tiles.virtualearth.net",
493                                                 "api-key", key,
494                                                 "check-file-server-time", TRUE,
495                                                 "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
496                                                 "license", "Microsoft Bing Maps Specific",
497                                                 "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",
498                                                 NULL);
499 }