8 #include "vik-map-type.h"
16 #include <glib/gi18n.h>
24 #include "curl_download.h"
27 #include "vikmapslayer.h"
36 static DownloadOptions osm_options = { NULL, 0, a_check_map_file };
38 /* 1 << (x) is like a 2**(x) */
39 #define GZ(x) (1<<(x))
41 static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
42 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
44 static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
46 #define ERROR_MARGIN 0.01
47 guint8 osm_zoom ( gdouble mpp ) {
49 for ( i = 0; i < num_scales; i++ ) {
50 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
58 class Osm:Map:Type from Vik:Map:Type {
59 private gchar *hostname;
61 private gchar *filename_format;
63 override (Vik:Map:Type) gboolean
64 coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
66 g_assert ( src->mode == VIK_COORD_LATLON );
71 dest->scale = osm_zoom ( xzoom );
72 if ( dest->scale == 255 )
75 dest->x = (src->east_west + 180) / 360 * GZ(17) / xzoom;
76 dest->y = (180 - MERCLAT(src->north_south)) / 360 * GZ(17) / xzoom;
81 override (Vik:Map:Type) void
82 mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
84 gdouble socalled_mpp = GZ(src->scale);
85 dest->mode = VIK_COORD_LATLON;
86 dest->east_west = ((src->x+0.5) / GZ(17) * socalled_mpp * 360) - 180;
87 dest->north_south = DEMERCLAT(180 - ((src->y+0.5) / GZ(17) * socalled_mpp * 360));
90 override (Vik:Map:Type) int
91 download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
94 gchar *uri = g_strdup_printf ( OSM_MAP_TYPE(self)->_priv->filename_format,
95 17 - src->scale, src->x, src->y );
96 res = a_http_download_get_url ( OSM_MAP_TYPE(self)->_priv->hostname,
97 uri, dest_fn, &osm_options );