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
29 #include "vikmapslayer.h"
31 #include "terraserver.h"
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 };
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);
43 #define TERRASERVER_SITE "terraserver-usa.com"
44 #define MARGIN_OF_ERROR 0.001
46 static int mpp_to_scale ( gdouble mpp, guint8 type )
50 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
54 case 1: return (type == 4) ? 8 : 0;
55 case 2: return (type == 4) ? 9 : 0;
56 case 4: return (type != 2) ? 10 : 0;
70 static gdouble scale_to_mpp ( gint scale )
72 return pow(2,scale - 10);
75 static gboolean terraserver_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest, guint8 type )
77 g_assert ( src->mode == VIK_COORD_UTM );
82 dest->scale = mpp_to_scale ( xmpp, type );
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;
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 ); }
99 void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
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;
109 static void terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
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 );
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 ); }