2 * Copyright (C) 2009-2010 Andy Spencer <andy753421@gmail.com>
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.
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.
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/>.
20 * @short_description: Map plugin
22 * #GisPluginMap provides map overlays. Much of this data is obtained from the
23 * OpenStreetMap project.
28 #include <glib/gstdio.h>
35 #define MAX_RESOLUTION 500
36 #define TILE_WIDTH 1024
37 #define TILE_HEIGHT 512
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
47 struct _LoadTileData {
52 static gboolean _load_tile_cb(gpointer _data)
54 struct _LoadTileData *data = _data;
55 GisPluginMap *map = data->map;
56 GisTile *tile = data->tile;
57 gchar *path = data->path;
61 g_debug("GisPluginMap: _load_tile_cb start");
62 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
64 g_warning("GisPluginMap: _load_tile - Error loading pixbuf %s", path);
72 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
73 gboolean alpha = gdk_pixbuf_get_has_alpha(pixbuf);
74 gint width = gdk_pixbuf_get_width(pixbuf);
75 gint height = gdk_pixbuf_get_height(pixbuf);
77 for (int i = 0; i < width*height; i++) {
78 for (int j = 0; j < G_N_ELEMENTS(colormap); j++) {
79 if (pixels[i*4+0] == colormap[j][0][0] &&
80 pixels[i*4+1] == colormap[j][0][1] &&
81 pixels[i*4+2] == colormap[j][0][2]) {
82 pixels[i*4+0] = colormap[j][1][0];
83 pixels[i*4+1] = colormap[j][1][1];
84 pixels[i*4+2] = colormap[j][1][2];
85 pixels[i*4+3] = colormap[j][1][3];
91 guint *tex = g_new0(guint, 1);
92 glGenTextures(1, tex);
93 glBindTexture(GL_TEXTURE_2D, *tex);
95 glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
96 glPixelStorei(GL_PACK_ALIGNMENT, 1);
97 glTexImage2D(GL_TEXTURE_2D, 0, 4, width, height, 0,
98 (alpha ? GL_RGBA : GL_RGB), GL_UNSIGNED_BYTE, pixels);
99 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
100 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
101 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
102 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
106 gtk_widget_queue_draw(GTK_WIDGET(map->viewer));
107 g_object_unref(pixbuf);
111 static void _load_tile(GisTile *tile, gpointer _map)
113 GisPluginMap *map = _map;
114 g_debug("GisPluginMap: _load_tile start %p", g_thread_self());
115 struct _LoadTileData *data = g_new0(struct _LoadTileData, 1);
118 data->path = gis_wms_fetch(map->wms, tile, GIS_ONCE, NULL, NULL);
119 g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, data, NULL);
120 g_debug("GisPluginMap: _load_tile end %p", g_thread_self());
123 static gboolean _free_tile_cb(gpointer data)
125 glDeleteTextures(1, data);
129 static void _free_tile(GisTile *tile, gpointer _map)
131 GisPluginMap *map = _map;
132 g_debug("GisPluginMap: _free_tile: %p", tile->data);
134 g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
137 static gpointer _update_tiles(gpointer _map)
139 g_debug("GisPluginMap: _update_tiles");
140 GisPluginMap *map = _map;
141 if (!g_mutex_trylock(map->mutex))
144 gis_viewer_get_location(map->viewer, &eye.lat, &eye.lon, &eye.elev);
145 gis_tile_update(map->tiles, &eye,
146 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
148 gis_tile_gc(map->tiles, time(NULL)-10,
150 g_mutex_unlock(map->mutex);
157 static void _on_location_changed(GisViewer *viewer,
158 gdouble lat, gdouble lon, gdouble elev, GisPluginMap *map)
160 g_thread_create(_update_tiles, map, FALSE, NULL);
163 static gpointer _threaded_init(GisPluginMap *map)
165 _load_tile(map->tiles, map);
174 * gis_plugin_map_new:
175 * @viewer: the #GisViewer to use for drawing
177 * Create a new instance of the map plugin.
179 * Returns: the new #GisPluginMap
181 GisPluginMap *gis_plugin_map_new(GisViewer *viewer)
183 g_debug("GisPluginMap: new");
184 GisPluginMap *map = g_object_new(GIS_TYPE_PLUGIN_MAP, NULL);
185 map->viewer = g_object_ref(viewer);
187 /* Load initial tiles */
188 g_thread_create((GThreadFunc)_threaded_init, map, FALSE, NULL);
190 /* Connect signals */
191 map->sigid = g_signal_connect(map->viewer, "location-changed",
192 G_CALLBACK(_on_location_changed), map);
195 gis_viewer_add(viewer, GIS_OBJECT(map->tiles), GIS_LEVEL_OVERLAY-1, 0);
205 static void gis_plugin_map_plugin_init(GisPluginInterface *iface);
206 G_DEFINE_TYPE_WITH_CODE(GisPluginMap, gis_plugin_map, G_TYPE_OBJECT,
207 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
208 gis_plugin_map_plugin_init));
209 static void gis_plugin_map_plugin_init(GisPluginInterface *iface)
211 g_debug("GisPluginMap: plugin_init");
212 /* Add methods to the interface */
214 /* Class/Object init */
215 static void gis_plugin_map_init(GisPluginMap *map)
217 g_debug("GisPluginMap: init");
219 map->mutex = g_mutex_new();
220 map->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
221 map->wms = gis_wms_new(
222 "http://labs.metacarta.com/wms/vmap0", "basic", "image/png",
223 "osm/", "png", TILE_WIDTH, TILE_HEIGHT);
225 static void gis_plugin_map_dispose(GObject *gobject)
227 g_debug("GisPluginMap: dispose");
228 GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
229 /* Drop references */
231 g_signal_handler_disconnect(map->viewer, map->sigid);
232 g_object_unref(map->viewer);
235 G_OBJECT_CLASS(gis_plugin_map_parent_class)->dispose(gobject);
237 static void gis_plugin_map_finalize(GObject *gobject)
239 g_debug("GisPluginMap: finalize");
240 GisPluginMap *map = GIS_PLUGIN_MAP(gobject);
242 gis_tile_free(map->tiles, _free_tile, map);
243 gis_wms_free(map->wms);
244 g_mutex_free(map->mutex);
245 G_OBJECT_CLASS(gis_plugin_map_parent_class)->finalize(gobject);
248 static void gis_plugin_map_class_init(GisPluginMapClass *klass)
250 g_debug("GisPluginMap: class_init");
251 GObjectClass *gobject_class = (GObjectClass*)klass;
252 gobject_class->dispose = gis_plugin_map_dispose;
253 gobject_class->finalize = gis_plugin_map_finalize;