1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/*
* Copyright (C) 2010-2013 Jiri Techet <techet@gmail.com>
*
* 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
*/
/**
* SECTION:champlain-map-source
* @short_description: A base class for map sources
*
* #ChamplainTile objects come from map sources which are represented by
* #ChamplainMapSource. This is should be considered an abstract
* type as it does nothing of interest.
*
* When loading new tiles, #ChamplainView calls champlain_map_source_fill_tile()
* on the current #ChamplainMapSource passing it a #ChamplainTile to be filled
* with the image.
*
* Apart from being a base class of all map sources, #ChamplainMapSource
* also supports cooperation of multiple map sources by arranging them into
* chains. Every map source has the #ChamplainMapSource:next-source property
* that determines the next map source in the chain. When a function of
* a #ChamplainMapSource object is invoked, the map source may decide to
* delegate the work to the next map source in the chain by invoking the
* same function on it.
* To understand the concept of chains, consider for instance a chain
* consisting of #ChamplainFileCache whose next source is
* #ChamplainNetworkTileSource whose next source is an error tile source
* created with champlain_map_source_factory_create_error_source ().
* When champlain_map_source_fill_tile() is called on the first object of the
* chain, #ChamplainFileCache, the cache checks whether it contains the
* requested tile in its database. If it does, it returns the tile; otherwise,
* it calls champlain_map_source_fill_tile() on the next source in the chain
* (#ChamplainNetworkTileSource). The network tile source loads the tile
* from the network. When successful, it returns the tile; otherwise it requests
* the tile from the next source in the chain (error tile source).
* The error tile source always generates an error tile, no matter what
* its next source is.
*/
#include "champlain-map-source.h"
#include <math.h>
struct _ChamplainMapSourcePrivate
{
ChamplainMapSource *next_source;
ChamplainRenderer *renderer;
};
G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (ChamplainMapSource, champlain_map_source, G_TYPE_INITIALLY_UNOWNED)
enum
{
PROP_0,
PROP_NEXT_SOURCE,
PROP_RENDERER,
};
static void
champlain_map_source_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
ChamplainMapSourcePrivate *priv = CHAMPLAIN_MAP_SOURCE (object)->priv;
switch (prop_id)
{
case PROP_NEXT_SOURCE:
g_value_set_object (value, priv->next_source);
break;
case PROP_RENDERER:
g_value_set_object (value, priv->renderer);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
champlain_map_source_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
ChamplainMapSource *map_source = CHAMPLAIN_MAP_SOURCE (object);
switch (prop_id)
{
case PROP_NEXT_SOURCE:
champlain_map_source_set_next_source (map_source,
g_value_get_object (value));
break;
case PROP_RENDERER:
champlain_map_source_set_renderer (map_source,
g_value_get_object (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
}
}
static void
champlain_map_source_dispose (GObject *object)
{
ChamplainMapSourcePrivate *priv = CHAMPLAIN_MAP_SOURCE (object)->priv;
if (priv->next_source)
{
g_object_unref (priv->next_source);
priv->next_source = NULL;
}
if (priv->renderer)
{
g_object_unref (priv->renderer);
priv->renderer = NULL;
}
G_OBJECT_CLASS (champlain_map_source_parent_class)->dispose (object);
}
static void
champlain_map_source_finalize (GObject *object)
{
G_OBJECT_CLASS (champlain_map_source_parent_class)->finalize (object);
}
static void
champlain_map_source_constructed (GObject *object)
{
if (G_OBJECT_CLASS (champlain_map_source_parent_class)->constructed)
G_OBJECT_CLASS (champlain_map_source_parent_class)->constructed (object);
}
static void
champlain_map_source_class_init (ChamplainMapSourceClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GParamSpec *pspec;
object_class->finalize = champlain_map_source_finalize;
object_class->dispose = champlain_map_source_dispose;
object_class->get_property = champlain_map_source_get_property;
object_class->set_property = champlain_map_source_set_property;
object_class->constructed = champlain_map_source_constructed;
klass->get_id = NULL;
klass->get_name = NULL;
klass->get_license = NULL;
klass->get_license_uri = NULL;
klass->get_min_zoom_level = NULL;
klass->get_max_zoom_level = NULL;
klass->get_tile_size = NULL;
klass->get_projection = NULL;
klass->fill_tile = NULL;
/**
* ChamplainMapSource:next-source:
*
* Next source in the loading chain.
*
* Since: 0.6
*/
pspec = g_param_spec_object ("next-source",
"Next Source",
"Next source in the loading chain",
CHAMPLAIN_TYPE_MAP_SOURCE,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_NEXT_SOURCE, pspec);
/**
* ChamplainMapSource:renderer:
*
* Renderer used for tiles rendering.
*
* Since: 0.8
*/
pspec = g_param_spec_object ("renderer",
"Tile renderer",
"Tile renderer used to render tiles",
CHAMPLAIN_TYPE_RENDERER,
G_PARAM_READWRITE);
g_object_class_install_property (object_class, PROP_RENDERER, pspec);
}
static void
champlain_map_source_init (ChamplainMapSource *map_source)
{
ChamplainMapSourcePrivate *priv = champlain_map_source_get_instance_private (map_source);
map_source->priv = priv;
priv->next_source = NULL;
priv->renderer = NULL;
}
/**
* champlain_map_source_get_next_source:
* @map_source: a #ChamplainMapSource
*
* Get the next source in the chain.
*
* Returns: (transfer none): the next source in the chain.
*
* Since: 0.6
*/
ChamplainMapSource *
champlain_map_source_get_next_source (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return map_source->priv->next_source;
}
/**
* champlain_map_source_get_renderer:
* @map_source: a #ChamplainMapSource
*
* Get the renderer used for tiles rendering.
*
* Returns: (transfer none): the renderer.
*
* Since: 0.8
*/
ChamplainRenderer *
champlain_map_source_get_renderer (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return map_source->priv->renderer;
}
/**
* champlain_map_source_set_next_source:
* @map_source: a #ChamplainMapSource
* @next_source: the next #ChamplainMapSource in the chain
*
* Sets the next map source in the chain.
*
* Since: 0.6
*/
void
champlain_map_source_set_next_source (ChamplainMapSource *map_source,
ChamplainMapSource *next_source)
{
g_return_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source));
ChamplainMapSourcePrivate *priv = map_source->priv;
if (priv->next_source != NULL)
g_object_unref (priv->next_source);
if (next_source)
{
g_return_if_fail (CHAMPLAIN_IS_MAP_SOURCE (next_source));
g_object_ref_sink (next_source);
}
priv->next_source = next_source;
g_object_notify (G_OBJECT (map_source), "next-source");
}
/**
* champlain_map_source_set_renderer:
* @map_source: a #ChamplainMapSource
* @renderer: the renderer
*
* Sets the renderer used for tiles rendering.
*
* Since: 0.8
*/
void
champlain_map_source_set_renderer (ChamplainMapSource *map_source,
ChamplainRenderer *renderer)
{
g_return_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source));
g_return_if_fail (CHAMPLAIN_IS_RENDERER (renderer));
ChamplainMapSourcePrivate *priv = map_source->priv;
if (priv->renderer != NULL)
g_object_unref (priv->renderer);
g_object_ref_sink (renderer);
priv->renderer = renderer;
g_object_notify (G_OBJECT (map_source), "renderer");
}
/**
* champlain_map_source_get_id:
* @map_source: a #ChamplainMapSource
*
* Gets map source's id.
*
* Returns: the map source's id.
*
* Since: 0.4
*/
const gchar *
champlain_map_source_get_id (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_id (map_source);
}
/**
* champlain_map_source_get_name:
* @map_source: a #ChamplainMapSource
*
* Gets map source's name.
*
* Returns: the map source's name.
*
* Since: 0.4
*/
const gchar *
champlain_map_source_get_name (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_name (map_source);
}
/**
* champlain_map_source_get_license:
* @map_source: a #ChamplainMapSource
*
* Gets map source's license.
*
* Returns: the map source's license.
*
* Since: 0.4
*/
const gchar *
champlain_map_source_get_license (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_license (map_source);
}
/**
* champlain_map_source_get_license_uri:
* @map_source: a #ChamplainMapSource
*
* Gets map source's license URI.
*
* Returns: the map source's license URI.
*
* Since: 0.4
*/
const gchar *
champlain_map_source_get_license_uri (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), NULL);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_license_uri (map_source);
}
/**
* champlain_map_source_get_min_zoom_level:
* @map_source: a #ChamplainMapSource
*
* Gets map source's minimum zoom level.
*
* Returns: the miminum zoom level this map source supports
*
* Since: 0.4
*/
guint
champlain_map_source_get_min_zoom_level (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_min_zoom_level (map_source);
}
/**
* champlain_map_source_get_max_zoom_level:
* @map_source: a #ChamplainMapSource
*
* Gets map source's maximum zoom level.
*
* Returns: the maximum zoom level this map source supports
*
* Since: 0.4
*/
guint
champlain_map_source_get_max_zoom_level (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_max_zoom_level (map_source);
}
/**
* champlain_map_source_get_tile_size:
* @map_source: a #ChamplainMapSource
*
* Gets map source's tile size.
*
* Returns: the tile's size (width and height) in pixels for this map source
*
* Since: 0.4
*/
guint
champlain_map_source_get_tile_size (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_tile_size (map_source);
}
/**
* champlain_map_source_get_projection:
* @map_source: a #ChamplainMapSource
*
* Gets map source's projection.
*
* Returns: the map source's projection.
*
* Since: 0.4
*/
ChamplainMapProjection
champlain_map_source_get_projection (ChamplainMapSource *map_source)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), CHAMPLAIN_MAP_PROJECTION_MERCATOR);
return CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->get_projection (map_source);
}
/**
* champlain_map_source_get_x:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
* @longitude: a longitude
*
* Gets the x position on the map using this map source's projection.
* (0, 0) is located at the top left.
*
* Returns: the x position
*
* Since: 0.4
*/
gdouble
champlain_map_source_get_x (ChamplainMapSource *map_source,
guint zoom_level,
gdouble longitude)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
longitude = CLAMP (longitude, CHAMPLAIN_MIN_LONGITUDE, CHAMPLAIN_MAX_LONGITUDE);
/* FIXME: support other projections */
return ((longitude + 180.0) / 360.0 * pow (2.0, zoom_level)) * champlain_map_source_get_tile_size (map_source);
}
/**
* champlain_map_source_get_y:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
* @latitude: a latitude
*
* Gets the y position on the map using this map source's projection.
* (0, 0) is located at the top left.
*
* Returns: the y position
*
* Since: 0.4
*/
gdouble
champlain_map_source_get_y (ChamplainMapSource *map_source,
guint zoom_level,
gdouble latitude)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
latitude = CLAMP (latitude, CHAMPLAIN_MIN_LATITUDE, CHAMPLAIN_MAX_LATITUDE);
/* FIXME: support other projections */
return ((1.0 - log (tan (latitude * M_PI / 180.0) + 1.0 / cos (latitude * M_PI / 180.0)) / M_PI) /
2.0 * pow (2.0, zoom_level)) * champlain_map_source_get_tile_size (map_source);
}
/**
* champlain_map_source_get_longitude:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
* @x: a x position
*
* Gets the longitude corresponding to this x position in the map source's
* projection.
*
* Returns: the longitude
*
* Since: 0.4
*/
gdouble
champlain_map_source_get_longitude (ChamplainMapSource *map_source,
guint zoom_level,
gdouble x)
{
gdouble longitude;
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0.0);
/* FIXME: support other projections */
gdouble dx = (gdouble) x / champlain_map_source_get_tile_size (map_source);
longitude = dx / pow (2.0, zoom_level) * 360.0 - 180.0;
return CLAMP (longitude, CHAMPLAIN_MIN_LONGITUDE, CHAMPLAIN_MAX_LONGITUDE);
}
/**
* champlain_map_source_get_latitude:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
* @y: a y position
*
* Gets the latitude corresponding to this y position in the map source's
* projection.
*
* Returns: the latitude
*
* Since: 0.4
*/
gdouble
champlain_map_source_get_latitude (ChamplainMapSource *map_source,
guint zoom_level,
gdouble y)
{
gdouble latitude;
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0.0);
/* FIXME: support other projections */
gdouble dy = (gdouble) y / champlain_map_source_get_tile_size (map_source);
gdouble n = M_PI - 2.0 * M_PI * dy / pow (2.0, zoom_level);
latitude = 180.0 / M_PI *atan (0.5 * (exp (n) - exp (-n)));
return CLAMP (latitude, CHAMPLAIN_MIN_LATITUDE, CHAMPLAIN_MAX_LATITUDE);
}
/**
* champlain_map_source_get_row_count:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
*
* Gets the number of tiles in a row at this zoom level for this map source.
*
* Returns: the number of tiles in a row
*
* Since: 0.4
*/
guint
champlain_map_source_get_row_count (ChamplainMapSource *map_source,
guint zoom_level)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
/* FIXME: support other projections */
return (zoom_level != 0) ? 2 << (zoom_level - 1) : 1;
}
/**
* champlain_map_source_get_column_count:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
*
* Gets the number of tiles in a column at this zoom level for this map
* source.
*
* Returns: the number of tiles in a column
*
* Since: 0.4
*/
guint
champlain_map_source_get_column_count (ChamplainMapSource *map_source,
guint zoom_level)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0);
/* FIXME: support other projections */
return (zoom_level != 0) ? 2 << (zoom_level - 1) : 1;
}
#define EARTH_RADIUS 6378137.0 /* meters, Equatorial radius */
/**
* champlain_map_source_get_meters_per_pixel:
* @map_source: a #ChamplainMapSource
* @zoom_level: the zoom level
* @latitude: a latitude
* @longitude: a longitude
*
* Gets meters per pixel at the position on the map using this map source's projection.
*
* Returns: the meters per pixel
*
* Since: 0.4.3
*/
gdouble
champlain_map_source_get_meters_per_pixel (ChamplainMapSource *map_source,
guint zoom_level,
gdouble latitude,
G_GNUC_UNUSED gdouble longitude)
{
g_return_val_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source), 0.0);
/*
* Width is in pixels. (1 px)
* m/px = radius_at_latitude / width_in_pixels
* k = radius of earth = 6 378.1 km
* radius_at_latitude = 2pi * k * sin (pi/2-theta)
*/
gdouble tile_size = champlain_map_source_get_tile_size (map_source);
/* FIXME: support other projections */
return 2.0 *M_PI *EARTH_RADIUS *sin (M_PI / 2.0 - M_PI / 180.0 *latitude) /
(tile_size * champlain_map_source_get_row_count (map_source, zoom_level));
}
/**
* champlain_map_source_fill_tile:
* @map_source: a #ChamplainMapSource
* @tile: a #ChamplainTile
*
* Fills the tile with image data (either from cache, network or rendered
* locally).
*
* Since: 0.4
*/
void
champlain_map_source_fill_tile (ChamplainMapSource *map_source,
ChamplainTile *tile)
{
g_return_if_fail (CHAMPLAIN_IS_MAP_SOURCE (map_source));
CHAMPLAIN_MAP_SOURCE_GET_CLASS (map_source)->fill_tile (map_source, tile);
}
|