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_thread(gpointer _tile, gpointer _sat)
40 GritsTile *tile = _tile;
41 GritsPluginSat *sat = _sat;
43 g_debug("GritsPluginSat: _load_tile_thread start %p - tile=%p",
44 g_thread_self(), tile);
46 g_debug("GritsPluginSat: _load_tile_thread - aborted");
51 gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
52 if (!path) return; // Canceled/error
55 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
57 g_warning("GritsPluginSat: _load_tile_thread - Error loading pixbuf %s", path);
65 #ifdef DRAW_TILE_BORDER
67 gint width = gdk_pixbuf_get_width(pixbuf);
68 gint height = gdk_pixbuf_get_height(pixbuf);
69 gint stride = gdk_pixbuf_get_rowstride(pixbuf);
70 guchar *pixels = gdk_pixbuf_get_pixels(pixbuf);
71 for (int i = 0; i < border; i++) {
72 memset(&pixels[( i)*stride], 0xff, stride);
73 memset(&pixels[(height-1-i)*stride], 0xff, stride);
75 for (int i = 0; i < height; i++) {
76 memset(&pixels[(i*stride) ], 0xff, border*4);
77 memset(&pixels[(i*stride)+((width-border)*4)], 0xff, border*4);
81 /* Load the GL texture from the main thread */
82 grits_tile_load_pixbuf(tile, pixbuf);
83 g_debug("GritsPluginSat: _load_tile_thread end %p", g_thread_self());
86 static void _load_tile_func(GritsTile *tile, gpointer _sat)
88 g_debug("GritsPluginSat: __load_tile_func - tile=%p", tile);
89 GritsPluginSat *sat = _sat;
90 g_thread_pool_push(sat->threads, tile, NULL);
96 static void _on_location_changed(GritsViewer *viewer,
97 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
99 GritsPoint eye = {lat, lon, elev};
100 grits_tile_update(sat->tiles, &eye,
101 MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
102 _load_tile_func, sat);
103 grits_tile_gc(sat->tiles, time(NULL)-10, NULL, sat);
110 * grits_plugin_sat_new:
111 * @viewer: the #GritsViewer to use for drawing
113 * Create a new instance of the satellite plugin.
115 * Returns: the new #GritsPluginSat
117 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
119 g_debug("GritsPluginSat: new");
120 GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
121 sat->viewer = g_object_ref(viewer);
123 /* Load initial tiles */
124 gdouble lat, lon, elev;
125 grits_viewer_get_location(viewer, &lat, &lon, &elev);
126 _on_location_changed(viewer, lat, lon, elev, sat);
128 /* Connect signals */
129 sat->sigid = g_signal_connect(sat->viewer, "location-changed",
130 G_CALLBACK(_on_location_changed), sat);
133 grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
143 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface);
144 G_DEFINE_TYPE_WITH_CODE(GritsPluginSat, grits_plugin_sat, G_TYPE_OBJECT,
145 G_IMPLEMENT_INTERFACE(GRITS_TYPE_PLUGIN,
146 grits_plugin_sat_plugin_init));
147 static void grits_plugin_sat_plugin_init(GritsPluginInterface *iface)
149 g_debug("GritsPluginSat: plugin_init");
150 /* Add methods to the interface */
152 /* Class/Object init */
153 static void grits_plugin_sat_init(GritsPluginSat *sat)
155 g_debug("GritsPluginSat: init");
157 sat->threads = g_thread_pool_new(_load_tile_thread, sat, 1, FALSE, NULL);
158 sat->tiles = grits_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
159 sat->wms = grits_wms_new(
160 "http://www.nasa.network.com/wms", "bmng200406", "image/jpeg",
161 "bmng/", "jpg", TILE_WIDTH, TILE_HEIGHT);
162 g_object_ref(sat->tiles);
164 static void grits_plugin_sat_dispose(GObject *gobject)
166 g_debug("GritsPluginSat: dispose");
167 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
169 /* Drop references */
171 GritsViewer *viewer = sat->viewer;
172 g_signal_handler_disconnect(viewer, sat->sigid);
173 grits_http_abort(sat->wms->http);
174 g_thread_pool_free(sat->threads, TRUE, TRUE);
176 grits_object_destroy_pointer(&sat->tiles);
177 g_object_unref(viewer);
179 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
181 static void grits_plugin_sat_finalize(GObject *gobject)
183 g_debug("GritsPluginSat: finalize");
184 GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
186 grits_wms_free(sat->wms);
187 grits_tile_free(sat->tiles, NULL, sat);
188 G_OBJECT_CLASS(grits_plugin_sat_parent_class)->finalize(gobject);
191 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
193 g_debug("GritsPluginSat: class_init");
194 GObjectClass *gobject_class = (GObjectClass*)klass;
195 gobject_class->dispose = grits_plugin_sat_dispose;
196 gobject_class->finalize = grits_plugin_sat_finalize;