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