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