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