2 * Copyright (C) 2009-2011 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: Satellite plugin
22 * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23 * provided by NASA's Blue Marble Next Generation.
28 #include <glib/gstdio.h>
34 #define MAX_RESOLUTION 500
35 #define TILE_WIDTH 1024
36 #define TILE_HEIGHT 512
38 static void _load_tile(GritsTile *tile, gpointer _sat)
40 GritsPluginSat *sat = _sat;
41 g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
43 g_debug("GritsPluginSat: _load_tile - aborted");
48 gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
49 if (!path) return; // Canceled/error
52 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
54 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
62 #ifdef DRAW_TILE_BORDER
64 gint width = gdk_pixbuf_get_width(pixbuf);
65 gint height = gdk_pixbuf_get_height(pixbuf);
66 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
67 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
68 for (int i = 0; i < border; i++) {
69 memset(&pixels[( i)*stride], 0xff, stride);
70 memset(&pixels[(height-1-i)*stride], 0xff, stride);
72 for (int i = 0; i < height; i++) {
73 memset(&pixels[(i*stride) ], 0xff, border*4);
74 memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
78 /* Load the GL texture from the main thread */
79 grits_tile_load_pixbuf(tile, pixbuf);
80 g_debug("GritsPluginSat: _load_tile end %p", g_thread_self());
83 static void _update_tiles(gpointer _, gpointer _sat)
85 g_debug("GritsPluginSat: _update_tiles");
86 GritsPluginSat *sat = _sat;
88 grits_viewer_get_location(sat->viewer, &eye.lat, &eye.lon, &eye.elev);
89 grits_tile_update(sat->tiles, &eye,
90 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
92 grits_tile_gc(sat->tiles, time(NULL)-10, NULL, sat);
98 static void _on_location_changed(GritsViewer *viewer,
99 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
101 g_thread_pool_push(sat->threads, NULL+1, NULL);
108 * grits_plugin_sat_new:
109 * @viewer: the #GritsViewer to use for drawing
111 * Create a new instance of the satellite plugin.
113 * Returns: the new #GritsPluginSat
115 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
117 g_debug("GritsPluginSat: new");
118 GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
119 sat->viewer = g_object_ref(viewer);
121 /* Load initial tiles */
122 _update_tiles(NULL, sat);
124 /* Connect signals */
125 sat->sigid = g_signal_connect(sat->viewer, "location-changed",
126 G_CALLBACK(_on_location_changed), sat);
129 grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
139 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
140 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
141 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
142 grits_plugin_sat_plugin_init));
143 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
145 g_debug("GritsPluginSat: plugin_init");
146 /* Add methods to the interface */
148 /* Class/Object init */
149 static void grits_plugin_sat_init(GritsPluginSat *sat)
151 g_debug("GritsPluginSat: init");
153 sat->threads = g_thread_pool_new(_update_tiles, sat, 1, FALSE, NULL);
154 sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
155 sat->wms = grits_wms_new(
156 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
157 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
158 g_object_ref(sat->tiles);
160 static void grits_plugin_sat_dispose(GObject *gobject)
162 g_debug("GritsPluginSat: dispose");
163 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
165 /* Drop references */
167 GritsViewer *viewer = sat->viewer;
169 g_signal_handler_disconnect(viewer, sat->sigid);
170 grits_viewer_remove(viewer, GRITS_OBJECT(sat->tiles));
171 g_object_unref(sat->tiles);
172 soup_session_abort(sat->wms->http->soup);
173 g_thread_pool_free(sat->threads, TRUE, TRUE);
174 g_object_unref(viewer);
176 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
178 static void grits_plugin_sat_finalize(GObject *gobject)
180 g_debug("GritsPluginSat: finalize");
181 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
183 grits_wms_free(sat->wms);
184 grits_tile_free(sat->tiles, NULL, sat);
185 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
188 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
190 g_debug("GritsPluginSat: class_init");
191 GObjectClass *gobject_class = (GObjectClass*)klass;
192 gobject_class->dispose = grits_plugin_sat_dispose;
193 gobject_class->finalize = grits_plugin_sat_finalize;