--- /dev/null
+%headertop{
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#endif
+
+#include "vikcoord.h"
+#include "mapcoord.h"
+#include "vik-map-type.h"
+%}
+
+%{
+#ifdef HAVE_MATH_H
+#include <math.h>
+#endif
+
+#include "download.h"
+%}
+
+%h{
+
+%}
+
+%{
+
+static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
+
+#define TERRASERVER_SITE "terraserver-usa.com"
+#define MARGIN_OF_ERROR 0.001
+
+static int mpp_to_scale ( gdouble mpp, guint8 type )
+{
+ mpp *= 4;
+ gint t = (gint) mpp;
+ if ( ABS(mpp - t) > MARGIN_OF_ERROR )
+ return FALSE;
+
+ switch ( t ) {
+ case 1: return (type == 4) ? 8 : 0;
+ case 2: return (type == 4) ? 9 : 0;
+ case 4: return (type != 2) ? 10 : 0;
+ case 8: return 11;
+ case 16: return 12;
+ case 32: return 13;
+ case 64: return 14;
+ case 128: return 15;
+ case 256: return 16;
+ case 512: return 17;
+ case 1024: return 18;
+ case 2048: return 19;
+ default: return 0;
+ }
+}
+
+static gdouble scale_to_mpp ( gint scale )
+{
+ return pow(2,scale - 10);
+}
+
+%}
+
+class Terraserver:Map:Type from Vik:Map:Type {
+ private int type;
+
+ override (Vik:Map:Type) gboolean
+ coord_to_mapcoord ( Vik:Map:Type *self, const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
+ {
+ int type = TERRASERVER_MAP_TYPE(self)->_priv->type;
+ g_assert ( src->mode == VIK_COORD_UTM );
+
+ if ( xmpp != ympp )
+ return FALSE;
+
+ dest->scale = mpp_to_scale ( xmpp, type );
+ if ( ! dest->scale )
+ return FALSE;
+
+ dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
+ dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
+ dest->z = src->utm_zone;
+ return TRUE;
+ }
+
+ override (Vik:Map:Type) void
+ mapcoord_to_center_coord ( Vik:Map:Type *self, MapCoord *src, VikCoord *dest )
+ {
+ // FIXME: slowdown here!
+ gdouble mpp = scale_to_mpp ( src->scale );
+ dest->mode = VIK_COORD_UTM;
+ dest->utm_zone = src->z;
+ dest->east_west = ((src->x * 200) + 100) * mpp;
+ dest->north_south = ((src->y * 200) + 100) * mpp;
+ }
+
+ override (Vik:Map:Type) int
+ download ( Vik:Map:Type *self, MapCoord *src, const gchar *dest_fn )
+ {
+ int res = -1;
+ int type = TERRASERVER_MAP_TYPE(self)->_priv->type;
+ gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
+ src->scale, src->x, src->y, src->z );
+ res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
+ g_free ( uri );
+ return(res);
+ }
+}
\ No newline at end of file