]> git.street.me.uk Git - andy/viking.git/blob - src/expedia.c
Put vikutils.h into viking.h
[andy/viking.git] / src / expedia.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2005, Evan Battaglia <viking@greentorch.org>
5  *
6  * Some formulas or perhaps even code derived from GPSDrive
7  * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  *
23  */
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #ifdef HAVE_MATH_H
31 #include <math.h>
32 #endif
33
34 #include "map_ids.h"
35 #include "globals.h"
36 #include "coords.h"
37 #include "vikcoord.h"
38 #include "mapcoord.h"
39 #include "download.h"
40 #include "vikmapslayer.h"
41
42 #include "expedia.h"
43
44 static gboolean expedia_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
45 static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
46 static int expedia_download ( MapCoord *src, const gchar *dest_fn, void *handle );
47 static void * expedia_handle_init ( );
48 static void expedia_handle_cleanup ( void *handle );
49
50 static DownloadFileOptions expedia_options = { FALSE, FALSE, NULL, 2, a_check_map_file, NULL };
51
52 void expedia_init() {
53   VikMapsLayer_MapType map_type = { MAP_ID_EXPEDIA, 0, 0, VIK_VIEWPORT_DRAWMODE_EXPEDIA, expedia_coord_to_mapcoord, expedia_mapcoord_to_center_coord, expedia_download, expedia_handle_init, expedia_handle_cleanup };
54   maps_layer_register_type(_("Expedia Street Maps"), MAP_ID_EXPEDIA, &map_type);
55 }
56
57 #define EXPEDIA_SITE "expedia.com"
58 #define MPP_MARGIN_OF_ERROR 0.01
59 #define DEGREES_TO_RADS 0.0174532925
60 #define HEIGHT_OF_LAT_DEGREE (111318.84502/ALTI_TO_MPP)
61 #define HEIGHT_OF_LAT_MINUTE (1855.3140837/ALTI_TO_MPP)
62 #define WIDTH_BUFFER 0
63 #define HEIGHT_BUFFER 25
64 #define REAL_WIDTH_BUFFER 1
65 #define REAL_HEIGHT_BUFFER 26
66
67 /* first buffer is to cut off the expedia/microsoft logo. Annoying little buggers ;) */
68 /* second is to allow for a 1-pixel overlap on each side. this is a good thing (tm) */
69
70 static const guint expedia_altis[]              = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
71 /* square this number to find out how many per square degree. */
72 static const gdouble expedia_altis_degree_freq[]  = { 120, 60, 30, 15, 8, 4, 2, 1, 1, 1 };
73 static const guint expedia_altis_count = sizeof(expedia_altis) / sizeof(expedia_altis[0]);
74
75 gdouble expedia_altis_freq ( gint alti )
76 {
77   static gint i;
78   for ( i = 0; i < expedia_altis_count; i++ )
79     if ( expedia_altis[i] == alti )
80       return expedia_altis_degree_freq [ i ];
81
82   g_error ( _("Invalid expedia altitude") );
83   return 0;
84 }
85
86 /* returns -1 if none of the above. */
87 gint expedia_zoom_to_alti ( gdouble zoom )
88 {
89   guint i;
90   for ( i = 0; i < expedia_altis_count; i++ )
91     if ( fabs(expedia_altis[i] - zoom) / zoom < MPP_MARGIN_OF_ERROR )
92       return expedia_altis[i];
93   return -1;
94 }
95
96 /*
97 gint expedia_pseudo_zone ( gint alti, gint x, gint y )
98 {
99   return (int) (x/expedia_altis_freq(alti)*180) + (int) (y/expedia_altis_freq(alti)*90);
100 }
101 */
102
103 void expedia_snip ( const gchar *file )
104 {
105   /* Load the pixbuf */
106   GError *gx = NULL;
107   GdkPixbuf *old, *cropped;
108   gint width, height;
109
110   old = gdk_pixbuf_new_from_file ( file, &gx );
111   if (gx)
112   {
113     g_warning ( _("Couldn't open EXPEDIA image file (right after successful download! Please report and delete image file!): %s"), gx->message );
114     g_error_free ( gx );
115     return;
116   }
117
118   width = gdk_pixbuf_get_width ( old );
119   height = gdk_pixbuf_get_height ( old );
120
121   cropped = gdk_pixbuf_new_subpixbuf ( old, WIDTH_BUFFER, HEIGHT_BUFFER,
122                               width - 2*WIDTH_BUFFER, height - 2*HEIGHT_BUFFER );
123
124   gdk_pixbuf_save ( cropped, file, "png", &gx, NULL );
125   if ( gx ) {
126     g_warning ( _("Couldn't save EXPEDIA image file (right after successful download! Please report and delete image file!): %s"), gx->message );
127     g_error_free ( gx );
128   }
129
130   g_object_unref ( cropped );
131   g_object_unref ( old );
132 }
133
134 /* if degree_freeq = 60 -> nearest minute (in middle) */
135 /* everything starts at -90,-180 -> 0,0. then increments by (1/degree_freq) */
136 static gboolean expedia_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
137 {
138   gint alti;
139
140   g_assert ( src->mode == VIK_COORD_LATLON );
141
142   if ( xzoom != yzoom )
143     return FALSE;
144
145   alti = expedia_zoom_to_alti ( xzoom );
146   if ( alti != -1 )
147   {
148     dest->scale = alti;
149     dest->x = (int) (((src->east_west+180) * expedia_altis_freq(alti))+0.5);
150     dest->y = (int) (((src->north_south+90) * expedia_altis_freq(alti))+0.5);
151     /* + 0.5 to round off and not floor */
152
153     /* just to space out tiles on the filesystem */
154     dest->z = 0;
155     return TRUE;
156   }
157   return FALSE;
158 }
159
160 void expedia_xy_to_latlon_middle ( gint alti, gint x, gint y, struct LatLon *ll )
161 {
162   ll->lon = (((gdouble)x) / expedia_altis_freq(alti)) - 180;
163   ll->lat = (((gdouble)y) / expedia_altis_freq(alti)) - 90;
164 }
165
166 static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
167 {
168   dest->mode = VIK_COORD_LATLON;
169   dest->east_west = (((gdouble)src->x) / expedia_altis_freq(src->scale)) - 180;
170   dest->north_south = (((gdouble)src->y) / expedia_altis_freq(src->scale)) - 90;
171 }
172
173 static DownloadResult_t expedia_download ( MapCoord *src, const gchar *dest_fn, void *handle )
174 {
175   gint height, width;
176   struct LatLon ll;
177   gchar *uri;
178
179   expedia_xy_to_latlon_middle ( src->scale, src->x, src->y, &ll );
180
181   height = HEIGHT_OF_LAT_DEGREE / expedia_altis_freq(src->scale) / (src->scale);
182   width = height * cos ( ll.lat * DEGREES_TO_RADS );
183
184   height += 2*REAL_HEIGHT_BUFFER;
185   width  += 2*REAL_WIDTH_BUFFER;
186
187   uri = g_strdup_printf ( "/pub/agent.dll?qscr=mrdt&ID=3XNsF.&CenP=%lf,%lf&Lang=%s&Alti=%d&Size=%d,%d&Offs=0.000000,0.000000&BCheck&tpid=1",
188                ll.lat, ll.lon, (ll.lon > -30) ? "EUR0809" : "USA0409", src->scale, width, height );
189
190   DownloadResult_t res = a_http_download_get_url ( EXPEDIA_SITE, uri, dest_fn, &expedia_options, NULL );
191   if (res == DOWNLOAD_SUCCESS)
192         expedia_snip ( dest_fn );
193   g_free(uri);
194   return(res);
195 }
196
197 static void * expedia_handle_init ( )
198 {
199   // Not much going on here
200   return 0;
201 }
202
203 static void expedia_handle_cleanup ( void *handle )
204 {
205   // Even less here!
206 }