]> git.street.me.uk Git - andy/viking.git/blame - src/terraservermapsource.c
Fix memory leak on re-downloading 'bad' map image tiles.
[andy/viking.git] / src / terraservermapsource.c
CommitLineData
c9c9e658
GB
1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2/*
3 * viking
a482007a 4 * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
c9c9e658
GB
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.
10 *
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.
15 *
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/>.
18 */
19#ifdef HAVE_CONFIG_H
20#include "config.h"
21#endif
22
23#ifdef HAVE_MATH_H
24#include <math.h>
25#endif
26
27#include "globals.h"
28#include "terraservermapsource.h"
29
30static gboolean _coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
31static void _mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest );
a0262d5b 32static gboolean _is_direct_file_access ( VikMapSource *self );
0fb11294 33static gboolean _is_mbtiles ( VikMapSource *self );
f66ea3ec
GB
34
35static gchar *_get_uri( VikMapSourceDefault *self, MapCoord *src );
36static gchar *_get_hostname( VikMapSourceDefault *self );
686baff0 37static DownloadFileOptions *_get_download_options( VikMapSourceDefault *self );
c9c9e658
GB
38
39/* FIXME Huge gruik */
686baff0 40static DownloadFileOptions terraserver_options = { FALSE, FALSE, NULL, 0, a_check_map_file, NULL, NULL };
c9c9e658
GB
41
42typedef struct _TerraserverMapSourcePrivate TerraserverMapSourcePrivate;
43struct _TerraserverMapSourcePrivate
44{
e76eccdc 45 guint8 type;
c9c9e658
GB
46};
47
48#define TERRASERVER_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TERRASERVER_TYPE_MAP_SOURCE, TerraserverMapSourcePrivate))
49
e76eccdc
GB
50/* properties */
51enum
52{
53 PROP_0,
54
55 PROP_TYPE,
56};
57
f6411015 58G_DEFINE_TYPE (TerraserverMapSource, terraserver_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT);
c9c9e658
GB
59
60static void
3a84e537 61terraserver_map_source_init (TerraserverMapSource *self)
c9c9e658
GB
62{
63 /* initialize the object here */
31f3c5e7
GB
64 g_object_set (G_OBJECT (self),
65 "tilesize-x", 200,
66 "tilesize-y", 200,
67 "drawmode", VIK_VIEWPORT_DRAWMODE_UTM,
68 NULL);
c9c9e658
GB
69}
70
71static void
72terraserver_map_source_finalize (GObject *object)
73{
74 /* TODO: Add deinitalization code here */
75
76 G_OBJECT_CLASS (terraserver_map_source_parent_class)->finalize (object);
77}
78
e76eccdc
GB
79static void
80terraserver_map_source_set_property (GObject *object,
81 guint property_id,
82 const GValue *value,
83 GParamSpec *pspec)
84{
85 TerraserverMapSource *self = TERRASERVER_MAP_SOURCE (object);
86 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE (self);
87
88 switch (property_id)
89 {
90 case PROP_TYPE:
91 priv->type = g_value_get_uint (value);
92 break;
93
94 default:
95 /* We don't have any other property... */
96 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
97 break;
98 }
99}
100
101static void
102terraserver_map_source_get_property (GObject *object,
103 guint property_id,
104 GValue *value,
105 GParamSpec *pspec)
106{
107 TerraserverMapSource *self = TERRASERVER_MAP_SOURCE (object);
108 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE (self);
109
110 switch (property_id)
111 {
112 case PROP_TYPE:
113 g_value_set_uint (value, priv->type);
114 break;
115
116 default:
117 /* We don't have any other property... */
118 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
119 break;
120 }
121}
122
c9c9e658
GB
123static void
124terraserver_map_source_class_init (TerraserverMapSourceClass *klass)
125{
126 GObjectClass* object_class = G_OBJECT_CLASS (klass);
a3319e11 127 VikMapSourceClass* grandparent_class = VIK_MAP_SOURCE_CLASS (klass);
f66ea3ec 128 VikMapSourceDefaultClass* parent_class = VIK_MAP_SOURCE_DEFAULT_CLASS (klass);
e76eccdc
GB
129 GParamSpec *pspec = NULL;
130
131 object_class->set_property = terraserver_map_source_set_property;
132 object_class->get_property = terraserver_map_source_get_property;
133
c9c9e658 134 /* Overiding methods */
a3319e11
RN
135 grandparent_class->coord_to_mapcoord = _coord_to_mapcoord;
136 grandparent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
a0262d5b 137 grandparent_class->is_direct_file_access = _is_direct_file_access;
0fb11294 138 grandparent_class->is_mbtiles = _is_mbtiles;
f66ea3ec
GB
139
140 parent_class->get_uri = _get_uri;
141 parent_class->get_hostname = _get_hostname;
142 parent_class->get_download_options = _get_download_options;
e76eccdc
GB
143
144 pspec = g_param_spec_uint ("type",
145 "Type",
146 "Type of Terraserver map",
147 0 /* minimum value */,
148 G_MAXUINT8 /* maximum value */,
149 0 /* default value */,
150 G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
151 g_object_class_install_property (object_class, PROP_TYPE, pspec);
152
c9c9e658
GB
153 g_type_class_add_private (klass, sizeof (TerraserverMapSourcePrivate));
154
155 object_class->finalize = terraserver_map_source_finalize;
156}
157
ef06c03a 158#define TERRASERVER_SITE "msrmaps.com"
c9c9e658
GB
159#define MARGIN_OF_ERROR 0.001
160
161static int mpp_to_scale ( gdouble mpp, guint8 type )
162{
163 mpp *= 4;
164 gint t = (gint) mpp;
165 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
166 return FALSE;
167
168 switch ( t ) {
169 case 1: return (type == 4) ? 8 : 0;
170 case 2: return (type == 4) ? 9 : 0;
171 case 4: return (type != 2) ? 10 : 0;
172 case 8: return 11;
173 case 16: return 12;
174 case 32: return 13;
175 case 64: return 14;
176 case 128: return 15;
177 case 256: return 16;
178 case 512: return 17;
179 case 1024: return 18;
180 case 2048: return 19;
181 default: return 0;
182 }
183}
184
185static gdouble scale_to_mpp ( gint scale )
186{
187 return pow(2,scale - 10);
188}
189
190static gboolean
191_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
192{
193 g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
194
195 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self);
196 int type = priv->type;
d3232158 197 if ( src->mode != VIK_COORD_UTM )
6c83cd7c 198 return FALSE;
c9c9e658
GB
199
200 if ( xmpp != ympp )
201 return FALSE;
202
203 dest->scale = mpp_to_scale ( xmpp, type );
204 if ( ! dest->scale )
205 return FALSE;
206
207 dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
208 dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
209 dest->z = src->utm_zone;
210 return TRUE;
211}
212
a0262d5b
RN
213static gboolean
214_is_direct_file_access ( VikMapSource *self )
215{
216 return FALSE;
217}
218
0fb11294
RN
219static gboolean
220_is_mbtiles ( VikMapSource *self )
221{
222 return FALSE;
223}
224
c9c9e658
GB
225static void
226_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
227{
228 // FIXME: slowdown here!
229 gdouble mpp = scale_to_mpp ( src->scale );
230 dest->mode = VIK_COORD_UTM;
231 dest->utm_zone = src->z;
232 dest->east_west = ((src->x * 200) + 100) * mpp;
233 dest->north_south = ((src->y * 200) + 100) * mpp;
234}
235
f66ea3ec
GB
236static gchar *
237_get_uri( VikMapSourceDefault *self, MapCoord *src )
c9c9e658 238{
f66ea3ec 239 g_return_val_if_fail (TERRASERVER_IS_MAP_SOURCE(self), NULL);
c9c9e658 240
f66ea3ec 241 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self);
c9c9e658
GB
242 int type = priv->type;
243 gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
244 src->scale, src->x, src->y, src->z );
f66ea3ec
GB
245 return uri;
246}
c9c9e658 247
f66ea3ec
GB
248static gchar *
249_get_hostname( VikMapSourceDefault *self )
825413ba 250{
f66ea3ec
GB
251 g_return_val_if_fail (TERRASERVER_IS_MAP_SOURCE(self), NULL);
252
253 return g_strdup( TERRASERVER_SITE );
825413ba
SW
254}
255
686baff0 256static DownloadFileOptions *
f66ea3ec 257_get_download_options( VikMapSourceDefault *self )
825413ba 258{
f66ea3ec
GB
259 g_return_val_if_fail (TERRASERVER_IS_MAP_SOURCE(self), NULL);
260
261 return &terraserver_options;
825413ba
SW
262}
263
c9c9e658 264TerraserverMapSource *
d7e495b2 265terraserver_map_source_new_with_id (guint16 id, const char *label, int type)
c9c9e658 266{
82aa018d
GB
267 char *copyright = NULL;
268 switch (type)
269 {
270 case 1:
271 copyright = "© DigitalGlobe";
272 break;
273 case 2:
274 copyright = "© LandVoyage";
275 break;
276 case 4:
277 copyright = "© DigitalGlobe";
278 break;
279 default:
280 g_critical("Houston, we've had a problem. type=%d", type);
281 }
282
283 return g_object_new(TERRASERVER_TYPE_MAP_SOURCE,
284 "id", id, "label", label, "type", type,
285 "copyright", copyright,
286 NULL);
825413ba 287}