]> Pileus Git - grits/blob - src/plugins/map.c
72fb320e0bd9ae287f59a0eee289489af82a55a2
[grits] / src / plugins / map.c
1 /*
2  * Copyright (C) 2009-2011 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 /**
19  * SECTION:map
20  * @short_description: Map plugin
21  *
22  * #GritsPluginMap provides map overlays. Much of this data is obtained from the
23  * OpenStreetMap project.
24  */
25
26 #include <time.h>
27 #include <glib/gstdio.h>
28
29 #include <grits.h>
30
31 #include "map.h"
32
33 #define MAX_RESOLUTION 1
34 #define TILE_WIDTH     256
35 #define TILE_HEIGHT    256
36 //#define MAX_RESOLUTION 100
37 //#define TILE_WIDTH     1024
38 //#define TILE_HEIGHT    512
39
40 static const guchar colormap[][2][4] = {
41         //{{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x00}}, // Oceans
42         //{{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
43         //{{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
44         //{{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
45         //{{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
46 };
47
48 struct _LoadTileData {
49         GritsPluginMap *map;
50         GritsTile      *tile;
51         guint8         *pixels;
52         gboolean        alpha;
53         gint            width;
54         gint            height;
55 };
56 static gboolean _load_tile_cb(gpointer _data)
57 {
58         struct _LoadTileData *data = _data;
59         g_debug("GritsPluginMap: _load_tile_cb start");
60
61         guint *tex = g_new0(guint, 1);
62         glGenTextures(1, tex);
63         glBindTexture(GL_TEXTURE_2D, *tex);
64
65         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
66         glPixelStorei(GL_PACK_ALIGNMENT, 1);
67         glTexImage2D(GL_TEXTURE_2D, 0, 4, data->width, data->height, 0,
68                         (data->alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, data->pixels);
69         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
70         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
71         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
72         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
73         glFlush();
74
75         data->tile->data = tex;
76         gtk_widget_queue_draw(GTK_WIDGET(data->map->viewer));
77         g_free(data->pixels);
78         g_free(data);
79         return FALSE;
80 }
81
82 static void _load_tile(GritsTile *tile, gpointer _map)
83 {
84         GritsPluginMap *map = _map;
85         g_debug("GritsPluginMap: _load_tile start %p", g_thread_self());
86         if (map->aborted) {
87                 g_debug("GritsPluginMap: _load_tile - aborted");
88                 return;
89         }
90
91         /* Download tile */
92         gchar *path = grits_tms_fetch(map->tms, tile, GRITS_ONCE, NULL, NULL);
93         //gchar *path = grits_wms_fetch(map->wms, tile, GRITS_ONCE, NULL, NULL);
94         if (!path) return; // Canceled/error
95
96         /* Load pixbuf */
97         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
98         if (!pixbuf) {
99                 g_warning("GritsPluginMap: _load_tile - Error loading pixbuf %s", path);
100                 g_remove(path);
101                 g_free(path);
102                 return;
103         }
104         g_free(path);
105
106         /* Copy pixbuf data for callback */
107         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
108         data->map    = map;
109         data->tile   = tile;
110         data->pixels = gdk_pixbuf_get_pixels(pixbuf);
111         data->alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
112         data->width  = gdk_pixbuf_get_width(pixbuf);
113         data->height = gdk_pixbuf_get_height(pixbuf);
114         data->pixels = g_memdup(data->pixels,
115                         data->width * data->height * (data->alpha ? 4 : 3));
116         g_object_unref(pixbuf);
117
118         /* Map texture colors, if needed */
119         for (int i = 0; i < data->width * data->height; i++) {
120                 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
121                         if (data->pixels[i*4+0] == colormap[j][0][0] &&
122                             data->pixels[i*4+1] == colormap[j][0][1] &&
123                             data->pixels[i*4+2] == colormap[j][0][2]) {
124                                 data->pixels[i*4+0] = colormap[j][1][0];
125                                 data->pixels[i*4+1] = colormap[j][1][1];
126                                 data->pixels[i*4+2] = colormap[j][1][2];
127                                 data->pixels[i*4+3] = colormap[j][1][3];
128                                 break;
129                         }
130                 }
131         }
132
133         /* Load the GL texture from the main thread */
134         g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
135         g_debug("GritsPluginMap: _load_tile end %p", g_thread_self());
136 }
137
138 static gboolean _free_tile_cb(gpointer data)
139 {
140         glDeleteTextures(1, data);
141         g_free(data);
142         return FALSE;
143 }
144 static void _free_tile(GritsTile *tile, gpointer _map)
145 {
146         if (tile->data) {
147                 g_debug("GritsPluginMap: _free_tile: %p", tile->data);
148                 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
149         }
150 }
151
152 static void _update_tiles(gpointer _, gpointer _map)
153 {
154         g_debug("GritsPluginMap: _update_tiles");
155         GritsPluginMap *map = _map;
156         GritsPoint eye;
157         grits_viewer_get_location(map->viewer, &eye.lat, &eye.lon, &eye.elev);
158         grits_tile_update(map->tiles, &eye,
159                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
160                         _load_tile, map);
161         grits_tile_gc(map->tiles, time(NULL)-10,
162                         _free_tile, map);
163 }
164
165 /*************
166  * Callbacks *
167  *************/
168 static void _on_location_changed(GritsViewer *viewer,
169                 gdouble lat, gdouble lon, gdouble elev, GritsPluginMap *map)
170 {
171         g_thread_pool_push(map->threads, NULL+1, NULL);
172 }
173
174 /***********
175  * Methods *
176  ***********/
177 /**
178  * grits_plugin_map_new:
179  * @viewer: the #GritsViewer to use for drawing
180  *
181  * Create a new instance of the map plugin.
182  *
183  * Returns: the new #GritsPluginMap
184  */
185 GritsPluginMap *grits_plugin_map_new(GritsViewer *viewer)
186 {
187         g_debug("GritsPluginMap: new");
188         GritsPluginMap *map = g_object_new(GRITS_TYPE_PLUGIN_MAP, NULL);
189         map->viewer = g_object_ref(viewer);
190
191         /* Load initial tiles */
192         _load_tile(map->tiles, map);
193         _update_tiles(NULL, map);
194
195         /* Connect signals */
196         map->sigid = g_signal_connect(map->viewer, "location-changed",
197                         G_CALLBACK(_on_location_changed), map);
198
199         /* Add renderers */
200         grits_viewer_add(viewer, GRITS_OBJECT(map->tiles), GRITS_LEVEL_WORLD, FALSE);
201
202         return map;
203 }
204
205
206 /****************
207  * GObject code *
208  ****************/
209 /* Plugin init */
210 static void grits_plugin_map_plugin_init(GritsPluginInterface *iface);
211 G_DEFINE_TYPE_WITH_CODE(GritsPluginMap, grits_plugin_map, G_TYPE_OBJECT,
212                 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
213                         grits_plugin_map_plugin_init));
214 static void grits_plugin_map_plugin_init(GritsPluginInterface *iface)
215 {
216         g_debug("GritsPluginMap: plugin_init");
217         /* Add methods to the interface */
218 }
219 /* Class/Object init */
220 static void grits_plugin_map_init(GritsPluginMap *map)
221 {
222         g_debug("GritsPluginMap: init");
223         /* Set defaults */
224         map->threads = g_thread_pool_new(_update_tiles, map, 1, FALSE, NULL);
225         map->tiles = grits_tile_new(NULL, 85.0511, -85.0511, EAST, WEST);
226         map->tms   = grits_tms_new("http://tile.openstreetmap.org",
227                 "osmtile/", "png");
228         map->tiles->proj = GRITS_PROJ_MERCATOR;
229         //map->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
230         //map->wms   = grits_wms_new(
231         //      "http://vmap0.tiles.osgeo.org/wms/vmap0",
232         //      "basic,priroad,secroad,depthcontour,clabel,statelabel",
233         //       "image/png", "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
234         g_object_ref(map->tiles);
235 }
236 static void grits_plugin_map_dispose(GObject *gobject)
237 {
238         g_debug("GritsPluginMap: dispose");
239         GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
240         map->aborted = TRUE;
241         /* Drop references */
242         if (map->viewer) {
243                 GritsViewer *viewer = map->viewer;
244                 map->viewer = NULL;
245                 g_signal_handler_disconnect(viewer, map->sigid);
246                 grits_viewer_remove(viewer, GRITS_OBJECT(map->tiles));
247                 soup_session_abort(map->tms->http->soup);
248                 //soup_session_abort(map->wms->http->soup);
249                 g_thread_pool_free(map->threads, TRUE, TRUE);
250                 while (gtk_events_pending())
251                         gtk_main_iteration();
252                 g_object_unref(viewer);
253         }
254         G_OBJECT_CLASS(grits_plugin_map_parent_class)->dispose(gobject);
255 }
256 static void grits_plugin_map_finalize(GObject *gobject)
257 {
258         g_debug("GritsPluginMap: finalize");
259         GritsPluginMap *map = GRITS_PLUGIN_MAP(gobject);
260         /* Free data */
261         grits_tms_free(map->tms);
262         //grits_wms_free(map->wms);
263         grits_tile_free(map->tiles, _free_tile, map);
264         G_OBJECT_CLASS(grits_plugin_map_parent_class)->finalize(gobject);
265
266 }
267 static void grits_plugin_map_class_init(GritsPluginMapClass *klass)
268 {
269         g_debug("GritsPluginMap: class_init");
270         GObjectClass *gobject_class = (GObjectClass*)klass;
271         gobject_class->dispose  = grits_plugin_map_dispose;
272         gobject_class->finalize = grits_plugin_map_finalize;
273 }