]> Pileus Git - grits/blob - src/plugins/sat.c
Add cube GtkGL example
[grits] / src / plugins / sat.c
1 /*
2  * Copyright (C) 2009-2011 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:sat
20  * @short_description: Satellite plugin
21  *
22  * #GritsPluginSat provides overlays using satellite imagery. This is mostly
23  * provided by NASA's Blue Marble Next Generation.
24  */
25
26 #include <time.h>
27 #include <string.h>
28 #include <glib/gstdio.h>
29
30 #include <grits.h>
31
32 #include "sat.h"
33
34 #define MAX_RESOLUTION 500
35 #define TILE_WIDTH     1024
36 #define TILE_HEIGHT    512
37
38 static void _load_tile_thread(gpointer _tile, gpointer _sat)
39 {
40         GritsTile      *tile = _tile;
41         GritsPluginSat *sat  = _sat;
42
43         g_debug("GritsPluginSat: _load_tile_thread start %p - tile=%p",
44                         g_thread_self(), tile);
45         if (sat->aborted) {
46                 g_debug("GritsPluginSat: _load_tile_thread - aborted");
47                 return;
48         }
49
50         /* Download tile */
51         gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
52         if (!path) return; // Canceled/error
53
54         /* Load pixbuf */
55         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
56         if (!pixbuf) {
57                 g_warning("GritsPluginSat: _load_tile_thread - Error loading pixbuf %s", path);
58                 g_remove(path);
59                 g_free(path);
60                 return;
61         }
62         g_free(path);
63
64         /* Draw a border */
65 #ifdef DRAW_TILE_BORDER
66         gint    border = 10;
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);
74         }
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);
78         }
79 #endif
80
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());
84 }
85
86 static void _load_tile_func(GritsTile *tile, gpointer _sat)
87 {
88         g_debug("GritsPluginSat: __load_tile_func - tile=%p", tile);
89         GritsPluginSat *sat = _sat;
90         g_thread_pool_push(sat->threads, tile, NULL);
91 }
92
93 /*************
94  * Callbacks *
95  *************/
96 static void _on_location_changed(GritsViewer *viewer,
97                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
98 {
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);
104 }
105
106 /***********
107  * Methods *
108  ***********/
109 /**
110  * grits_plugin_sat_new:
111  * @viewer: the #GritsViewer to use for drawing
112  *
113  * Create a new instance of the satellite plugin.
114  *
115  * Returns: the new #GritsPluginSat
116  */
117 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
118 {
119         g_debug("GritsPluginSat: new");
120         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
121         sat->viewer = g_object_ref(viewer);
122
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);
127
128         /* Connect signals */
129         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
130                         G_CALLBACK(_on_location_changed), sat);
131
132         /* Add renderers */
133         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
134
135         return sat;
136 }
137
138
139 /****************
140  * GObject code *
141  ****************/
142 /* Plugin init */
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)
148 {
149         g_debug("GritsPluginSat: plugin_init");
150         /* Add methods to the interface */
151 }
152 /* Class/Object init */
153 static void grits_plugin_sat_init(GritsPluginSat *sat)
154 {
155         g_debug("GritsPluginSat: init");
156         /* Set defaults */
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);
163 }
164 static void grits_plugin_sat_dispose(GObject *gobject)
165 {
166         g_debug("GritsPluginSat: dispose");
167         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
168         sat->aborted = TRUE;
169         /* Drop references */
170         if (sat->viewer) {
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);
175                 sat->viewer = NULL;
176                 grits_object_destroy_pointer(&sat->tiles);
177                 g_object_unref(viewer);
178         }
179         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
180 }
181 static void grits_plugin_sat_finalize(GObject *gobject)
182 {
183         g_debug("GritsPluginSat: finalize");
184         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
185         /* Free data */
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);
189
190 }
191 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
192 {
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;
197 }