0951541116e89b05627fe78742f2713423050041
[grits] / src / tile-test.c
1 /*
2  * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <gtk/gtk.h>
19 #include <gdk/gdkkeysyms.h>
20
21 #include "grits-util.h"
22 #include "data/grits-wms.h"
23 #include "data/grits-tms.h"
24 #include "objects/grits-tile.h"
25
26 struct CacheState {
27         GtkWidget *image;
28         GtkWidget *status;
29         GtkWidget *progress;
30 };
31
32 struct LoadData {
33         GtkImage  *image;
34         GdkPixbuf *pixbuf;
35 };
36
37 void chunk_callback(gsize cur, gsize total, gpointer _state)
38 {
39         struct CacheState *state = _state;
40         g_message("chunk_callback: %ld/%ld", (glong)cur, (glong)total);
41
42         if (state->progress == NULL) {
43                 state->progress = gtk_progress_bar_new();
44                 gtk_box_pack_end(GTK_BOX(state->status), state->progress, FALSE, FALSE, 0);
45                 gtk_widget_show(state->progress);
46         }
47
48         if (cur == total)
49                 gtk_widget_destroy(state->progress);
50         else
51                 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(state->progress), (gdouble)cur/total);
52 }
53
54 gboolean load_callback(gpointer _data)
55 {
56         struct LoadData *data = _data;
57         gtk_image_set_from_pixbuf(GTK_IMAGE(data->image), data->pixbuf);
58         g_free(data);
59         return FALSE;
60 }
61
62 void load_image(GtkImage *image, GdkPixbuf *pixbuf)
63 {
64         struct LoadData *data = g_new0(struct LoadData, 1);
65         data->image  = image;
66         data->pixbuf = pixbuf;
67         g_idle_add(load_callback, data);
68 }
69
70 gpointer do_bmng_cache(gpointer _image)
71 {
72         GtkImage *image = _image;
73         g_message("Creating bmng tile");
74         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
75         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
76         tile = tile->children[0][1];
77
78         g_message("Fetching bmng image");
79         GritsWms *bmng_wms = grits_wms_new(
80                 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
81                 "bmng_test/", "jpg", 512, 256);
82         const char *path = grits_wms_fetch(bmng_wms, tile, GRITS_ONCE, NULL, NULL);
83
84         g_message("Loading bmng image: [%s]", path);
85         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
86         load_image(GTK_IMAGE(image), pixbuf);
87
88         g_message("Cleaning bmng up");
89         grits_wms_free(bmng_wms);
90         grits_tile_free(tile, NULL, NULL);
91         return NULL;
92 }
93
94 gpointer do_osm_cache(gpointer _image)
95 {
96         GtkImage *image = _image;
97         g_message("Creating osm tile");
98         GritsTile *tile = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
99         tile->children[0][1] = grits_tile_new(tile, NORTH, 0, 0, WEST);
100         tile = tile->children[0][1];
101
102         g_message("Fetching osm image");
103         GritsWms *osm_wms = grits_wms_new(
104                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
105                 "osm_test/", "png", 512, 256);
106         const char *path = grits_wms_fetch(osm_wms, tile, GRITS_ONCE, NULL, NULL);
107
108         g_message("Loading osm image: [%s]", path);
109         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
110         load_image(GTK_IMAGE(image), pixbuf);
111
112         g_message("Cleaning osm up");
113         grits_wms_free(osm_wms);
114         grits_tile_free(tile, NULL, NULL);
115         return NULL;
116 }
117
118 gpointer do_osm2_cache(gpointer _image)
119 {
120         GtkImage *image = _image;
121         g_message("Creating osm2 tile");
122         GritsTile *tile = grits_tile_new(NULL, 85.0511, -85.0511, EAST, WEST);
123         tile->children[0][1] = grits_tile_new(tile, 85.0511, 0, 0, WEST);
124         tile = tile->children[0][1];
125
126         g_message("Fetching osm2 image");
127         GritsTms *osm2_tms = grits_tms_new("http://tile.openstreetmap.org",
128                         "tms_test/", "png");
129         const char *path = grits_tms_fetch(osm2_tms, tile, GRITS_ONCE, NULL, NULL);
130
131         g_message("Loading osm2 image: [%s]", path);
132         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
133         load_image(GTK_IMAGE(image), pixbuf);
134
135         g_message("Cleaning osm2 up");
136         grits_tms_free(osm2_tms);
137         grits_tile_free(tile, NULL, NULL);
138         return NULL;
139 }
140
141
142 gboolean key_press_cb(GtkWidget *widget, GdkEventKey *event, gpointer user_data)
143 {
144         if (event->keyval == GDK_KEY_q)
145                 gtk_main_quit();
146         return TRUE;
147 }
148
149 int main(int argc, char **argv)
150 {
151         gtk_init(&argc, &argv);
152
153         GtkWidget *win        = gtk_window_new(GTK_WINDOW_TOPLEVEL);
154         GtkWidget *vbox1      = gtk_vbox_new(FALSE, 0);
155         GtkWidget *vbox2      = gtk_vbox_new(FALSE, 0);
156         GtkWidget *status     = gtk_statusbar_new();
157         GtkWidget *scroll     = gtk_scrolled_window_new(NULL, NULL);
158         GtkWidget *bmng_image = gtk_image_new();
159         GtkWidget *srtm_image = gtk_image_new();
160         GtkWidget *osm_image  = gtk_image_new();
161         GtkWidget *osm2_image = gtk_image_new();
162         gtk_container_add(GTK_CONTAINER(win), vbox1);
163         gtk_box_pack_start(GTK_BOX(vbox1), scroll, TRUE, TRUE, 0);
164         gtk_scrolled_window_add_with_viewport(GTK_SCROLLED_WINDOW(scroll), vbox2);
165         gtk_box_pack_start(GTK_BOX(vbox2), bmng_image, TRUE, TRUE, 0);
166         gtk_box_pack_start(GTK_BOX(vbox2), srtm_image, TRUE, TRUE, 0);
167         gtk_box_pack_start(GTK_BOX(vbox2), osm_image,  TRUE, TRUE, 0);
168         gtk_box_pack_start(GTK_BOX(vbox2), osm2_image, TRUE, TRUE, 0);
169         gtk_box_pack_start(GTK_BOX(vbox1), status, FALSE, FALSE, 0);
170         g_signal_connect(win, "key-press-event", G_CALLBACK(key_press_cb), NULL);
171         g_signal_connect(win, "destroy", gtk_main_quit, NULL);
172         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scroll),
173                         GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
174
175         g_thread_new("bmng-thread", do_bmng_cache, bmng_image);
176         g_thread_new("osm-thread",  do_osm_cache,  osm_image);
177         g_thread_new("osm2-thread", do_osm2_cache, osm2_image);
178
179         gtk_widget_show_all(win);
180         gtk_main();
181
182         return 0;
183 }