]> Pileus Git - grits/blob - src/plugins/map.c
7a2d66d373f91d7d6d6daeb0a6e6ef101767bef7
[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  * #GisPluginMap provides map overlays. Much of this data is obtained from the
23  * OpenStreetMap project.
24  */
25
26 #include <time.h>
27 #include <stdlib.h>
28 #include <glib/gstdio.h>
29 #include <GL/gl.h>
30
31 #include <gis.h>
32
33 #include "map.h"
34
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH     1024
37 #define TILE_HEIGHT    512
38
39 static const guchar colormap[][2][4] = {
40         {{0x73, 0x91, 0xad}, {0x73, 0x91, 0xad, 0x20}}, // Oceans
41         {{0xf6, 0xee, 0xee}, {0xf6, 0xee, 0xee, 0x00}}, // Ground
42         {{0xff, 0xff, 0xff}, {0xff, 0xff, 0xff, 0xff}}, // Borders
43         {{0x73, 0x93, 0xad}, {0x73, 0x93, 0xad, 0x40}}, // Lakes
44         {{0xff, 0xe1, 0x80}, {0xff, 0xe1, 0x80, 0x60}}, // Cities
45 };
46
47 struct _LoadTileData {
48         GisPluginMap *map;
49         GisTile      *tile;
50         GdkPixbuf    *pixbuf;
51 };
52 static gboolean _load_tile_cb(gpointer _data)
53 {
54         struct _LoadTileData *data = _data;
55         GisPluginMap *map    = data->map;
56         GisTile      *tile   = data->tile;
57         GdkPixbuf    *pixbuf = data->pixbuf;
58         g_free(data);
59
60         /* Create Texture */
61         g_debug("GisPluginMap: _load_tile_cb start");
62         guchar   *pixels = gdk_pixbuf_get_pixels(pixbuf);
63         gboolean  alpha  = gdk_pixbuf_get_has_alpha(pixbuf);
64         gint      width  = gdk_pixbuf_get_width(pixbuf);
65         gint      height = gdk_pixbuf_get_height(pixbuf);
66
67         for (int i = 0; i < width*height; i++) {
68                 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
69                         if (pixels[i*4+0] == colormap[j][0][0] &&
70                             pixels[i*4+1] == colormap[j][0][1] &&
71                             pixels[i*4+2] == colormap[j][0][2]) {
72                                 pixels[i*4+0] = colormap[j][1][0];
73                                 pixels[i*4+1] = colormap[j][1][1];
74                                 pixels[i*4+2] = colormap[j][1][2];
75                                 pixels[i*4+3] = colormap[j][1][3];
76                                 break;
77                         }
78                 }
79         }
80
81         guint *tex = g_new0(guint, 1);
82         glGenTextures(1, tex);
83         glBindTexture(GL_TEXTURE_2D, *tex);
84
85         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
86         glPixelStorei(GL_PACK_ALIGNMENT, 1);
87         glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
88                         (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
89         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
90         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
92         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
93         glFlush();
94
95         tile->data = tex;
96         gtk_widget_queue_draw(GTK_WIDGET(map->viewer));
97         g_object_unref(pixbuf);
98         return FALSE;
99 }
100
101 static void _load_tile(GisTile *tile, gpointer _map)
102 {
103         GisPluginMap *map = _map;
104         g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
105         char *path = gis_wms_fetch(map->wms, tile, GIS_ONCE, NULL, NULL);
106         struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
107         data->map    = map;
108         data->tile   = tile;
109         data->pixbuf = gdk_pixbuf_new_from_file(path, NULL);
110         if (data->pixbuf) {
111                 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
112         } else {
113                 g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
114                 g_remove(path);
115         }
116         g_free(path);
117         g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
118 }
119
120 static gboolean _free_tile_cb(gpointer data)
121 {
122         glDeleteTextures(1, data);
123         g_free(data);
124         return FALSE;
125 }
126 static void _free_tile(GisTile *tile, gpointer _map)
127 {
128         GisPluginMap *map = _map;
129         g_debug("GisPluginMap: _free_tile: %p", tile->data);
130         g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
131 }
132
133 static gpointer _update_tiles(gpointer _map)
134 {
135         g_debug("GisPluginMap: _update_tiles");
136         GisPluginMap *map = _map;
137         if (!g_mutex_trylock(map->mutex))
138                 return NULL;
139         gdouble lat, lon, elev;
140         gis_viewer_get_location(map->viewer, &lat, &lon, &elev);
141         gis_tile_update(map->tiles,
142                         MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
143                         lat, lon, elev,
144                         _load_tile, map);
145         gis_tile_gc(map->tiles, time(NULL)-10,
146                         _free_tile, map);
147         g_mutex_unlock(map->mutex);
148         return NULL;
149 }
150
151 /*************
152  * Callbacks *
153  *************/
154 static void _on_location_changed(GisViewer *viewer,
155                 gdouble lat, gdouble lon, gdouble elev, GisPluginMap *map)
156 {
157         g_thread_create(_update_tiles, map, FALSE, NULL);
158 }
159
160 static gpointer _threaded_init(GisPluginMap *map)
161 {
162         _load_tile(map->tiles, map);
163         _update_tiles(map);
164         return NULL;
165 }
166
167 /***********
168  * Methods *
169  ***********/
170 /**
171  * gis_plugin_map_new:
172  * @viewer: the #GisViewer to use for drawing
173  *
174  * Create a new instance of the map plugin.
175  *
176  * Returns: the new #GisPluginMap
177  */
178 GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
179 {
180         g_debug("GisPluginMap: new");
181         GisPluginMap *map = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
182         map->viewer = g_object_ref(viewer);
183
184         /* Load initial tiles */
185         g_thread_create((GThreadFunc)_threaded_init, map, FALSE, NULL);
186
187         /* Connect signals */
188         map->sigid = g_signal_connect(map->viewer, "location-changed",
189                         G_CALLBACK(_on_location_changed), map);
190
191         /* Add renderers */
192         gis_viewer_add(viewer, GIS_OBJECT(map->tiles), GIS_LEVEL_OVERLAY, 0);
193
194         return map;
195 }
196
197
198 /****************
199  * GObject code *
200  ****************/
201 /* Plugin init */
202 static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
203 G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
204                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
205                         gis_plugin_map_plugin_init));
206 static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
207 {
208         g_debug("GisPluginMap: plugin_init");
209         /* Add methods to the interface */
210 }
211 /* Class/Object init */
212 static void gis_plugin_map_init(GisPluginMap *map)
213 {
214         g_debug("GisPluginMap: init");
215         /* Set defaults */
216         map->mutex  = g_mutex_new();
217         map->tiles  = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
218         map->wms    = gis_wms_new(
219                 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
220                 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
221 }
222 static void gis_plugin_map_dispose(GObject *gobject)
223 {
224         g_debug("GisPluginMap: dispose");
225         GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
226         /* Drop references */
227         if (map->viewer) {
228                 g_signal_handler_disconnect(map->viewer, map->sigid);
229                 g_object_unref(map->viewer);
230                 map->viewer = NULL;
231         }
232         G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
233 }
234 static void gis_plugin_map_finalize(GObject *gobject)
235 {
236         g_debug("GisPluginMap: finalize");
237         GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
238         /* Free data */
239         gis_tile_free(map->tiles, _free_tile, map);
240         gis_wms_free(map->wms);
241         g_mutex_free(map->mutex);
242         G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
243
244 }
245 static void gis_plugin_map_class_init(GisPluginMapClass *klass)
246 {
247         g_debug("GisPluginMap: class_init");
248         GObjectClass *gobject_class = (GObjectClass*)klass;
249         gobject_class->dispose  = gis_plugin_map_dispose;
250         gobject_class->finalize = gis_plugin_map_finalize;
251 }