]> git.street.me.uk Git - andy/viking.git/blob - src/terraserver-map-type.gob
Merge vikslippymapsourceabstract and slippy-map-type
[andy/viking.git] / src / terraserver-map-type.gob
1 %headertop{
2 #ifdef HAVE_CONFIG_H
3 #include "config.h"
4 #endif
5
6 #include "vikcoord.h"
7 #include "mapcoord.h"
8 #include "vikmapsourcedefault.h"
9 %}
10
11 %{
12 #ifdef HAVE_MATH_H
13 #include <math.h>
14 #endif
15
16 #include "download.h"
17 #include "vikviewport.h"
18 %}
19
20 %h{
21
22 %}
23
24 %{
25
26 static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
27
28 #define TERRASERVER_SITE "terraserver-usa.com"
29 #define MARGIN_OF_ERROR 0.001
30
31 static int mpp_to_scale ( gdouble mpp, guint8 type )
32 {
33   mpp *= 4;
34   gint t = (gint) mpp;
35   if ( ABS(mpp - t) > MARGIN_OF_ERROR )
36     return FALSE;
37
38   switch ( t ) {
39     case 1: return (type == 4) ? 8 : 0;
40     case 2: return (type == 4) ? 9 : 0;
41     case 4: return (type != 2) ? 10 : 0;
42     case 8: return 11;
43     case 16: return 12;
44     case 32: return 13;
45     case 64: return 14;
46     case 128: return 15;
47     case 256: return 16;
48     case 512: return 17;
49     case 1024: return 18;
50     case 2048: return 19;
51     default: return 0;
52   }
53 }
54
55 static gdouble scale_to_mpp ( gint scale )
56 {
57   return pow(2,scale - 10);
58 }
59
60 %}
61
62 class Terraserver:Map:Type from Vik:Map:Source:Default {
63   private int type;
64
65   init (self) {
66           /* initialize the object here */
67           // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_x = 200;
68           // FIXME VIK_MAP_SOURCE_DEFAULT(self)->tilesize_y = 200;
69           // FIXME VIK_MAP_SOURCE_DEFAULT(self)->drawmode = VIK_VIEWPORT_DRAWMODE_UTM;
70   }
71
72   public GObject *
73   new_with_id (guint8 id, int type) {
74           TerraserverMapType *ret = GET_NEW;
75           // FIXME VIK_MAP_SOURCE_DEFAULT(ret)->uniq_id = id;
76           ret->_priv->type = type;
77           return G_OBJECT (ret);
78   }
79
80   override (Vik:Map:Source) gboolean
81   coord_to_mapcoord ( Vik:Map:Source *self, const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
82   {
83   int type = TERRASERVER_MAP_TYPE(self)->_priv->type;
84   g_assert ( src->mode == VIK_COORD_UTM );
85
86   if ( xmpp != ympp )
87     return FALSE;
88
89   dest->scale = mpp_to_scale ( xmpp, type );
90   if ( ! dest->scale )
91     return FALSE;
92
93   dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
94   dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
95   dest->z = src->utm_zone;
96   return TRUE;
97   }
98
99   override (Vik:Map:Source) void
100   mapcoord_to_center_coord ( Vik:Map:Source *self, MapCoord *src, VikCoord *dest )
101   {
102   // FIXME: slowdown here!
103   gdouble mpp = scale_to_mpp ( src->scale );
104   dest->mode = VIK_COORD_UTM;
105   dest->utm_zone = src->z;
106   dest->east_west = ((src->x * 200) + 100) * mpp;
107   dest->north_south = ((src->y * 200) + 100) * mpp;
108   }
109
110   override (Vik:Map:Source) int
111    download ( Vik:Map:Source *self, MapCoord *src, const gchar *dest_fn )
112   {
113   int res = -1;
114   int type = TERRASERVER_MAP_TYPE(self)->_priv->type;
115   gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
116                                   src->scale, src->x, src->y, src->z );
117   res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
118   g_free ( uri );
119   return(res);
120   }
121 }