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