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 *_get_uri ( VikMapSourceDefault *self, MapCoord *src );
59 static gchar *_get_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 */
84 /* The pixbuf to store the logo */
85 static GdkPixbuf *pixbuf = NULL;
87 #define BING_MAP_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BING_TYPE_MAP_SOURCE, BingMapSourcePrivate))
99 G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
102 bing_map_source_init (BingMapSource *self)
104 /* initialize the object here */
105 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
107 priv->hostname = NULL;
109 priv->api_key = NULL;
110 priv->attributions = NULL;
111 priv->attribution = NULL;
115 bing_map_source_finalize (GObject *object)
117 BingMapSource *self = BING_MAP_SOURCE (object);
118 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
120 g_free (priv->hostname);
121 priv->hostname = NULL;
124 g_free (priv->api_key);
125 priv->api_key = NULL;
127 G_OBJECT_CLASS (bing_map_source_parent_class)->finalize (object);
131 _set_property (GObject *object,
136 BingMapSource *self = BING_MAP_SOURCE (object);
137 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
142 g_free (priv->hostname);
143 priv->hostname = g_value_dup_string (value);
148 priv->url = g_value_dup_string (value);
152 priv->api_key = g_strdup (g_value_get_string (value));
156 /* We don't have any other property... */
157 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
163 _get_property (GObject *object,
168 BingMapSource *self = BING_MAP_SOURCE (object);
169 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
174 g_value_set_string (value, priv->hostname);
178 g_value_set_string (value, priv->url);
182 g_value_set_string (value, priv->api_key);
186 /* We don't have any other property... */
187 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
194 bing_map_source_class_init (BingMapSourceClass *klass)
196 GObjectClass* object_class = G_OBJECT_CLASS (klass);
197 VikMapSourceDefaultClass* grandparent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
198 VikMapSourceClass* base_class = VIK_MAP_SOURCE_CLASS (klass);
199 GParamSpec *pspec = NULL;
201 /* Overiding methods */
202 object_class->set_property = _set_property;
203 object_class->get_property = _get_property;
204 grandparent_class->get_uri = _get_uri;
205 grandparent_class->get_hostname = _get_hostname;
206 base_class->get_logo = _get_logo;
207 base_class->get_copyright = _get_copyright;
209 pspec = g_param_spec_string ("hostname",
211 "The hostname of the map server",
212 "<no-set>" /* default value */,
213 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
214 g_object_class_install_property (object_class, PROP_HOSTNAME, pspec);
216 pspec = g_param_spec_string ("url",
218 "The template of the tiles' URL",
219 "<no-set>" /* default value */,
220 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
221 g_object_class_install_property (object_class, PROP_URL, pspec);
223 pspec = g_param_spec_string ("api-key",
225 "The API key to access Bing",
226 "<no-set>" /* default value */,
227 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
228 g_object_class_install_property (object_class, PROP_API_KEY, pspec);
230 g_type_class_add_private (klass, sizeof (BingMapSourcePrivate));
232 object_class->finalize = bing_map_source_finalize;
234 pixbuf = gdk_pixbuf_from_pixdata ( &bing_maps_pixbuf, TRUE, NULL );
238 compute_quad_tree(int zoom, int tilex, int tiley)
240 /* Picked from http://trac.openstreetmap.org/browser/applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java?rev=24486 */
244 for(i = zoom; i > 0; i--)
247 int mask = 1 << (i - 1);
248 if ((tilex & mask) != 0) {
251 if ((tiley & mask) != 0) {
261 _get_uri( VikMapSourceDefault *self, MapCoord *src )
263 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
265 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
266 gchar *quadtree = compute_quad_tree (17 - src->scale, src->x, src->y);
267 gchar *uri = g_strdup_printf ( priv->url, quadtree );
273 _get_hostname( VikMapSourceDefault *self )
275 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
277 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
278 return g_strdup( priv->hostname );
281 static const GdkPixbuf *
282 _get_logo( VikMapSource *self )
288 _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
290 g_return_if_fail (BING_IS_MAP_SOURCE(self));
291 g_debug("%s: looking for %g %g %g %g at %g", __FUNCTION__, bbox.south, bbox.north, bbox.east, bbox.west, zoom);
293 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
295 int level = map_utils_mpp_to_scale (zoom);
297 /* Loop over all known attributions */
298 GList *attribution = priv->attributions;
299 if (attribution == NULL && g_strcmp0 ("<no-set>", priv->api_key)) {
300 _async_load_attributions (BING_MAP_SOURCE (self));
302 while (attribution != NULL) {
303 struct _Attribution *current = (struct _Attribution*)attribution->data;
304 /* 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); */
305 if (BBOX_INTERSECT(bbox, current->bounds) &&
306 (17 - level) > current->minZoom &&
307 (17 - level) < current->maxZoom) {
308 (*fct)(data, current->attribution);
309 g_debug("%s: found match %s", __FUNCTION__, current->attribution);
311 attribution = attribution->next;
315 /* Called for open tags <foo bar="baz"> */
317 _start_element (GMarkupParseContext *context,
318 const gchar *element_name,
319 const gchar **attribute_names,
320 const gchar **attribute_values,
324 BingMapSource *self = BING_MAP_SOURCE (user_data);
325 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
326 const gchar *element = g_markup_parse_context_get_element (context);
327 if (strcmp (element, "CoverageArea") == 0) {
328 /* New Attribution */
329 struct _Attribution *attribution = g_malloc (sizeof(struct _Attribution));
330 priv->attributions = g_list_append (priv->attributions, attribution);
331 attribution->attribution = g_strdup (priv->attribution);
335 /* Called for character data */
336 /* text is not nul-terminated */
338 _text (GMarkupParseContext *context,
344 BingMapSource *self = BING_MAP_SOURCE (user_data);
345 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
347 struct _Attribution *attribution = priv->attributions == NULL ? NULL : g_list_last (priv->attributions)->data;
348 const gchar *element = g_markup_parse_context_get_element (context);
349 gchar *textl = g_strndup (text, text_len);
350 const GSList *stack = g_markup_parse_context_get_element_stack (context);
351 int len = g_slist_length ((GSList *)stack);
353 const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
355 if (strcmp (element, "Attribution") == 0) {
356 g_free (priv->attribution);
357 priv->attribution = g_strdup (textl);
358 } else if (parent != NULL && strcmp (parent, "CoverageArea") == 0) {
359 if (strcmp (element, "ZoomMin") == 0) {
360 attribution->minZoom = atoi (textl);
361 } else if (strcmp (element, "ZoomMax") == 0) {
362 attribution->maxZoom = atoi (textl);
364 } else if (parent != NULL && strcmp (parent, "BoundingBox") == 0) {
365 if (strcmp (element, "SouthLatitude") == 0) {
366 attribution->bounds.south = g_ascii_strtod (textl, NULL);
367 } else if (strcmp (element, "WestLongitude") == 0) {
368 attribution->bounds.west = g_ascii_strtod (textl, NULL);
369 } else if (strcmp (element, "NorthLatitude") == 0) {
370 attribution->bounds.north = g_ascii_strtod (textl, NULL);
371 } else if (strcmp (element, "EastLongitude") == 0) {
372 attribution->bounds.east = g_ascii_strtod (textl, NULL);
376 g_debug("Current attribution %s from %d to %d %g %g %g %g",
377 attribution->attribution,
378 attribution->minZoom, attribution->maxZoom,
379 attribution->bounds.south, attribution->bounds.north, attribution->bounds.west, attribution->bounds.east);
385 _parse_file_for_attributions(BingMapSource *self, gchar *filename)
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);
393 FILE *file = g_fopen (filename, "r");
395 /* TODO emit warning */
398 /* setup context parse (ie callbacks) */
399 xml_parser.start_element = &_start_element;
400 xml_parser.end_element = NULL;
401 xml_parser.text = &_text;
402 xml_parser.passthrough = NULL;
403 xml_parser.error = NULL;
405 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
410 while (xml_context &&
411 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
415 /* Avoid possible BOM at begining of the file */
416 offset = buff[0] == '<' ? 0 : 3;
420 if (!g_markup_parse_context_parse(xml_context, buff+offset, nb-offset, &error))
422 fprintf(stderr, "%s: parsing error: %s.\n",
423 __FUNCTION__, error->message);
424 g_markup_parse_context_free(xml_context);
427 g_clear_error (&error);
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);
437 g_markup_parse_context_free(xml_context);
445 _load_attributions ( BingMapSource *self )
447 int ret = 0; /* OK */
449 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
450 gchar *uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
452 gchar *tmpname = a_download_uri_to_tmp_file ( uri, vik_map_source_default_get_download_options(VIK_MAP_SOURCE_DEFAULT(self)) );
454 g_debug("%s: %s", __FUNCTION__, tmpname);
455 if (!_parse_file_for_attributions(self, tmpname)) {
468 _emit_update ( gpointer data )
472 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
479 _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
481 _load_attributions ( self );
482 int result = a_background_thread_progress ( threaddata, 1.0 );
484 return -1; /* Abort thread */
487 /* As we are on a download thread,
488 * it's better to fire the update from the main loop.
490 g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
496 _async_load_attributions ( BingMapSource *self )
498 a_background_thread ( BACKGROUND_POOL_REMOTE,
499 /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
500 _("Bing attribution Loading"),
501 (vik_thr_func) _load_attributions_thread,
510 * bing_map_source_new_with_id:
511 * @id: internal identifier.
512 * @label: the label to display in map provider selector.
513 * @key: the API key to access Bing's services.
515 * Constructor for Bing map source.
517 * Returns: a newly allocated BingMapSource GObject.
520 bing_map_source_new_with_id (guint16 id, const gchar *label, const gchar *key)
522 /* initialize settings here */
523 return g_object_new(BING_TYPE_MAP_SOURCE,
526 "name", "Bing-Aerial",
527 "hostname", "ecn.t2.tiles.virtualearth.net",
528 "url", "/tiles/a%s.jpeg?g=587",
530 "check-file-server-time", TRUE,
532 "zoom-max", 19, // NB: Might be regionally different rather than the same across the world
533 "copyright", "© 2011 Microsoft Corporation and/or its suppliers",
534 "license", "Microsoft Bing Maps Specific",
535 "license-url", "http://www.microsoft.com/maps/assets/docs/terms.aspx",