libcurl is a requirement
[viking/guyou.git] / src / terraserver.c
blob85d7a61a8b55b1f8e8d3afdb2e3131b32a9d8171
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2003-2005, Evan Battaglia <gtoevan@gmx.net>
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.
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.
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
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"
31 #include "terraserver.h"
33 static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
34 static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn );
36 static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
37 static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn );
39 static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
40 static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn );
42 static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
44 static DownloadOptions terraserver_options = { NULL };
46 void terraserver_init () {
47 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 };
48 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 };
49 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 };
51 maps_layer_register_type("Terraserver Topos", 2, &map_type_1);
52 maps_layer_register_type("Terraserver Aerials", 1, &map_type_2);
53 maps_layer_register_type("Terraserver Urban Areas", 4, &map_type_3);
56 #define TERRASERVER_SITE "terraserver-usa.com"
57 #define MARGIN_OF_ERROR 0.001
59 static int mpp_to_scale ( gdouble mpp, guint8 type )
61 mpp *= 4;
62 gint t = (gint) mpp;
63 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
64 return FALSE;
66 switch ( t ) {
67 case 1: return (type == 4) ? 8 : 0;
68 case 2: return (type == 4) ? 9 : 0;
69 case 4: return (type != 2) ? 10 : 0;
70 case 8: return 11;
71 case 16: return 12;
72 case 32: return 13;
73 case 64: return 14;
74 case 128: return 15;
75 case 256: return 16;
76 case 512: return 17;
77 case 1024: return 18;
78 case 2048: return 19;
79 default: return 0;
83 static gdouble scale_to_mpp ( gint scale )
85 return pow(2,scale - 10);
88 static gboolean terraserver_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest, guint8 type )
90 g_assert ( src->mode == VIK_COORD_UTM );
92 if ( xmpp != ympp )
93 return FALSE;
95 dest->scale = mpp_to_scale ( xmpp, type );
96 if ( ! dest->scale )
97 return FALSE;
99 dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
100 dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
101 dest->z = src->utm_zone;
102 return TRUE;
105 static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
106 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 2 ); }
107 static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
108 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 1 ); }
109 static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
110 { return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 4 ); }
112 static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
114 // FIXME: slowdown here!
115 gdouble mpp = scale_to_mpp ( src->scale );
116 dest->mode = VIK_COORD_UTM;
117 dest->utm_zone = src->z;
118 dest->east_west = ((src->x * 200) + 100) * mpp;
119 dest->north_south = ((src->y * 200) + 100) * mpp;
122 static int terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
124 int res = -1;
125 gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
126 src->scale, src->x, src->y, src->z );
127 res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
128 g_free ( uri );
129 return(res);
132 static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn )
133 { return terraserver_download ( src, dest_fn, 2 ); }
134 static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn )
135 { return terraserver_download ( src, dest_fn, 1 ); }
136 static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn )
137 { return terraserver_download ( src, dest_fn, 4 ); }