Expose VikMapSourceDefault's private fields as properties
[viking/guyou.git] / src / terraservermapsource.c
blob4d53f0411bb33eb133bfc69ae918b10e344ae0fb
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * viking
4 * Copyright (C) Guilhem Bonnefille 2009 <guilhem.bonnefille@gmail.com>
5 *
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/>.
19 #ifdef HAVE_CONFIG_H
20 #include "config.h"
21 #endif
23 #ifdef HAVE_MATH_H
24 #include <math.h>
25 #endif
27 #include "globals.h"
28 #include "terraservermapsource.h"
30 static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
31 static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
32 static int _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn );
34 /* FIXME Huge gruik */
35 static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
37 typedef struct _TerraserverMapSourcePrivate TerraserverMapSourcePrivate;
38 struct _TerraserverMapSourcePrivate
40 int type;
43 #define TERRASERVER_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TERRASERVER_TYPE_MAP_SOURCE, TerraserverMapSourcePrivate))
45 G_DEFINE_TYPE_EXTENDED (TerraserverMapSource, terraserver_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
47 static void
48 terraserver_map_source_init (TerraserverMapSource *self)
50 /* initialize the object here */
51 g_object_set (G_OBJECT (self),
52 "tilesize-x", 200,
53 "tilesize-y", 200,
54 "drawmode", VIK_VIEWPORT_DRAWMODE_UTM,
55 NULL);
58 static void
59 terraserver_map_source_finalize (GObject *object)
61 /* TODO: Add deinitalization code here */
63 G_OBJECT_CLASS (terraserver_map_source_parent_class)->finalize (object);
66 static void
67 terraserver_map_source_class_init (TerraserverMapSourceClass *klass)
69 GObjectClass* object_class = G_OBJECT_CLASS (klass);
70 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
72 /* Overiding methods */
73 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
74 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
75 parent_class->download = _download;
77 g_type_class_add_private (klass, sizeof (TerraserverMapSourcePrivate));
79 object_class->finalize = terraserver_map_source_finalize;
82 #define TERRASERVER_SITE "terraserver-usa.com"
83 #define MARGIN_OF_ERROR 0.001
85 static int mpp_to_scale ( gdouble mpp, guint8 type )
87 mpp *= 4;
88 gint t = (gint) mpp;
89 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
90 return FALSE;
92 switch ( t ) {
93 case 1: return (type == 4) ? 8 : 0;
94 case 2: return (type == 4) ? 9 : 0;
95 case 4: return (type != 2) ? 10 : 0;
96 case 8: return 11;
97 case 16: return 12;
98 case 32: return 13;
99 case 64: return 14;
100 case 128: return 15;
101 case 256: return 16;
102 case 512: return 17;
103 case 1024: return 18;
104 case 2048: return 19;
105 default: return 0;
109 static gdouble scale_to_mpp ( gint scale )
111 return pow(2,scale - 10);
114 static gboolean
115 _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
117 g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
119 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self);
120 int type = priv->type;
121 g_assert ( src->mode == VIK_COORD_UTM );
123 if ( xmpp != ympp )
124 return FALSE;
126 dest->scale = mpp_to_scale ( xmpp, type );
127 if ( ! dest->scale )
128 return FALSE;
130 dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
131 dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
132 dest->z = src->utm_zone;
133 return TRUE;
136 static void
137 _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
139 // FIXME: slowdown here!
140 gdouble mpp = scale_to_mpp ( src->scale );
141 dest->mode = VIK_COORD_UTM;
142 dest->utm_zone = src->z;
143 dest->east_west = ((src->x * 200) + 100) * mpp;
144 dest->north_south = ((src->y * 200) + 100) * mpp;
147 static int
148 _download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
150 g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
152 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self); int res = -1;
153 int type = priv->type;
154 gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
155 src->scale, src->x, src->y, src->z );
156 res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
157 g_free ( uri );
158 return res;
161 TerraserverMapSource *
162 terraserver_map_source_new_with_id (guint8 id, int type)
164 TerraserverMapSource *ret = g_object_new(TERRASERVER_TYPE_MAP_SOURCE, "id", id, NULL);
166 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(ret);
167 priv->type = type;
168 return ret;