]> git.street.me.uk Git - andy/viking.git/blob - src/terraserver.c
Introducing download module
[andy/viking.git] / src / terraserver.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <gtk/gtk.h>
23 #include <math.h>
24 #include "viking.h"
25 #include "coords.h"
26 #include "vikcoord.h"
27 #include "mapcoord.h"
28 #include "download.h"
29 #include "vikmapslayer.h"
30
31 #include "terraserver.h"
32
33 void terraserver_init () {
34   VikMapsLayer_MapType map_type_1 = { 2, 200, 200, VIK_VIEWPORT_DRAWMODE_UTM, terraserver_topo_coord_to_mapcoord, terraserver_mapcoord_to_center_coord, terraserver_topo_download };
35   VikMapsLayer_MapType map_type_2 = { 1, 200, 200, VIK_VIEWPORT_DRAWMODE_UTM, terraserver_aerial_coord_to_mapcoord, terraserver_mapcoord_to_center_coord, terraserver_aerial_download };
36   VikMapsLayer_MapType map_type_3 = { 4, 200, 200, VIK_VIEWPORT_DRAWMODE_UTM, terraserver_urban_coord_to_mapcoord, terraserver_mapcoord_to_center_coord, terraserver_urban_download };
37
38   maps_layer_register_type("Terraserver Topos", 2, &map_type_1);
39   maps_layer_register_type("Terraserver Aerials", 1, &map_type_2);
40   maps_layer_register_type("Terraserver Urban Areas", 4, &map_type_3);
41 }
42
43 #define TERRASERVER_SITE "terraserver-usa.com"
44 #define MARGIN_OF_ERROR 0.001
45
46 static int mpp_to_scale ( gdouble mpp, guint8 type )
47 {
48   mpp *= 4;
49   gint t = (gint) mpp;
50   if ( ABS(mpp - t) > MARGIN_OF_ERROR )
51     return FALSE;
52
53   switch ( t ) {
54     case 1: return (type == 4) ? 8 : 0;
55     case 2: return (type == 4) ? 9 : 0;
56     case 4: return (type != 2) ? 10 : 0;
57     case 8: return 11;
58     case 16: return 12;
59     case 32: return 13;
60     case 64: return 14;
61     case 128: return 15;
62     case 256: return 16;
63     case 512: return 17;
64     case 1024: return 18;
65     case 2048: return 19;
66     default: return 0;
67   }
68 }
69
70 static gdouble scale_to_mpp ( gint scale )
71 {
72   return pow(2,scale - 10);
73 }
74
75 static gboolean terraserver_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest, guint8 type )
76 {
77   g_assert ( src->mode == VIK_COORD_UTM );
78
79   if ( xmpp != ympp )
80     return FALSE;
81
82   dest->scale = mpp_to_scale ( xmpp, type );
83   if ( ! dest->scale )
84     return FALSE;
85
86   dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
87   dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
88   dest->z = src->utm_zone;
89   return TRUE;
90 }
91
92 gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
93 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 2 ); }
94 gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
95 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 1 ); }
96 gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
97 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 4 ); }
98
99 void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
100 {
101   // FIXME: slowdown here!
102   gdouble mpp = scale_to_mpp ( src->scale );
103   dest->mode = VIK_COORD_UTM;
104   dest->utm_zone = src->z;
105   dest->east_west = ((src->x * 200) + 100) * mpp;
106   dest->north_south = ((src->y * 200) + 100) * mpp;
107 }
108
109 static void terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
110 {
111   gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
112                                   src->scale, src->x, src->y, src->z );
113   a_http_download_get_url_nohostname ( TERRASERVER_SITE, uri, dest_fn );
114   g_free ( uri );
115 }
116
117 void terraserver_topo_download ( MapCoord *src, const gchar *dest_fn )
118 { terraserver_download ( src, dest_fn, 2 ); }
119 void terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn )
120 { terraserver_download ( src, dest_fn, 1 ); }
121 void terraserver_urban_download ( MapCoord *src, const gchar *dest_fn )
122 { terraserver_download ( src, dest_fn, 4 ); }