]> git.street.me.uk Git - andy/viking.git/blame - src/terraserver.c
Add configuration options for OpenAerial and BlueMarble
[andy/viking.git] / src / terraserver.c
CommitLineData
50a14534
EB
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 */
8c00358d
GB
21#ifdef HAVE_CONFIG_H
22#include "config.h"
23#endif
50a14534
EB
24
25#include <gtk/gtk.h>
8c00358d 26#ifdef HAVE_MATH_H
50a14534 27#include <math.h>
8c00358d
GB
28#endif
29
79845167 30#include "viking.h"
50a14534
EB
31#include "coords.h"
32#include "vikcoord.h"
33#include "mapcoord.h"
85611cd9 34#include "download.h"
cdcaf41c
QT
35#include "vikmapslayer.h"
36
37#include "terraserver.h"
38
02f4eaf7
QT
39static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
40static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn );
41
42static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
43static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn );
44
45static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest );
46static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn );
47
48static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
49
1ac37c09 50static DownloadOptions terraserver_options = { NULL, 0, a_check_map_file };
80214df2 51
cdcaf41c
QT
52void 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}
50a14534
EB
61
62#define TERRASERVER_SITE "terraserver-usa.com"
63#define MARGIN_OF_ERROR 0.001
64
65static 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
89static gdouble scale_to_mpp ( gint scale )
90{
91 return pow(2,scale - 10);
92}
93
94static 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
02f4eaf7 111static gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
50a14534 112{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 2 ); }
02f4eaf7 113static gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
50a14534 114{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 1 ); }
02f4eaf7 115static gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
50a14534
EB
116{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 4 ); }
117
02f4eaf7 118static void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
50a14534
EB
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
02f4eaf7 128static int terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
50a14534 129{
02f4eaf7 130 int res = -1;
50a14534
EB
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 );
80214df2 133 res = a_http_download_get_url ( TERRASERVER_SITE, uri, dest_fn, &terraserver_options );
50a14534 134 g_free ( uri );
02f4eaf7 135 return(res);
50a14534
EB
136}
137
02f4eaf7
QT
138static int terraserver_topo_download ( MapCoord *src, const gchar *dest_fn )
139{ return terraserver_download ( src, dest_fn, 2 ); }
140static int terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn )
141{ return terraserver_download ( src, dest_fn, 1 ); }
142static int terraserver_urban_download ( MapCoord *src, const gchar *dest_fn )
143{ return terraserver_download ( src, dest_fn, 4 ); }