]> git.street.me.uk Git - andy/viking.git/blame - src/terraserver.c
Initial revision
[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 */
21
22#include <gtk/gtk.h>
23#include <math.h>
24#include "coords.h"
25#include "vikcoord.h"
26#include "mapcoord.h"
27#include "http.h"
28
29#define TERRASERVER_SITE "terraserver-usa.com"
30#define MARGIN_OF_ERROR 0.001
31
32static int mpp_to_scale ( gdouble mpp, guint8 type )
33{
34 mpp *= 4;
35 gint t = (gint) mpp;
36 if ( ABS(mpp - t) > MARGIN_OF_ERROR )
37 return FALSE;
38
39 switch ( t ) {
40 case 1: return (type == 4) ? 8 : 0;
41 case 2: return (type == 4) ? 9 : 0;
42 case 4: return (type != 2) ? 10 : 0;
43 case 8: return 11;
44 case 16: return 12;
45 case 32: return 13;
46 case 64: return 14;
47 case 128: return 15;
48 case 256: return 16;
49 case 512: return 17;
50 case 1024: return 18;
51 case 2048: return 19;
52 default: return 0;
53 }
54}
55
56static gdouble scale_to_mpp ( gint scale )
57{
58 return pow(2,scale - 10);
59}
60
61static gboolean terraserver_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest, guint8 type )
62{
63 g_assert ( src->mode == VIK_COORD_UTM );
64
65 if ( xmpp != ympp )
66 return FALSE;
67
68 dest->scale = mpp_to_scale ( xmpp, type );
69 if ( ! dest->scale )
70 return FALSE;
71
72 dest->x = (gint)(((gint)(src->east_west))/(200*xmpp));
73 dest->y = (gint)(((gint)(src->north_south))/(200*xmpp));
74 dest->z = src->utm_zone;
75 return TRUE;
76}
77
78gboolean terraserver_topo_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
79{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 2 ); }
80gboolean terraserver_aerial_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
81{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 1 ); }
82gboolean terraserver_urban_coord_to_mapcoord ( const VikCoord *src, gdouble xmpp, gdouble ympp, MapCoord *dest )
83{ return terraserver_coord_to_mapcoord ( src, xmpp, ympp, dest, 4 ); }
84
85void terraserver_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
86{
87 // FIXME: slowdown here!
88 gdouble mpp = scale_to_mpp ( src->scale );
89 dest->mode = VIK_COORD_UTM;
90 dest->utm_zone = src->z;
91 dest->east_west = ((src->x * 200) + 100) * mpp;
92 dest->north_south = ((src->y * 200) + 100) * mpp;
93}
94
95static void terraserver_download ( MapCoord *src, const gchar *dest_fn, guint8 type )
96{
97 gchar *uri = g_strdup_printf ( "/tile.ashx?T=%d&S=%d&X=%d&Y=%d&Z=%d", type,
98 src->scale, src->x, src->y, src->z );
99 a_http_download_get_url_nohostname ( TERRASERVER_SITE, uri, dest_fn );
100 g_free ( uri );
101}
102
103void terraserver_topo_download ( MapCoord *src, const gchar *dest_fn )
104{ terraserver_download ( src, dest_fn, 2 ); }
105void terraserver_aerial_download ( MapCoord *src, const gchar *dest_fn )
106{ terraserver_download ( src, dest_fn, 1 ); }
107void terraserver_urban_download ( MapCoord *src, const gchar *dest_fn )
108{ terraserver_download ( src, dest_fn, 4 ); }