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