]> Pileus Git - grits/blob - src/plugins/sat.c
b43e4faf1ccbfd13c7d99615478206b79de223d1
[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(GritsTile *tile, gpointer _sat)
39 {
40         GritsPluginSat *sat = _sat;
41         g_debug("GritsPluginSat: _load_tile start %p", g_thread_self());
42         if (sat->aborted) {
43                 g_debug("GritsPluginSat: _load_tile - aborted");
44                 return;
45         }
46
47         /* Download tile */
48         gchar *path = grits_wms_fetch(sat->wms, tile, GRITS_ONCE, NULL, NULL);
49         if (!path) return; // Canceled/error
50
51         /* Load pixbuf */
52         GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(path, NULL);
53         if (!pixbuf) {
54                 g_warning("GritsPluginSat: _load_tile - Error loading pixbuf %s", path);
55                 g_remove(path);
56                 g_free(path);
57                 return;
58         }
59         g_free(path);
60
61         /* Draw a border */
62 #ifdef DRAW_TILE_BORDER
63         gint    border = 10;
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);
71         }
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);
75         }
76 #endif
77
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());
81 }
82
83 static void _update_tiles(gpointer _, gpointer _sat)
84 {
85         g_debug("GritsPluginSat: _update_tiles");
86         GritsPluginSat *sat = _sat;
87         GritsPoint eye;
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,
91                         _load_tile, sat);
92         grits_tile_gc(sat->tiles, time(NULL)-10, NULL, sat);
93 }
94
95 /*************
96  * Callbacks *
97  *************/
98 static void _on_location_changed(GritsViewer *viewer,
99                 gdouble lat, gdouble lon, gdouble elev, GritsPluginSat *sat)
100 {
101         g_thread_pool_push(sat->threads, NULL+1, NULL);
102 }
103
104 /***********
105  * Methods *
106  ***********/
107 /**
108  * grits_plugin_sat_new:
109  * @viewer: the #GritsViewer to use for drawing
110  *
111  * Create a new instance of the satellite plugin.
112  *
113  * Returns: the new #GritsPluginSat
114  */
115 GritsPluginSat *grits_plugin_sat_new(GritsViewer *viewer)
116 {
117         g_debug("GritsPluginSat: new");
118         GritsPluginSat *sat = g_object_new(GRITS_TYPE_PLUGIN_SAT, NULL);
119         sat->viewer = g_object_ref(viewer);
120
121         /* Load initial tiles */
122         _update_tiles(NULL, sat);
123
124         /* Connect signals */
125         sat->sigid = g_signal_connect(sat->viewer, "location-changed",
126                         G_CALLBACK(_on_location_changed), sat);
127
128         /* Add renderers */
129         grits_viewer_add(viewer, GRITS_OBJECT(sat->tiles), GRITS_LEVEL_WORLD, FALSE);
130
131         return sat;
132 }
133
134
135 /****************
136  * GObject code *
137  ****************/
138 /* Plugin init */
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)
144 {
145         g_debug("GritsPluginSat: plugin_init");
146         /* Add methods to the interface */
147 }
148 /* Class/Object init */
149 static void grits_plugin_sat_init(GritsPluginSat *sat)
150 {
151         g_debug("GritsPluginSat: init");
152         /* Set defaults */
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);
159 }
160 static void grits_plugin_sat_dispose(GObject *gobject)
161 {
162         g_debug("GritsPluginSat: dispose");
163         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
164         sat->aborted = TRUE;
165         /* Drop references */
166         if (sat->viewer) {
167                 GritsViewer *viewer = sat->viewer;
168                 sat->viewer = NULL;
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);
175         }
176         G_OBJECT_CLASS(grits_plugin_sat_parent_class)->dispose(gobject);
177 }
178 static void grits_plugin_sat_finalize(GObject *gobject)
179 {
180         g_debug("GritsPluginSat: finalize");
181         GritsPluginSat *sat = GRITS_PLUGIN_SAT(gobject);
182         /* Free data */
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);
186
187 }
188 static void grits_plugin_sat_class_init(GritsPluginSatClass *klass)
189 {
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;
194 }