1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) 2011, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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/>.
21 * SECTION:bingmapsource
22 * @short_description: the class for Bing Maps
24 * The #BingMapSource class handles Bing map source.
26 * License and term of use are available here:
27 * http://wiki.openstreetmap.org/wiki/File:Bing_license.pdf
29 * Technical details are available here:
30 * http://msdn.microsoft.com/en-us/library/dd877180.aspx
45 #include <glib/gstdio.h>
46 #include <glib/gi18n.h>
47 #include <gdk-pixbuf/gdk-pixdata.h>
49 #include "bingmapsource.h"
52 #include "background.h"
53 #include "icons/icons.h"
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"
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 );
73 typedef struct _BingMapSourcePrivate BingMapSourcePrivate;
74 struct _BingMapSourcePrivate
80 /* Current attribution, when parsing */
82 gboolean loading_attributions;
85 /* The pixbuf to store the logo */
86 static GdkPixbuf *pixbuf = NULL;
88 #define BING_MAP_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BING_TYPE_MAP_SOURCE, BingMapSourcePrivate))
100 G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
103 bing_map_source_init (BingMapSource *self)
105 /* initialize the object here */
106 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
108 priv->hostname = NULL;
110 priv->api_key = NULL;
111 priv->attributions = NULL;
112 priv->attribution = NULL;
113 priv->loading_attributions = FALSE;
117 bing_map_source_finalize (GObject *object)
119 BingMapSource *self = BING_MAP_SOURCE (object);
120 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
122 g_free (priv->hostname);
123 priv->hostname = NULL;
126 g_free (priv->api_key);
127 priv->api_key = NULL;
129 G_OBJECT_CLASS (bing_map_source_parent_class)->finalize (object);
133 _set_property (GObject *object,
138 BingMapSource *self = BING_MAP_SOURCE (object);
139 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
144 g_free (priv->hostname);
145 priv->hostname = g_value_dup_string (value);
150 priv->url = g_value_dup_string (value);
154 priv->api_key = g_strdup (g_value_get_string (value));
158 /* We don't have any other property... */
159 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
165 _get_property (GObject *object,
170 BingMapSource *self = BING_MAP_SOURCE (object);
171 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
176 g_value_set_string (value, priv->hostname);
180 g_value_set_string (value, priv->url);
184 g_value_set_string (value, priv->api_key);
188 /* We don't have any other property... */
189 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
196 bing_map_source_class_init (BingMapSourceClass *klass)
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;
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;
211 pspec = g_param_spec_string ("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);
218 pspec = g_param_spec_string ("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);
225 pspec = g_param_spec_string ("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);
232 g_type_class_add_private (klass, sizeof (BingMapSourcePrivate));
234 object_class->finalize = bing_map_source_finalize;
236 pixbuf = gdk_pixbuf_from_pixdata ( &bing_maps_pixbuf, TRUE, NULL );
240 compute_quad_tree(int zoom, int tilex, int tiley)
242 /* Picked from http://trac.openstreetmap.org/browser/applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java?rev=24486 */
246 for(i = zoom; i > 0; i--)
249 int mask = 1 << (i - 1);
250 if ((tilex & mask) != 0) {
253 if ((tiley & mask) != 0) {
263 bget_uri( VikMapSourceDefault *self, MapCoord *src )
265 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
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 );
275 bget_hostname( VikMapSourceDefault *self )
277 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
279 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
280 return g_strdup( priv->hostname );
283 static const GdkPixbuf *
284 _get_logo( VikMapSource *self )
290 _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
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);
295 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
297 int level = map_utils_mpp_to_scale (zoom);
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));
305 // Wait until attributions loaded before processing them
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);
317 attribution = attribution->next;
321 /* Called for open tags <foo bar="baz"> */
323 bstart_element (GMarkupParseContext *context,
324 const gchar *element_name,
325 const gchar **attribute_names,
326 const gchar **attribute_values,
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);
341 /* Called for character data */
342 /* text is not nul-terminated */
344 btext (GMarkupParseContext *context,
350 BingMapSource *self = BING_MAP_SOURCE (user_data);
351 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
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);
359 const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
360 if (strcmp (element, "Attribution") == 0) {
361 g_free (priv->attribution);
362 priv->attribution = g_strdup (textl);
366 if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
367 if (strcmp (element, "ZoomMin") == 0) {
368 attribution->minZoom = atoi (textl);
369 } else if (strcmp (element, "ZoomMax") == 0) {
370 attribution->maxZoom = atoi (textl);
372 } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
373 if (strcmp (element, "SouthLatitude") == 0) {
374 attribution->bounds.south = g_ascii_strtod (textl, NULL);
375 } else if (strcmp (element, "WestLongitude") == 0) {
376 attribution->bounds.west = g_ascii_strtod (textl, NULL);
377 } else if (strcmp (element, "NorthLatitude") == 0) {
378 attribution->bounds.north = g_ascii_strtod (textl, NULL);
379 } else if (strcmp (element, "EastLongitude") == 0) {
380 attribution->bounds.east = g_ascii_strtod (textl, NULL);
389 _parse_file_for_attributions(BingMapSource *self, gchar *filename)
391 GMarkupParser xml_parser;
392 GMarkupParseContext *xml_context = NULL;
393 GError *error = NULL;
394 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
395 g_return_val_if_fail(priv != NULL, FALSE);
397 FILE *file = g_fopen (filename, "r");
399 /* TODO emit warning */
402 /* setup context parse (ie callbacks) */
403 xml_parser.start_element = &bstart_element;
404 xml_parser.end_element = NULL;
405 xml_parser.text = &btext;
406 xml_parser.passthrough = NULL;
407 xml_parser.error = NULL;
409 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
414 while (xml_context &&
415 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
419 /* Avoid possible BOM at begining of the file */
420 offset = buff[0] == '<' ? 0 : 3;
424 if (!g_markup_parse_context_parse(xml_context, buff+offset, nb-offset, &error))
426 fprintf(stderr, "%s: parsing error: %s.\n",
427 __FUNCTION__, error->message);
428 g_markup_parse_context_free(xml_context);
431 g_clear_error (&error);
435 !g_markup_parse_context_end_parse(xml_context, &error))
436 fprintf(stderr, "%s: errors occurred while reading file: %s.\n",
437 __FUNCTION__, error->message);
438 g_clear_error (&error);
441 g_markup_parse_context_free(xml_context);
446 GList *attribution = priv->attributions;
447 while (attribution != NULL) {
448 struct _Attribution *aa = (struct _Attribution*)attribution->data;
449 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);
450 attribution = attribution->next;
458 _load_attributions ( BingMapSource *self )
460 int ret = 0; /* OK */
462 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
463 priv->loading_attributions = TRUE;
464 gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
466 gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
468 g_debug("%s: %s", __FUNCTION__, tmpname);
469 if (!_parse_file_for_attributions(self, tmpname)) {
475 priv->loading_attributions = FALSE;
477 (void)g_remove(tmpname);
483 _emit_update ( gpointer data )
487 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
494 _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
496 _load_attributions ( self );
497 int result = a_background_thread_progress ( threaddata, 1.0 );
499 return -1; /* Abort thread */
502 /* As we are on a download thread,
503 * it's better to fire the update from the main loop.
505 g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
511 _async_load_attributions ( BingMapSource *self )
513 a_background_thread ( BACKGROUND_POOL_REMOTE,
514 /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
515 _("Bing attribution Loading"),
516 (vik_thr_func) _load_attributions_thread,
525 * bing_map_source_new_with_id:
526 * @id: internal identifier.
527 * @label: the label to display in map provider selector.
528 * @key: the API key to access Bing's services.
530 * Constructor for Bing map source.
532 * Returns: a newly allocated BingMapSource GObject.
535 bing_map_source_new_with_id (guint16 id, const gchar *label, const gchar *key)
537 /* initialize settings here */
538 return g_object_new(BING_TYPE_MAP_SOURCE,
541 "name", "Bing-Aerial",
542 "hostname", "ecn.t2.tiles.virtualearth.net",
543 "url", "/tiles/a%s.jpeg?g=587",
545 "check-file-server-time", TRUE,
547 "zoom-max", 19, // NB: Might be regionally different rather than the same across the world
548 "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
549 "license", "Microsoft Bing Maps Specific",
550 "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",