]> git.street.me.uk Git - andy/viking.git/blame - src/khmaps.c
ChangeLog entry
[andy/viking.git] / src / khmaps.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 <string.h>
24#include <math.h>
79845167 25#include "viking.h"
50a14534
EB
26#include "coords.h"
27#include "vikcoord.h"
28#include "mapcoord.h"
85611cd9 29#include "download.h"
cdcaf41c 30#include "vikmapslayer.h"
50a14534
EB
31
32#include "khmaps.h"
33
a3188993 34static DownloadOptions khmaps_options = { 1, NULL };
80214df2 35
cdcaf41c
QT
36void khmaps_init () {
37 VikMapsLayer_MapType map_type = { 8, 256, 256, VIK_VIEWPORT_DRAWMODE_KH, khmaps_coord_to_mapcoord, khmaps_mapcoord_to_center_coord, khmaps_download };
38
39 maps_layer_register_type("Old KH Satellite Images", 8, &map_type);
40}
41
50a14534
EB
42/* 1 << (x-1) is like a 2**(x-1) */
43#define KH(x) ((1<<(x-1)))
44
45static const gdouble scale_mpps[] = { KH(1), KH(2), KH(3), KH(4), KH(5), KH(6), KH(7), KH(8), KH(9),
46 KH(10), KH(11), KH(12), KH(13), KH(14), KH(15) };
47
48static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0])) - 1;
49
50#define ERROR_MARGIN 0.01
51guint8 khmaps_zoom ( gdouble mpp ) {
52 gint i;
53 for ( i = 0; i <= num_scales; i++ ) {
54 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN )
55 return i;
56 }
57 return 255;
58}
59
60gboolean khmaps_coord_to_mapcoord ( const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
61{
62 g_assert ( src->mode == VIK_COORD_LATLON );
63
64 if ( xzoom != yzoom )
65 return FALSE;
66
67 dest->scale = khmaps_zoom ( xzoom );
68 if ( dest->scale == 255 )
69 return FALSE;
70
71
72 dest->x = (gint) ((1<<(16 - dest->scale)) + (src->east_west / 180 * (1<<(16-dest->scale))));
73 dest->y = (gint) ((1<<(16 - dest->scale)) - (src->north_south / 180 * (1<<(16-dest->scale))));
74 dest->z = 0;
75 return TRUE;
76}
77
78void khmaps_mapcoord_to_center_coord ( MapCoord *src, VikCoord *dest )
79{
80 dest->mode = VIK_COORD_LATLON;
81 dest->east_west = (src->x+0.5 - (1<<(16 - src->scale))) * 180 / (1<<(16-src->scale));
82 dest->north_south = -((src->y+0.5 - (1<<(16 - src->scale))) * 180 / (1<<(16-src->scale)));
83}
84
85static char *kh_encode(guint32 x, guint32 y, guint8 scale)
86{
87 gchar *buf = g_malloc ( (20-scale)*sizeof(gchar) );
88 guint32 ya = 1 << (17 - scale);
89 gint8 i, j;
90
91 if (y < 0 || (ya-1 < y)) {
92 strcpy(buf,"tqq"); /* BAD */
93 return buf;
94 }
95 if (x < 0 || ya-1 < x) {
96 x %= ya;
97 if (x < 0)
98 x += ya;
99 }
100
101 buf[0] = 't';
102 for (j = 1, i = 16; i >= scale; i--, j++) {
103 ya /= 2;
104 if (y < ya) {
105 if (x < ya)
106 buf[j]='q';
107 else {
108 buf[j]='r';
109 x -= ya;
110 }
111 } else {
112 if (x < ya) {
113 buf[j] = 't';
114 y -= ya;
115 } else {
116 buf[j] = 's';
117 x -= ya;
118 y -= ya;
119 }
120 }
121 }
122 buf[j] = '\0';
123 return buf;
124}
125
126void khmaps_download ( MapCoord *src, const gchar *dest_fn )
127{
128 gchar *tmp = kh_encode(src->x, src->y, src->scale);
129 gchar *uri = g_strdup_printf ( "/kh?v=2&t=%s", tmp );
130 g_print("%d %d %d = %s\n", src->x, src->y, src->scale, uri);
131 g_free ( tmp );
80214df2 132 a_http_download_get_url ( "kh.google.com", uri, dest_fn, &khmaps_options );
50a14534
EB
133 g_free ( uri );
134}
135
136/* Popularity has its disadvantages ... */