]> git.street.me.uk Git - andy/viking.git/blob - src/terraserver.c
Allow to select between Google and Geonames for searching method
[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 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <gtk/gtk.h>
26 #ifdef HAVE_MATH_H
27 #include <math.h>
28 #endif
29
30 #include "viking.h"
31 #include "coords.h"
32 #include "vikcoord.h"
33 #include "mapcoord.h"
34 #include "download.h"
35 #include "vikmapslayer.h"
36
37 #include "terraserver.h"
38
39 static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
40 static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn );
41
42 static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
43 static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn );
44
45 static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
46 static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn );
47
48 static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
49
50 static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
51
52 void terraserver_init () {
53   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 };
54   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 };
55   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 };
56
57   maps_layer_register_type("Terraserver Topos", 2, &map_type_1);
58   maps_layer_register_type("Terraserver Aerials", 1, &map_type_2);
59   maps_layer_register_type("Terraserver Urban Areas", 4, &map_type_3);
60 }
61
62 #define TERRASERVER_SITE "terraserver-usa.com"
63 #define MARGIN_OF_ERROR 0.001
64
65 static int mpp_to_scale ( gdouble mpp, guint8 type )
66 {
67   mpp *= 4;
68   gint t = (gint) mpp;
69   if ( ABS(mpp - t) > MARGIN_OF_ERROR )
70     return FALSE;
71
72   switch ( t ) {
73     case 1: return (type == 4) ? 8 : 0;
74     case 2: return (type == 4) ? 9 : 0;
75     case 4: return (type != 2) ? 10 : 0;
76     case 8: return 11;
77     case 16: return 12;
78     case 32: return 13;
79     case 64: return 14;
80     case 128: return 15;
81     case 256: return 16;
82     case 512: return 17;
83     case 1024: return 18;
84     case 2048: return 19;
85     default: return 0;
86   }
87 }
88
89 static gdouble scale_to_mpp ( gint scale )
90 {
91   return pow(2,scale - 10);
92 }
93
94 static gboolean terraserver_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest, guint8 type )
95 {
96   g_assert ( src->mode == VIK_COORD_UTM );
97
98   if ( xmpp != ympp )
99     return FALSE;
100
101   dest->scale = mpp_to_scale ( xmpp, type );
102   if ( ! dest->scale )
103     return FALSE;
104
105   dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
106   dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
107   dest->z = src->utm_zone;
108   return TRUE;
109 }
110
111 static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
112 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 2 ); }
113 static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
114 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 1 ); }
115 static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
116 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 4 ); }
117
118 static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
119 {
120   // FIXME: slowdown here!
121   gdouble mpp = scale_to_mpp ( src->scale );
122   dest->mode = VIK_COORD_UTM;
123   dest->utm_zone = src->z;
124   dest->east_west = ((src->x * 200) + 100) * mpp;
125   dest->north_south = ((src->y * 200) + 100) * mpp;
126 }
127
128 static int terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
129 {
130   int res = -1;
131   gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
132                                   src->scale, src->x, src->y, src->z );
133   res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
134   g_free ( uri );
135   return(res);
136 }
137
138 static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn )
139 { return terraserver_download ( src, dest_fn, 2 ); }
140 static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn )
141 { return terraserver_download ( src, dest_fn, 1 ); }
142 static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn )
143 { return terraserver_download ( src, dest_fn, 4 ); }