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/>.
19 #include <glib/gstdio.h>
21 #include <gtk/gtkgl.h>
31 #include "../aweather-location.h"
33 /* Drawing functions */
34 static void _draw_hud(GisCallback *callback, gpointer _self)
36 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
40 g_debug("GisPluginRadar: _draw_hud");
41 /* Print the color table */
42 glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
43 glMatrixMode(GL_PROJECTION); glLoadIdentity();
44 glDisable(GL_TEXTURE_2D);
45 glDisable(GL_ALPHA_TEST);
46 glDisable(GL_CULL_FACE);
47 glDisable(GL_LIGHTING);
48 glEnable(GL_COLOR_MATERIAL);
51 for (i = 0; i < 256; i++) {
52 glColor4ubv(self->colormap->data[i]);
53 glVertex3f(-1.0, (float)((i ) - 256/2)/(256/2), 0.0); // bot left
54 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
55 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
56 glVertex3f(-0.9, (float)((i ) - 256/2)/(256/2), 0.0); // bot right
65 /* Clear the config area */
66 static void _load_gui_clear(GisPluginRadar *self)
68 GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
70 gtk_widget_destroy(child);
73 /* Setup a loading screen in the tab */
74 static void _load_gui_pre(GisPluginRadar *self)
76 g_debug("GisPluginRadar: _load_gui_pre");
78 /* Set up progress bar */
79 _load_gui_clear(self);
80 GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
81 gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
82 self->progress_bar = gtk_progress_bar_new();
83 self->progress_label = gtk_label_new("Loading radar...");
84 gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar, FALSE, FALSE, 0);
85 gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
86 gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
87 gtk_widget_show_all(self->config_body);
90 /* Update pogress bar of loading screen */
91 static void _load_gui_update(char *path, goffset cur, goffset total, gpointer _self)
93 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
94 double percent = (double)cur/total;
96 //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
97 // cur, total, percent*100);
99 gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
101 gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
102 percent*100, (double)cur/1000000, (double)total/1000000);
103 gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
107 /* Update pogress bar of loading screen */
108 static void _load_gui_error(GisPluginRadar *self, gchar *error)
110 gchar *msg = g_strdup_printf(
111 "GisPluginRadar: error loading radar - %s", error);
112 g_warning("%s", msg);
113 _load_gui_clear(self);
114 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(msg));
115 gtk_widget_show_all(self->config_body);
119 /* Clear loading screen and add sweep selectors */
120 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
121 static void _load_gui_success(GisPluginRadar *self, AWeatherLevel2 *level2)
123 Radar *radar = level2->radar;
124 g_debug("GisPluginRadar: _load_gui_success - %p", radar);
125 /* Clear existing items */
126 _load_gui_clear(self);
129 guint rows = 1, cols = 1, cur_cols;
130 gchar row_label_str[64], col_label_str[64], button_str[64];
131 GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
132 GtkWidget *table = gtk_table_new(rows, cols, FALSE);
134 for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
135 Volume *vol = radar->v[vi];
136 if (vol == NULL) continue;
137 rows++; cols = 1; elev = 0;
140 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
141 row_label = gtk_label_new(row_label_str);
142 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
143 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
144 gtk_table_attach(GTK_TABLE(table), row_label,
145 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
147 for (guint si = 0; si < vol->h.nsweeps; si++) {
148 Sweep *sweep = vol->sweep[si];
149 if (sweep == NULL || sweep->h.elev == 0) continue;
150 if (sweep->h.elev != elev) {
152 elev = sweep->h.elev;
155 g_object_get(table, "n-columns", &cur_cols, NULL);
156 if (cols > cur_cols) {
157 g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
158 col_label = gtk_label_new(col_label_str);
159 gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
160 gtk_widget_set_size_request(col_label, 50, -1);
161 gtk_table_attach(GTK_TABLE(table), col_label,
162 cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
165 elev_box = gtk_hbox_new(TRUE, 0);
166 gtk_table_attach(GTK_TABLE(table), elev_box,
167 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
172 g_snprintf(button_str, 64, "%3.2f", elev);
173 button = gtk_radio_button_new_with_label_from_widget(
174 GTK_RADIO_BUTTON(button), button_str);
175 gtk_widget_set_size_request(button, -1, 26);
176 //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
177 //gtk_widget_set_size_request(button, -1, 22);
178 g_object_set(button, "draw-indicator", FALSE, NULL);
179 gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
181 g_object_set_data(G_OBJECT(button), "level2", (gpointer)level2);
182 g_object_set_data(G_OBJECT(button), "type", (gpointer)vi);
183 g_object_set_data(G_OBJECT(button), "elev", (gpointer)(int)(elev*100));
184 g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
187 gtk_container_add(GTK_CONTAINER(self->config_body), table);
188 gtk_widget_show_all(table);
191 static gchar *_download_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
193 g_debug("GisPluginRadar: _download_radar - %s, %s", site, time);
195 /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
196 gchar *base = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
197 gchar *local = g_strdup_printf("%s/%s_%s", site, site, time);
198 gchar *uri = g_strconcat(base, "/", local, NULL);
199 GisCacheType mode = gis_viewer_get_offline(self->viewer) ? GIS_LOCAL : GIS_UPDATE;
200 gchar *file = gis_http_fetch(self->http, uri, local, mode, _load_gui_update, self);
207 struct SetRadarData {
208 GisPluginRadar *self;
212 static gpointer _set_radar_cb(gpointer _data)
214 g_debug("GisPluginRadar: _set_radar_cb");
215 struct SetRadarData *data = _data;
216 GisPluginRadar *self = data->self;
221 gis_viewer_remove(self->viewer, self->radar);
224 gchar *file = _download_radar(self, data->site, data->time);
226 _load_gui_error(self, "Download failed");
229 AWeatherLevel2 *level2 = aweather_level2_new_from_file(
230 self->viewer, colormaps, file, data->site);
232 _load_gui_error(self, "Loading radar failed");
236 self->colormap = level2->sweep_colors;
237 self->radar = gis_viewer_add(self->viewer, GIS_OBJECT(level2), GIS_LEVEL_WORLD, TRUE);
238 _load_gui_success(self, level2);
248 static void _set_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
252 //soup_session_abort(self->http->soup);
253 //g_mutex_lock(self->load_mutex);
254 struct SetRadarData *data = g_new(struct SetRadarData, 1);
256 data->site = g_strdup(site);
257 data->time = g_strdup(time);
258 g_thread_create(_set_radar_cb, data, FALSE, NULL);
264 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
266 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
267 AWeatherLevel2 *level2 = g_object_get_data(G_OBJECT(button), "level2");
268 gint type = (gint)g_object_get_data(G_OBJECT(button), "type");
269 gint elev = (gint)g_object_get_data(G_OBJECT(button), "elev");
270 aweather_level2_set_sweep(level2, type, (float)elev/100);
271 self->colormap = level2->sweep_colors;
274 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
276 g_debug("GisPluginRadar: _on_time_changed");
277 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
278 self->cur_time = g_strdup(time);
279 _set_radar(self, self->cur_site, self->cur_time);
282 static void _on_location_changed(GisViewer *viewer,
283 gdouble lat, gdouble lon, gdouble elev,
286 g_debug("GisPluginRadar: _on_location_changed");
287 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
288 gdouble min_dist = EARTH_R / 5;
289 city_t *city, *min_city = NULL;
290 for (city = cities; city->type; city++) {
291 if (city->type != LOCATION_CITY)
293 gdouble city_loc[3] = {};
294 gdouble eye_loc[3] = {lat, lon, elev};
295 lle2xyz(city->lat, city->lon, city->elev,
296 &city_loc[0], &city_loc[1], &city_loc[2]);
297 lle2xyz(lat, lon, elev,
298 &eye_loc[0], &eye_loc[1], &eye_loc[2]);
299 gdouble dist = distd(city_loc, eye_loc);
300 if (dist < min_dist) {
305 static city_t *last_city = NULL;
306 if (min_city && min_city != last_city){
307 self->cur_site = min_city->code;
308 _set_radar(self, self->cur_site, self->cur_time);
310 last_city = min_city;
317 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
319 /* TODO: move to constructor if possible */
320 g_debug("GisPluginRadar: new");
321 GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
322 self->viewer = viewer;
324 self->time_changed_id = g_signal_connect(viewer, "time-changed",
325 G_CALLBACK(_on_time_changed), self);
326 self->location_changed_id = g_signal_connect(viewer, "location-changed",
327 G_CALLBACK(_on_location_changed), self);
329 for (city_t *city = cities; city->type; city++) {
330 if (city->type != LOCATION_CITY)
332 g_debug("Adding marker for %s %s", city->code, city->label);
333 GisMarker *marker = gis_marker_new(city->label);
334 gis_point_set_lle(gis_object_center(GIS_OBJECT(marker)),
335 city->lat, city->lon, city->elev);
336 GIS_OBJECT(marker)->lod = EARTH_R/2;
337 gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, FALSE);
340 GisCallback *hud_cb = gis_callback_new(_draw_hud, self);
341 gis_viewer_add(viewer, GIS_OBJECT(hud_cb), GIS_LEVEL_HUD, FALSE);
346 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
348 GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
349 return self->config_body;
357 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
358 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
359 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
360 gis_plugin_radar_plugin_init));
361 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
363 g_debug("GisPluginRadar: plugin_init");
364 /* Add methods to the interface */
365 iface->get_config = gis_plugin_radar_get_config;
367 /* Class/Object init */
368 static void gis_plugin_radar_init(GisPluginRadar *self)
370 g_debug("GisPluginRadar: class_init");
372 self->http = gis_http_new("/nexrad/level2/");
373 self->config_body = gtk_alignment_new(0, 0, 1, 1);
374 //self->load_mutex = g_mutex_new();
375 gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
376 gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
378 static void gis_plugin_radar_dispose(GObject *gobject)
380 g_debug("GisPluginRadar: dispose");
381 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
382 g_signal_handler_disconnect(self->viewer, self->time_changed_id);
383 /* Drop references */
384 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
386 static void gis_plugin_radar_finalize(GObject *gobject)
388 g_debug("GisPluginRadar: finalize");
389 GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
391 gis_http_free(self->http);
392 //g_mutex_free(self->load_mutex);
393 G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
396 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
398 g_debug("GisPluginRadar: class_init");
399 GObjectClass *gobject_class = (GObjectClass*)klass;
400 gobject_class->dispose = gis_plugin_radar_dispose;
401 gobject_class->finalize = gis_plugin_radar_finalize;