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 3 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 "curl_download.h"
50 #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 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 );
72 typedef struct _BingMapSourcePrivate BingMapSourcePrivate;
73 struct _BingMapSourcePrivate
77 /* Current attribution, when parsing */
81 /* The pixbuf to store the logo */
82 static GdkPixbuf *pixbuf = NULL;
84 #define BING_MAP_SOURCE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), BING_TYPE_MAP_SOURCE, BingMapSourcePrivate))
94 G_DEFINE_TYPE (BingMapSource, bing_map_source, VIK_TYPE_SLIPPY_MAP_SOURCE);
97 bing_map_source_init (BingMapSource *self)
99 /* initialize the object here */
100 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
102 priv->api_key = NULL;
103 priv->attributions = NULL;
104 priv->attribution = NULL;
108 bing_map_source_finalize (GObject *object)
110 BingMapSource *self = BING_MAP_SOURCE (object);
111 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
113 g_free (priv->api_key);
114 priv->api_key = NULL;
116 G_OBJECT_CLASS (bing_map_source_parent_class)->finalize (object);
120 _set_property (GObject *object,
125 BingMapSource *self = BING_MAP_SOURCE (object);
126 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
131 priv->api_key = g_strdup (g_value_get_string (value));
135 /* We don't have any other property... */
136 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
142 _get_property (GObject *object,
147 BingMapSource *self = BING_MAP_SOURCE (object);
148 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
153 g_value_set_string (value, priv->api_key);
157 /* We don't have any other property... */
158 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
165 bing_map_source_class_init (BingMapSourceClass *klass)
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;
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;
179 pspec = g_param_spec_string ("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);
186 g_type_class_add_private (klass, sizeof (BingMapSourcePrivate));
188 object_class->finalize = bing_map_source_finalize;
190 pixbuf = gdk_pixbuf_from_pixdata ( &bing_maps_pixbuf, TRUE, NULL );
194 compute_quad_tree(int zoom, int tilex, int tiley)
196 /* Picked from http://trac.openstreetmap.org/browser/applications/editors/josm/plugins/slippymap/src/org/openstreetmap/josm/plugins/slippymap/SlippyMapPreferences.java?rev=24486 */
200 for(i = zoom; i > 0; i--)
203 int mask = 1 << (i - 1);
204 if ((tilex & mask) != 0) {
207 if ((tiley & mask) != 0) {
217 _get_uri( VikMapSourceDefault *self, MapCoord *src )
219 g_return_val_if_fail (BING_IS_MAP_SOURCE(self), NULL);
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");
228 static const GdkPixbuf *
229 _get_logo( VikMapSource *self )
235 _get_copyright(VikMapSource * self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
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);
240 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE(self);
242 int level = vik_slippy_map_source_zoom_to_scale (zoom);
244 /* Loop over all known attributions */
245 GList *attribution = priv->attributions;
246 if (attribution == NULL) {
247 _async_load_attributions (BING_MAP_SOURCE (self));
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);
258 attribution = attribution->next;
262 /* Called for open tags <foo bar="baz"> */
264 _start_element (GMarkupParseContext *context,
265 const gchar *element_name,
266 const gchar **attribute_names,
267 const gchar **attribute_values,
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);
282 /* Called for character data */
283 /* text is not nul-terminated */
285 _text (GMarkupParseContext *context,
291 BingMapSource *self = BING_MAP_SOURCE (user_data);
292 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
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);
300 const gchar *parent = len > 1 ? g_slist_nth_data ((GSList *)stack, 1) : NULL;
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);
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);
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);
332 _parse_file_for_attributions(BingMapSource *self, gchar *filename)
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);
340 FILE *file = g_fopen (filename, "r");
342 /* TODO emit warning */
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;
352 xml_context = g_markup_parse_context_new(&xml_parser, 0, self, NULL);
357 while (xml_context &&
358 (nb = fread (buff, sizeof(gchar), BUFSIZ, file)) > 0)
362 /* Avoid possible BOM at begining of the file */
363 offset = buff[0] == '<' ? 0 : 3;
367 if (!g_markup_parse_context_parse(xml_context, buff+offset, nb-offset, &error))
369 fprintf(stderr, "%s: parsing error: %s.\n",
370 __FUNCTION__, error->message);
371 g_markup_parse_context_free(xml_context);
374 g_clear_error (&error);
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);
384 g_markup_parse_context_free(xml_context);
392 _load_attributions ( BingMapSource *self )
398 int ret = 0; /* OK */
400 BingMapSourcePrivate *priv = BING_MAP_SOURCE_GET_PRIVATE (self);
402 if ((tmp_fd = g_file_open_tmp ("vik-bing.XXXXXX", &tmpname, NULL)) == -1) {
403 g_critical(_("couldn't open temp file"));
407 tmp_file = fdopen(tmp_fd, "r+");
408 uri = g_strdup_printf(URL_ATTR_FMT, priv->api_key);
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 */
421 g_debug("%s: %s", __FUNCTION__, tmpname);
422 if (!_parse_file_for_attributions(self, tmpname)) {
435 _emit_update ( gpointer data )
439 vik_layers_panel_emit_update ( VIK_LAYERS_PANEL (data) );
446 _load_attributions_thread ( BingMapSource *self, gpointer threaddata )
448 _load_attributions ( self );
449 int result = a_background_thread_progress ( threaddata, 1.0 );
451 return -1; /* Abort thread */
454 /* As we are on a download thread,
455 * it's better to fire the update from the main loop.
457 g_idle_add ( (GSourceFunc)_emit_update, NULL /* FIXME */ );
463 _async_load_attributions ( BingMapSource *self )
465 a_background_thread ( /*VIK_GTK_WINDOW_FROM_WIDGET(vp)*/NULL,
466 _("Bing attribution Loading"),
467 (vik_thr_func) _load_attributions_thread,
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.
481 * Constructor for Bing map source.
483 * Returns: a newly allocated BingMapSource GObject.
486 bing_map_source_new_with_id (guint8 id, const gchar *label, const gchar *key)
488 /* initialize settings here */
489 return g_object_new(BING_TYPE_MAP_SOURCE,
492 "hostname", "ecn.t2.tiles.virtualearth.net",
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",