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