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