champlain-memphis-renderer.c 20.2 KB
Newer Older
1 2
/*
 * Copyright (C) 2009 Simon Wenner <simon@wenner.ch>
Jiří Techet's avatar
Jiří Techet committed
3
 * Copyright (C) 2010-2013 Jiri Techet <techet@gmail.com>
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

Jiří Techet's avatar
Jiří Techet committed
20 21 22
/**
 * SECTION:champlain-memphis-renderer
 * @short_description: A renderer that renders tiles from OSM vector XML data
23 24 25
 *
 * The #ChamplainMemphisRenderer uses the rendering library
 * <ulink role="online-location" url="https://trac.openstreetmap.ch/trac/memphis/">
Jiří Techet's avatar
Jiří Techet committed
26
 * LibMemphis</ulink> to render tiles based on <ulink role="online-location" url="http://www.openstreetmap.org/">
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
 * OpenStreetMap</ulink> data. Tiles are rendered in separate threads.
 * It supports zoom levels 12 to 18.
 *
 * The output of the renderer can be configured with a Memphis rules XML file.
 * (TODO: link to the specification) The default rules only show
 * highways as thin black lines.
 * Once loaded, rules can be queried and edited.
 */


#define DEBUG_FLAG CHAMPLAIN_DEBUG_MEMPHIS
#include "champlain-debug.h"
#include "champlain-tile-cache.h"
#include "champlain-renderer.h"
#include "champlain-defines.h"
#include "champlain-enum-types.h"
#include "champlain-private.h"
#include "champlain-memphis-renderer.h"
#include "champlain-bounding-box.h"

#include <gdk/gdk.h>

#include <memphis/memphis.h>
#include <errno.h>
#include <string.h>

/* Tuning parameters */
#define MAX_THREADS 4

const gchar default_rules[] =
  "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
  "<rules version=\"0.1\" background=\"#ffffff\">"
59 60 61
  "<rule e=\"way\" k=\"highway\" v=\"*\">"
  "<line color=\"#000000\" width=\"1.0\"/>"
  "</rule>"
62 63 64 65 66 67 68 69 70
  "</rules>";

enum
{
  PROP_0,
  PROP_TILE_SIZE,
  PROP_BOUNDING_BOX
};

71 72 73 74 75 76 77
static void render (ChamplainRenderer *renderer,
    ChamplainTile *tile);
static void set_data (ChamplainRenderer *renderer,
    const gchar *data,
    guint size);
static void set_bounding_box (ChamplainMemphisRenderer *renderer,
    ChamplainBoundingBox *bbox);
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108


G_DEFINE_TYPE (ChamplainMemphisRenderer, champlain_memphis_renderer, CHAMPLAIN_TYPE_RENDERER)

#define GET_PRIVATE(o) \
  (G_TYPE_INSTANCE_GET_PRIVATE ((o), CHAMPLAIN_TYPE_MEMPHIS_RENDERER, ChamplainMemphisRendererPrivate))

struct _ChamplainMemphisRendererPrivate
{
  MemphisRuleSet *rules;
  MemphisRenderer *renderer;
  GThreadPool *thpool;
  guint tile_size;
  ChamplainBoundingBox *bbox;
};

typedef struct _WorkerThreadData WorkerThreadData;

struct _WorkerThreadData
{
  gint x;
  gint y;
  guint z;
  guint size;

  ChamplainRenderer *renderer;
  ChamplainTile *tile;
  cairo_surface_t *cst;
};

/* lock to protect the renderer state while rendering */
109 110 111 112 113 114 115 116 117
#if GLIB_CHECK_VERSION(2,32,0)
  static GRWLock MemphisLock;
#else
  GStaticRWLock MemphisLock = G_STATIC_RW_LOCK_INIT;
  #define g_rw_lock_reader_lock(l)   g_static_rw_lock_reader_lock (l)
  #define g_rw_lock_reader_unlock(l) g_static_rw_lock_reader_unlock (l)
  #define g_rw_lock_writer_lock(l)   g_static_rw_lock_writer_lock (l)
  #define g_rw_lock_writer_unlock(l) g_static_rw_lock_writer_unlock (l)
#endif
118

119 120 121
static void memphis_worker_thread (gpointer data,
    gpointer user_data);

122 123 124 125 126 127 128 129 130 131 132

static void
champlain_memphis_renderer_get_property (GObject *object,
    guint property_id,
    GValue *value,
    GParamSpec *pspec)
{
  ChamplainMemphisRenderer *renderer = CHAMPLAIN_MEMPHIS_RENDERER (object);

  switch (property_id)
    {
133 134 135 136 137 138 139 140 141 142
    case PROP_TILE_SIZE:
      g_value_set_uint (value, champlain_memphis_renderer_get_tile_size (renderer));
      break;

    case PROP_BOUNDING_BOX:
      g_value_set_boxed (value, champlain_memphis_renderer_get_bounding_box (renderer));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
143 144 145
    }
}

146

147 148 149 150 151 152 153 154 155 156
static void
champlain_memphis_renderer_set_property (GObject *object,
    guint property_id,
    const GValue *value,
    GParamSpec *pspec)
{
  ChamplainMemphisRenderer *renderer = CHAMPLAIN_MEMPHIS_RENDERER (object);

  switch (property_id)
    {
157 158 159 160 161 162 163 164 165 166
    case PROP_TILE_SIZE:
      champlain_memphis_renderer_set_tile_size (renderer, g_value_get_uint (value));
      break;

    case PROP_BOUNDING_BOX:
      set_bounding_box (renderer, g_value_get_boxed (value));
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
167 168 169
    }
}

170

171 172 173
static void
champlain_memphis_renderer_dispose (GObject *object)
{
174
  ChamplainMemphisRenderer *renderer = CHAMPLAIN_MEMPHIS_RENDERER (object);
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
  ChamplainMemphisRendererPrivate *priv = renderer->priv;

  if (priv->thpool)
    {
      g_thread_pool_free (priv->thpool, FALSE, TRUE);
      priv->thpool = NULL;
    }
  if (priv->renderer)
    {
      memphis_renderer_free (priv->renderer);
      priv->renderer = NULL;
    }
  if (priv->rules)
    {
      memphis_rule_set_free (priv->rules);
      priv->rules = NULL;
    }

  G_OBJECT_CLASS (champlain_memphis_renderer_parent_class)->dispose (object);
}

196

197 198 199
static void
champlain_memphis_renderer_finalize (GObject *object)
{
200
  ChamplainMemphisRenderer *renderer = CHAMPLAIN_MEMPHIS_RENDERER (object);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
  ChamplainMemphisRendererPrivate *priv = renderer->priv;

  champlain_bounding_box_free (priv->bbox);

  G_OBJECT_CLASS (champlain_memphis_renderer_parent_class)->finalize (object);
}


static void
champlain_memphis_renderer_class_init (ChamplainMemphisRendererClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  ChamplainRendererClass *renderer_class = CHAMPLAIN_RENDERER_CLASS (klass);

  g_type_class_add_private (klass, sizeof (ChamplainMemphisRendererPrivate));

  object_class->get_property = champlain_memphis_renderer_get_property;
  object_class->set_property = champlain_memphis_renderer_set_property;
  object_class->dispose = champlain_memphis_renderer_dispose;
  object_class->finalize = champlain_memphis_renderer_finalize;

  renderer_class->set_data = set_data;
  renderer_class->render = render;

  g_object_class_install_property (object_class,
      PROP_TILE_SIZE,
      g_param_spec_uint ("tile-size",
          "Tile Size",
          "The size of the rendered tile",
          0,
          G_MAXINT,
          256,
          G_PARAM_READWRITE));

Jiří Techet's avatar
Jiří Techet committed
235 236
  /**
   * ChamplainMemphisRenderer:bounding-box:
237 238 239
   *
   * The bounding box of the area that contains map data.
   *
Jiří Techet's avatar
Jiří Techet committed
240
   * Since: 0.8
241
   */
242 243 244
  g_object_class_install_property (object_class,
      PROP_BOUNDING_BOX,
      g_param_spec_boxed ("bounding-box",
245 246 247 248
          "Bounding Box",
          "The bounding box of the renderer",
          CHAMPLAIN_TYPE_BOUNDING_BOX,
          G_PARAM_READWRITE));
249 250
}

251

252 253 254
static void
champlain_memphis_renderer_init (ChamplainMemphisRenderer *renderer)
{
255
  ChamplainMemphisRendererPrivate *priv = GET_PRIVATE (renderer);
256 257 258 259 260 261 262 263 264 265

  renderer->priv = priv;

  priv->rules = memphis_rule_set_new ();
  memphis_rule_set_load_from_data (priv->rules, default_rules,
      strlen (default_rules), NULL);

  priv->renderer = memphis_renderer_new_full (priv->rules, memphis_map_new ());

  priv->thpool = g_thread_pool_new (memphis_worker_thread, renderer,
Jiří Techet's avatar
Jiří Techet committed
266
        MAX_THREADS, FALSE, NULL);
267 268 269 270

  priv->bbox = NULL;
}

271

Jiří Techet's avatar
Jiří Techet committed
272 273 274 275 276 277 278 279 280 281
/**
 * champlain_memphis_renderer_new_full:
 * @tile_size: the size of the rendered error tile
 *
 * Constructor of a #ChamplainMemphisRenderer.
 *
 * Returns: a constructed #ChamplainMemphisRenderer object
 *
 * Since: 0.8
 */
282 283
ChamplainMemphisRenderer *
champlain_memphis_renderer_new_full (guint tile_size)
284 285 286 287 288 289
{
  return g_object_new (CHAMPLAIN_TYPE_MEMPHIS_RENDERER, "tile-size", tile_size, NULL);
}


/*
290 291 292 293 294
 * Transform ARGB (Cairo) to RGBA (GdkPixbuf). RGBA is actualy reversed in
 * memory, so the transformation is ARGB -> ABGR (i.e. swapping B and R)
 */
static void
argb_to_rgba (guchar *data,
295 296 297
    guint size)
{
  guint32 *ptr;
298 299 300
  guint32 *endptr = (guint32 *) data + size / 4;

  for (ptr = (guint32 *) data; ptr < endptr; ptr++)
301 302 303
    *ptr = (*ptr & 0xFF00FF00) ^ ((*ptr & 0xFF0000) >> 16) ^ ((*ptr & 0xFF) << 16);
}

304

305 306 307 308 309 310
static gboolean
tile_loaded_cb (gpointer worker_data)
{
  WorkerThreadData *data = (WorkerThreadData *) worker_data;
  ChamplainTile *tile = data->tile;
  cairo_surface_t *cst = data->cst;
311
  ChamplainRenderer *renderer = CHAMPLAIN_RENDERER (data->renderer);
312 313 314
  gpointer ret_data = NULL;
  guint ret_size = 0;
  gboolean ret_error = TRUE;
315 316
  ClutterActor *actor;
  guint size = data->size;
317
  GdkPixbuf *pixbuf = NULL;
318 319
  gchar *buffer = NULL;
  gsize buffer_size;
320
  ClutterContent *content;
321

322
  g_slice_free (WorkerThreadData, data);
323 324 325 326

  if (!tile)
    {
      DEBUG ("Tile destroyed while loading");
327
      goto finish;
328 329 330
    }

  if (!cst)
331
    goto finish;
332 333 334 335 336 337 338

  /* modify directly the buffer of cairo surface - we don't use it any more
     and we close the surface anyway */
  argb_to_rgba (cairo_image_surface_get_data (cst),
      cairo_image_surface_get_stride (cst) * cairo_image_surface_get_height (cst));

  pixbuf = gdk_pixbuf_new_from_data (cairo_image_surface_get_data (cst),
Jiří Techet's avatar
Jiří Techet committed
339 340
        GDK_COLORSPACE_RGB, TRUE, 8, size, size,
        cairo_image_surface_get_stride (cst), NULL, NULL);
341

342 343
  if (!gdk_pixbuf_save_to_buffer (pixbuf, &buffer, &buffer_size, "png", NULL, NULL))
    goto finish;
344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
    
  content = clutter_image_new ();
  if (!clutter_image_set_data (CLUTTER_IMAGE (content),
          gdk_pixbuf_get_pixels (pixbuf),
          gdk_pixbuf_get_has_alpha (pixbuf)
            ? COGL_PIXEL_FORMAT_RGBA_8888
            : COGL_PIXEL_FORMAT_RGB_888,
          gdk_pixbuf_get_width (pixbuf),
          gdk_pixbuf_get_height (pixbuf),
          gdk_pixbuf_get_rowstride (pixbuf),
          NULL))
    {
      g_object_unref (content);
      goto finish;
    }

  actor = clutter_actor_new ();
  clutter_actor_set_size (actor, size, size);
  clutter_actor_set_content (actor, content);
  g_object_unref (content);
364 365 366

  champlain_tile_set_content (tile, actor);

367 368 369
  ret_data = buffer;
  ret_size = buffer_size;
  ret_error = FALSE;
370 371

finish:
372
  if (tile)
373
    g_signal_emit_by_name (tile, "render-complete", ret_data, ret_size, ret_error);
374 375 376 377 378 379

  if (pixbuf)
    g_object_unref (pixbuf);
  if (cst)
    cairo_surface_destroy (cst);
  g_object_unref (renderer);
380
  g_object_unref (tile);
381 382 383 384 385
  g_free (buffer);

  return FALSE;
}

386

387 388 389 390
static void
memphis_worker_thread (gpointer worker_data,
    G_GNUC_UNUSED gpointer user_data)
{
391
  WorkerThreadData *data = (WorkerThreadData *) worker_data;
392 393 394 395 396
  ChamplainMemphisRenderer *renderer = CHAMPLAIN_MEMPHIS_RENDERER (data->renderer);
  gboolean has_data = TRUE;

  data->cst = NULL;

397
  g_rw_lock_reader_lock (&MemphisLock);
398
  has_data = memphis_renderer_tile_has_data (renderer->priv->renderer, data->x, data->y, data->z);
399
  g_rw_lock_reader_unlock (&MemphisLock);
400 401 402 403 404 405 406 407 408 409 410

  if (has_data)
    {
      cairo_t *cr;

      /* create a clutter-independant surface to draw on */
      data->cst = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, data->size, data->size);
      cr = cairo_create (data->cst);

      DEBUG ("Draw Tile (%d, %d, %d)", data->x, data->y, data->z);

411
      g_rw_lock_reader_lock (&MemphisLock);
412
      memphis_renderer_draw_tile (renderer->priv->renderer, cr, data->x, data->y, data->z);
413
      g_rw_lock_reader_unlock (&MemphisLock);
414 415 416 417

      cairo_destroy (cr);
    }

418
  clutter_threads_add_idle_full (CLUTTER_PRIORITY_REDRAW, tile_loaded_cb, data, NULL);
419 420
}

421

422 423
static void
render (ChamplainRenderer *renderer,
424
    ChamplainTile *tile)
425 426 427 428 429 430 431 432
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

  ChamplainMemphisRendererPrivate *priv = CHAMPLAIN_MEMPHIS_RENDERER (renderer)->priv;
  GError *error = NULL;
  WorkerThreadData *data;

  DEBUG ("Render tile (%u, %u, %u)", champlain_tile_get_x (tile),
433 434
      champlain_tile_get_y (tile),
      champlain_tile_get_zoom_level (tile));
435

436
  data = g_slice_new (WorkerThreadData);
437 438 439 440 441 442 443
  data->x = champlain_tile_get_x (tile);
  data->y = champlain_tile_get_y (tile);
  data->z = champlain_tile_get_zoom_level (tile);
  data->size = priv->tile_size;
  data->tile = tile;
  data->renderer = renderer;

444
  g_object_ref (tile);
445 446 447 448 449 450 451
  g_object_ref (renderer);

  g_thread_pool_push (priv->thpool, data, &error);
  if (error)
    {
      g_error ("Thread pool error: %s", error->message);
      g_error_free (error);
452
      g_slice_free (WorkerThreadData, data);
453
      g_object_unref (renderer);
454
      g_object_unref (tile);
455 456 457
    }
}

458

459 460 461 462 463 464 465 466 467 468
static void
set_data (ChamplainRenderer *renderer,
    const gchar *data,
    guint size)
{
  ChamplainMemphisRendererPrivate *priv = GET_PRIVATE (renderer);
  ChamplainBoundingBox *bbox;
  GError *err = NULL;

  MemphisMap *map = memphis_map_new ();
469

470 471 472 473 474 475 476 477 478 479 480 481
  memphis_map_load_from_data (map, data, size, &err);

  DEBUG ("BBox data received");

  if (err != NULL)
    {
      g_critical ("Can't load map data: \"%s\"", err->message);
      memphis_map_free (map);
      g_error_free (err);
      return;
    }

482
  g_rw_lock_writer_lock (&MemphisLock);
483
  memphis_renderer_set_map (priv->renderer, map);
484
  g_rw_lock_writer_unlock (&MemphisLock);
485 486 487

  bbox = champlain_bounding_box_new ();

488 489
  memphis_map_get_bounding_box (map, &bbox->bottom, &bbox->left, &bbox->top,
      &bbox->right);
490 491 492 493
  g_object_set (G_OBJECT (renderer), "bounding-box", bbox, NULL);
  champlain_bounding_box_free (bbox);
}

494

Jiří Techet's avatar
Jiří Techet committed
495
/**
496 497 498 499 500 501
 * champlain_memphis_renderer_load_rules:
 * @renderer: a #ChamplainMemphisRenderer
 * @rules_path: a path to a rules file
 *
 * Loads a Memphis rules file.
 *
Jiří Techet's avatar
Jiří Techet committed
502
 * Since: 0.8
503 504 505 506 507 508 509 510 511 512 513
 */
void
champlain_memphis_renderer_load_rules (
    ChamplainMemphisRenderer *renderer,
    const gchar *rules_path)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

  ChamplainMemphisRendererPrivate *priv = renderer->priv;
  GError *err = NULL;

514
  /* TODO: Remove test when memphis handles invalid paths properly */
515 516 517 518 519 520
  if (!g_file_test (rules_path, G_FILE_TEST_EXISTS))
    {
      g_critical ("Error: \"%s\" does not exist.", rules_path);
      return;
    }

521
  g_rw_lock_writer_lock (&MemphisLock);
522 523 524 525
  if (rules_path)
    {
      memphis_rule_set_load_from_file (priv->rules, rules_path, &err);
      if (err != NULL)
526
        {
527 528
          g_critical ("Can't load rules file: \"%s\"", err->message);
          memphis_rule_set_load_from_data (priv->rules, default_rules,
529
              strlen (default_rules), NULL);
530
          g_rw_lock_writer_unlock (&MemphisLock);
531 532
          g_error_free (err);
          return;
533
        }
534 535 536
    }
  else
    memphis_rule_set_load_from_data (priv->rules, default_rules,
537
        strlen (default_rules), NULL);
538

539
  g_rw_lock_writer_unlock (&MemphisLock);
540 541 542
}


Jiří Techet's avatar
Jiří Techet committed
543
/**
544 545 546 547 548 549 550 551
 * champlain_memphis_renderer_get_background_color:
 * @renderer: a #ChamplainMemphisRenderer
 *
 * Gets the background color of the map.
 *
 * Returns: the background color of the map as a newly-allocated
 * #ClutterColor.
 *
Jiří Techet's avatar
Jiří Techet committed
552
 * Since: 0.8
553 554 555 556 557 558 559 560 561 562
 */
ClutterColor *
champlain_memphis_renderer_get_background_color (
    ChamplainMemphisRenderer *renderer)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer), NULL);

  ClutterColor color;
  guint8 r, b, g, a;

563
  g_rw_lock_reader_lock (&MemphisLock);
564
  memphis_rule_set_get_bg_color (renderer->priv->rules, &r, &g, &b, &a);
565
  g_rw_lock_reader_unlock (&MemphisLock);
566 567 568 569 570 571 572 573

  color.red = r;
  color.green = g;
  color.blue = b;
  color.alpha = a;
  return clutter_color_copy (&color);
}

574

Jiří Techet's avatar
Jiří Techet committed
575
/**
576 577 578 579 580 581
 * champlain_memphis_renderer_set_background_color:
 * @renderer: a #ChamplainMemphisRenderer
 * @color: a #ClutterColor
 *
 * Sets the background color of the map from a #ClutterColor.
 *
Jiří Techet's avatar
Jiří Techet committed
582
 * Since: 0.8
583 584 585 586 587 588 589 590
 */
void
champlain_memphis_renderer_set_background_color (
    ChamplainMemphisRenderer *renderer,
    const ClutterColor *color)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

591
  g_rw_lock_writer_lock (&MemphisLock);
592
  memphis_rule_set_set_bg_color (renderer->priv->rules, color->red,
593
      color->green, color->blue, color->alpha);
594
  g_rw_lock_writer_unlock (&MemphisLock);
595 596 597
}


Jiří Techet's avatar
Jiří Techet committed
598
/**
599 600
 * champlain_memphis_renderer_set_rule:
 * @renderer: a #ChamplainMemphisRenderer
Jiří Techet's avatar
Jiří Techet committed
601
 * @rule: a #ChamplainMemphisRule
602 603
 *
 * Edits or adds a #ChamplainMemphisRule to the rules-set. New rules are appended
Jiří Techet's avatar
Jiří Techet committed
604
 * to the list.
605
 *
Jiří Techet's avatar
Jiří Techet committed
606
 * Since: 0.8
607 608 609 610 611 612
 */
void
champlain_memphis_renderer_set_rule (ChamplainMemphisRenderer *renderer,
    ChamplainMemphisRule *rule)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer) &&
613
      MEMPHIS_RULE (rule));
614

615
  g_rw_lock_writer_lock (&MemphisLock);
616
  memphis_rule_set_set_rule (renderer->priv->rules, (MemphisRule *) rule);
617
  g_rw_lock_writer_unlock (&MemphisLock);
618 619
}

620

Jiří Techet's avatar
Jiří Techet committed
621
/**
622
 * champlain_memphis_renderer_get_rule: (skip)
623 624 625 626 627 628 629
 * @renderer: a #ChamplainMemphisRenderer
 * @id: an id string
 *
 * Gets the requested #ChamplainMemphisRule.
 *
 * Returns: the requested #ChamplainMemphisRule or NULL if none is found.
 *
Jiří Techet's avatar
Jiří Techet committed
630
 * Since: 0.8
631 632 633 634 635 636
 */
ChamplainMemphisRule *
champlain_memphis_renderer_get_rule (ChamplainMemphisRenderer *renderer,
    const gchar *id)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer) &&
637
      id != NULL, NULL);
638 639 640

  MemphisRule *rule;

641
  g_rw_lock_reader_lock (&MemphisLock);
642
  rule = memphis_rule_set_get_rule (renderer->priv->rules, id);
643
  g_rw_lock_reader_unlock (&MemphisLock);
644 645 646 647

  return (ChamplainMemphisRule *) rule;
}

648

Jiří Techet's avatar
Jiří Techet committed
649
/**
650 651 652 653 654
 * champlain_memphis_renderer_get_rule_ids:
 * @renderer: a #ChamplainMemphisRenderer
 *
 * Get a list of rule id's.
 *
655
 * Returns: (transfer full) (element-type utf8): a #GList of id strings of the form:
656 657 658 659
 * key1|key2|...|keyN:value1|value2|...|valueM
 *
 * Example: "waterway:river|stream|canal"
 *
Jiří Techet's avatar
Jiří Techet committed
660
 * Since: 0.8
661 662 663 664 665 666 667 668
 */
GList *
champlain_memphis_renderer_get_rule_ids (ChamplainMemphisRenderer *renderer)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer), NULL);

  GList *list;

669
  g_rw_lock_reader_lock (&MemphisLock);
670
  list = memphis_rule_set_get_rule_ids (renderer->priv->rules);
671
  g_rw_lock_reader_unlock (&MemphisLock);
672 673 674 675

  return list;
}

676

Jiří Techet's avatar
Jiří Techet committed
677
/**
678 679 680 681 682 683
 * champlain_memphis_renderer_remove_rule:
 * @renderer: a #ChamplainMemphisRenderer
 * @id: an id string
 *
 * Removes the rule with the given id.
 *
Jiří Techet's avatar
Jiří Techet committed
684
 * Since: 0.8
685
 */
686 687
void
champlain_memphis_renderer_remove_rule (
688 689 690 691 692
    ChamplainMemphisRenderer *renderer,
    const gchar *id)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

693
  g_rw_lock_writer_lock (&MemphisLock);
694
  memphis_rule_set_remove_rule (renderer->priv->rules, id);
695
  g_rw_lock_writer_unlock (&MemphisLock);
696 697
}

698

Jiří Techet's avatar
Jiří Techet committed
699 700 701 702 703 704 705 706 707
/**
 * champlain_memphis_renderer_set_tile_size:
 * @renderer: a #ChamplainMemphisRenderer
 * @size: the size of the rendered tiles
 *
 * Sets the size of the rendered tiles.
 *
 * Since: 0.8
 */
708 709 710 711 712 713
void
champlain_memphis_renderer_set_tile_size (ChamplainMemphisRenderer *renderer,
    guint size)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

714
  ChamplainMemphisRendererPrivate *priv = renderer->priv;
715 716 717

  renderer->priv->tile_size = size;

718
  g_rw_lock_writer_lock (&MemphisLock);
719
  memphis_renderer_set_resolution (priv->renderer, size);
720
  g_rw_lock_writer_unlock (&MemphisLock);
721 722 723 724

  g_object_notify (G_OBJECT (renderer), "tile-size");
}

725

Jiří Techet's avatar
Jiří Techet committed
726 727 728 729 730 731 732 733 734 735
/**
 * champlain_memphis_renderer_get_tile_size:
 * @renderer: a #ChamplainMemphisRenderer
 *
 * Gets the size of the rendered tiles.
 *
 * Returns: the size of the rendered tiles
 *
 * Since: 0.8
 */
736 737 738 739 740 741 742 743
guint
champlain_memphis_renderer_get_tile_size (ChamplainMemphisRenderer *renderer)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer), 0);

  return renderer->priv->tile_size;
}

744

Jiří Techet's avatar
Jiří Techet committed
745 746 747 748 749 750 751 752 753 754
/**
 * champlain_memphis_renderer_get_bounding_box:
 * @renderer: a #ChamplainMemphisRenderer
 *
 * Gets the bounding box of the area for which map data is available.
 *
 * Returns: the bounding box
 *
 * Since: 0.8
 */
755 756 757 758 759 760 761 762 763 764
ChamplainBoundingBox *
champlain_memphis_renderer_get_bounding_box (ChamplainMemphisRenderer *renderer)
{
  g_return_val_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer), NULL);

  ChamplainMemphisRendererPrivate *priv = renderer->priv;

  return priv->bbox;
}

765

766 767 768 769 770 771 772 773 774
static void
set_bounding_box (ChamplainMemphisRenderer *renderer, ChamplainBoundingBox *bbox)
{
  g_return_if_fail (CHAMPLAIN_IS_MEMPHIS_RENDERER (renderer));

  ChamplainMemphisRendererPrivate *priv = renderer->priv;

  champlain_bounding_box_free (priv->bbox);
  priv->bbox = champlain_bounding_box_copy (bbox);
775
  g_object_notify (G_OBJECT (renderer), "bounding-box");
776
}