]> git.street.me.uk Git - andy/viking.git/blame - src/terraservermapsource.c
GObjectify VikViewportDrawMode enum
[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
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.
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 );
33
34/* FIXME Huge gruik */
35static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
36
37typedef struct _TerraserverMapSourcePrivate TerraserverMapSourcePrivate;
38struct _TerraserverMapSourcePrivate
39{
40 int type;
41};
42
43#define TERRASERVER_MAP_SOURCE_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TERRASERVER_TYPE_MAP_SOURCE, TerraserverMapSourcePrivate))
44
45G_DEFINE_TYPE_EXTENDED (TerraserverMapSource, terraserver_map_source, VIK_TYPE_MAP_SOURCE_DEFAULT, (GTypeFlags)0,);
46
47static void
3a84e537 48terraserver_map_source_init (TerraserverMapSource *self)
c9c9e658
GB
49{
50 /* initialize the object here */
3a84e537
GB
51 vik_map_source_default_set_tilesize_x (VIK_MAP_SOURCE_DEFAULT (self), 200);
52 vik_map_source_default_set_tilesize_y (VIK_MAP_SOURCE_DEFAULT (self), 200);
53 vik_map_source_default_set_drawmode (VIK_MAP_SOURCE_DEFAULT (self), VIK_VIEWPORT_DRAWMODE_UTM);
c9c9e658
GB
54}
55
56static void
57terraserver_map_source_finalize (GObject *object)
58{
59 /* TODO: Add deinitalization code here */
60
61 G_OBJECT_CLASS (terraserver_map_source_parent_class)->finalize (object);
62}
63
64static void
65terraserver_map_source_class_init (TerraserverMapSourceClass *klass)
66{
67 GObjectClass* object_class = G_OBJECT_CLASS (klass);
68 VikMapSourceClass* parent_class = VIK_MAP_SOURCE_CLASS (klass);
69
70 /* Overiding methods */
71 parent_class->coord_to_mapcoord = _coord_to_mapcoord;
72 parent_class->mapcoord_to_center_coord = _mapcoord_to_center_coord;
73 parent_class->download = _download;
74
75 g_type_class_add_private (klass, sizeof (TerraserverMapSourcePrivate));
76
77 object_class->finalize = terraserver_map_source_finalize;
78}
79
80#define TERRASERVER_SITE "terraserver-usa.com"
81#define MARGIN_OF_ERROR 0.001
82
83static int mpp_to_scale ( gdouble mpp, guint8 type )
84{
85 mpp *= 4;
86 gint t = (gint) mpp;
87 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
88 return FALSE;
89
90 switch ( t ) {
91 case 1: return (type == 4) ? 8 : 0;
92 case 2: return (type == 4) ? 9 : 0;
93 case 4: return (type != 2) ? 10 : 0;
94 case 8: return 11;
95 case 16: return 12;
96 case 32: return 13;
97 case 64: return 14;
98 case 128: return 15;
99 case 256: return 16;
100 case 512: return 17;
101 case 1024: return 18;
102 case 2048: return 19;
103 default: return 0;
104 }
105}
106
107static gdouble scale_to_mpp ( gint scale )
108{
109 return pow(2,scale - 10);
110}
111
112static gboolean
113_coord_to_mapcoord ( VikMapSource *self, const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
114{
115 g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
116
117 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self);
118 int type = priv->type;
119 g_assert ( src->mode == VIK_COORD_UTM );
120
121 if ( xmpp != ympp )
122 return FALSE;
123
124 dest->scale = mpp_to_scale ( xmpp, type );
125 if ( ! dest->scale )
126 return FALSE;
127
128 dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
129 dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
130 dest->z = src->utm_zone;
131 return TRUE;
132}
133
134static void
135_mapcoord_to_center_coord ( VikMapSource *self, MapCoord *src, VikCoord *dest )
136{
137 // FIXME: slowdown here!
138 gdouble mpp = scale_to_mpp ( src->scale );
139 dest->mode = VIK_COORD_UTM;
140 dest->utm_zone = src->z;
141 dest->east_west = ((src->x * 200) + 100) * mpp;
142 dest->north_south = ((src->y * 200) + 100) * mpp;
143}
144
145static int
146_download ( VikMapSource *self, MapCoord *src, const gchar *dest_fn )
147{
148 g_return_val_if_fail(TERRASERVER_IS_MAP_SOURCE(self), FALSE);
149
150 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(self); int res = -1;
151 int type = priv->type;
152 gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
153 src->scale, src->x, src->y, src->z );
154 res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
155 g_free ( uri );
156 return res;
157}
158
159TerraserverMapSource *
160terraserver_map_source_new_with_id (guint8 id, int type)
161{
162 TerraserverMapSource *ret = g_object_new(TERRASERVER_TYPE_MAP_SOURCE, NULL);
163
3a84e537
GB
164 /* TODO use property */
165 vik_map_source_default_set_uniq_id(VIK_MAP_SOURCE_DEFAULT(ret), id);
166
167 TerraserverMapSourcePrivate *priv = TERRASERVER_MAP_SOURCE_PRIVATE(ret);
c9c9e658
GB
168 priv->type = type;
169 return ret;
170}