libcurl is a requirement
[viking/guyou.git] / src / expedia.c
blob93783e274791ec7b24a19b10b9bcef0e8fce2bc1
1 /*
2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2005, Evan Battaglia <viking@greentorch.org>
6 * Some formulas or perhaps even code derived from GPSDrive
7 * GPSDrive Copyright (C) 2001-2004 Fritz Ganter <ganter@ganter.at>
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.
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.
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
25 #include <gtk/gtk.h>
26 #include <math.h>
27 #include "globals.h"
28 #include "coords.h"
29 #include "vikcoord.h"
30 #include "mapcoord.h"
31 #include "download.h"
32 #include "vikmapslayer.h"
34 #include "expedia.h"
36 static gboolean expedia_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest );
37 static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest );
38 static int expedia_download ( MapCoord *src, const gchar *dest_fn );
40 static DownloadOptions expedia_options = { NULL };
42 void expedia_init() {
43 VikMapsLayer_MapType map_type = { 5, 0, 0, VIK_VIEWPORT_DRAWMODE_EXPEDIA, expedia_coord_to_mapcoord, expedia_mapcoord_to_center_coord, expedia_download };
44 maps_layer_register_type("Expedia Street Maps", 5, &map_type);
47 #define EXPEDIA_SITE "expedia.com"
48 #define MPP_MARGIN_OF_ERROR 0.01
49 #define DEGREES_TO_RADS 0.0174532925
50 #define HEIGHT_OF_LAT_DEGREE (111318.84502/ALTI_TO_MPP)
51 #define HEIGHT_OF_LAT_MINUTE (1855.3140837/ALTI_TO_MPP)
52 #define WIDTH_BUFFER 0
53 #define HEIGHT_BUFFER 25
54 #define REAL_WIDTH_BUFFER 1
55 #define REAL_HEIGHT_BUFFER 26
57 /* first buffer is to cut off the expedia/microsoft logo. Annoying little buggers ;) */
58 /* second is to allow for a 1-pixel overlap on each side. this is a good thing (tm) */
60 static const guint expedia_altis[] = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512 };
61 /* square this number to find out how many per square degree. */
62 static const gdouble expedia_altis_degree_freq[] = { 120, 60, 30, 15, 8, 4, 2, 1, 1, 1 };
63 static const guint expedia_altis_count = sizeof(expedia_altis) / sizeof(expedia_altis[0]);
65 gdouble expedia_altis_freq ( gint alti )
67 static gint i;
68 for ( i = 0; i < expedia_altis_count; i++ )
69 if ( expedia_altis[i] == alti )
70 return expedia_altis_degree_freq [ i ];
72 g_error ( "Invalid expedia altitude" );
73 return 0;
76 /* returns -1 if none of the above. */
77 gint expedia_zoom_to_alti ( gdouble zoom )
79 guint i;
80 for ( i = 0; i < expedia_altis_count; i++ )
81 if ( fabs(expedia_altis[i] - zoom) / zoom < MPP_MARGIN_OF_ERROR )
82 return expedia_altis[i];
83 return -1;
87 gint expedia_pseudo_zone ( gint alti, gint x, gint y )
89 return (int) (x/expedia_altis_freq(alti)*180) + (int) (y/expedia_altis_freq(alti)*90);
93 void expedia_snip ( const gchar *file )
95 /* Load the pixbuf */
96 GError *gx = NULL;
97 GdkPixbuf *old, *cropped;
98 gint width, height;
100 old = gdk_pixbuf_new_from_file ( file, &gx );
101 if (gx)
103 g_warning ( "Couldn't open EXPEDIA image file (right after successful download! Please report and delete image file!): %s", gx->message );
104 g_error_free ( gx );
105 return;
108 width = gdk_pixbuf_get_width ( old );
109 height = gdk_pixbuf_get_height ( old );
111 cropped = gdk_pixbuf_new_subpixbuf ( old, WIDTH_BUFFER, HEIGHT_BUFFER,
112 width - 2*WIDTH_BUFFER, height - 2*HEIGHT_BUFFER );
114 gdk_pixbuf_save ( cropped, file, "png", &gx, NULL );
115 if ( gx ) {
116 g_warning ( "Couldn't save EXPEDIA image file (right after successful download! Please report and delete image file!): %s", gx->message );
117 g_error_free ( gx );
120 g_object_unref ( cropped );
121 g_object_unref ( old );
124 /* if degree_freeq = 60 -> nearest minute (in middle) */
125 /* everything starts at -90,-180 -> 0,0. then increments by (1/degree_freq) */
126 static gboolean expedia_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
128 gint alti;
130 g_assert ( src->mode == VIK_COORD_LATLON );
132 if ( xzoom != yzoom )
133 return FALSE;
135 alti = expedia_zoom_to_alti ( xzoom );
136 if ( alti != -1 )
138 dest->scale = alti;
139 dest->x = (int) (((src->east_west+180) * expedia_altis_freq(alti))+0.5);
140 dest->y = (int) (((src->north_south+90) * expedia_altis_freq(alti))+0.5);
141 /* + 0.5 to round off and not floor */
143 /* just to space out tiles on the filesystem */
144 dest->z = 0;
145 return TRUE;
147 return FALSE;
150 void expedia_xy_to_latlon_middle ( gint alti, gint x, gint y, struct LatLon *ll )
152 ll->lon = (((gdouble)x) / expedia_altis_freq(alti)) - 180;
153 ll->lat = (((gdouble)y) / expedia_altis_freq(alti)) - 90;
156 static void expedia_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
158 dest->mode = VIK_COORD_LATLON;
159 dest->east_west = (((gdouble)src->x) / expedia_altis_freq(src->scale)) - 180;
160 dest->north_south = (((gdouble)src->y) / expedia_altis_freq(src->scale)) - 90;
163 static int expedia_download ( MapCoord *src, const gchar *dest_fn )
165 gint height, width;
166 struct LatLon ll;
167 gchar *uri;
168 int res = -1;
170 expedia_xy_to_latlon_middle ( src->scale, src->x, src->y, &ll );
172 height = HEIGHT_OF_LAT_DEGREE / expedia_altis_freq(src->scale) / (src->scale);
173 width = height * cos ( ll.lat * DEGREES_TO_RADS );
175 height += 2*REAL_HEIGHT_BUFFER;
176 width += 2*REAL_WIDTH_BUFFER;
178 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",
179 ll.lat, ll.lon, (ll.lon > -30) ? "EUR0809" : "USA0409", src->scale, width, height );
181 if ((res = a_http_download_get_url ( "expedia.com", uri, dest_fn, &expedia_options )) == 0) /* All OK */
182 expedia_snip ( dest_fn );
183 return(res);