]> Pileus Git - aweather/blob - src/plugins/radar.c
Refactor radar plugin
[aweather] / src / plugins / radar.c
1 /*
2  * Copyright (C) 2009-2010 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 #include <config.h>
19 #include <glib/gstdio.h>
20 #include <gtk/gtk.h>
21 #include <gtk/gtkgl.h>
22 #include <gio/gio.h>
23 #include <GL/gl.h>
24 #include <math.h>
25 #include <rsl.h>
26
27 #include <gis.h>
28
29 #include "radar.h"
30 #include "../aweather-location.h"
31
32
33 /**************************
34  * Data loading functions *
35  **************************/
36 /* Convert a sweep to an 2d array of data points */
37 static void _bscan_sweep(GisPluginRadar *self, Sweep *sweep, colormap_t *colormap,
38                 guint8 **data, int *width, int *height)
39 {
40         g_debug("GisPluginRadar: _bscan_sweep - %p, %p, %p",
41                         sweep, colormap, data);
42         /* Calculate max number of bins */
43         int max_bins = 0;
44         for (int i = 0; i < sweep->h.nrays; i++)
45                 max_bins = MAX(max_bins, sweep->ray[i]->h.nbins);
46
47         /* Allocate buffer using max number of bins for each ray */
48         guint8 *buf = g_malloc0(sweep->h.nrays * max_bins * 4);
49
50         /* Fill the data */
51         for (int ri = 0; ri < sweep->h.nrays; ri++) {
52                 Ray *ray  = sweep->ray[ri];
53                 for (int bi = 0; bi < ray->h.nbins; bi++) {
54                         /* copy RGBA into buffer */
55                         //guint val   = dz_f(ray->range[bi]);
56                         guint8 val   = (guint8)ray->h.f(ray->range[bi]);
57                         guint  buf_i = (ri*max_bins+bi)*4;
58                         buf[buf_i+0] = colormap->data[val][0];
59                         buf[buf_i+1] = colormap->data[val][1];
60                         buf[buf_i+2] = colormap->data[val][2];
61                         buf[buf_i+3] = colormap->data[val][3]*0.75; // TESTING
62                         if (val == BADVAL     || val == RFVAL      || val == APFLAG ||
63                             val == NOTFOUND_H || val == NOTFOUND_V || val == NOECHO) {
64                                 buf[buf_i+3] = 0x00; // transparent
65                         }
66                 }
67         }
68
69         /* set output */
70         *width  = max_bins;
71         *height = sweep->h.nrays;
72         *data   = buf;
73 }
74
75 /* Load a sweep as the active texture */
76 static gboolean _load_sweep(gpointer _self)
77 {
78         GisPluginRadar *self = _self;
79         if (!self->cur_sweep)
80                 return FALSE;
81         g_debug("GisPluginRadar: _load_sweep - %p", self->cur_sweep);
82         int height, width;
83         guint8 *data;
84         _bscan_sweep(self, self->cur_sweep, self->cur_colormap, &data, &width, &height);
85         glDeleteTextures(1, &self->cur_sweep_tex);
86         glGenTextures(1, &self->cur_sweep_tex);
87         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
88         glPixelStorei(GL_PACK_ALIGNMENT, 1);
89         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
90         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
91         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
92         glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
93                         GL_RGBA, GL_UNSIGNED_BYTE, data);
94         g_free(data);
95         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
96         return FALSE;
97 }
98
99 /* Load the colormap for a sweep */
100 static void _load_colormap(GisPluginRadar *self, gchar *table)
101 {
102         g_debug("GisPluginRadar: _load_colormap - %s", table);
103         /* Set colormap so we can draw it on expose */
104         for (int i = 0; colormaps[i].name; i++)
105                 if (g_str_equal(colormaps[i].name, table))
106                         self->cur_colormap = &colormaps[i];
107 }
108
109
110 /***************
111  * GUI loading *
112  ***************/
113 /* Setup a loading screen in the tab */
114 static void _load_gui_pre(GisPluginRadar *self)
115 {
116         g_debug("GisPluginRadar: _load_gui_pre");
117
118         gdk_threads_enter();
119         /* Set up progress bar */
120         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
121         if (child)
122                 gtk_widget_destroy(child);
123
124         GtkWidget *vbox = gtk_vbox_new(FALSE, 10);
125         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
126         self->progress_bar   = gtk_progress_bar_new();
127         self->progress_label = gtk_label_new("Loading radar...");
128         gtk_box_pack_start(GTK_BOX(vbox), self->progress_bar,   FALSE, FALSE, 0);
129         gtk_box_pack_start(GTK_BOX(vbox), self->progress_label, FALSE, FALSE, 0);
130         gtk_container_add(GTK_CONTAINER(self->config_body), vbox);
131         gtk_widget_show_all(self->config_body);
132
133         /* Clear radar */
134         if (self->cur_radar)
135                 RSL_free_radar(self->cur_radar);
136         self->cur_radar = NULL;
137         self->cur_sweep = NULL;
138         gtk_widget_queue_draw(GTK_WIDGET(self->viewer));
139         gdk_threads_leave();
140 }
141
142 /* Update pogress bar of loading screen */
143 static void _load_gui_update(char *path, goffset cur, goffset total, gpointer _self)
144 {
145         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
146         double percent = (double)cur/total;
147
148         //g_debug("GisPluginRadar: cache_chunk_cb - %lld/%lld = %.2f%%",
149         //              cur, total, percent*100);
150
151         gdk_threads_enter();
152         gtk_progress_bar_set_fraction(GTK_PROGRESS_BAR(self->progress_bar), MIN(percent, 1.0));
153
154         gchar *msg = g_strdup_printf("Loading radar... %5.1f%% (%.2f/%.2f MB)",
155                         percent*100, (double)cur/1000000, (double)total/1000000);
156         gtk_label_set_text(GTK_LABEL(self->progress_label), msg);
157         gdk_threads_leave();
158         g_free(msg);
159 }
160
161 /* Update pogress bar of loading screen */
162 static void _load_gui_error(GisPluginRadar *self, gchar *error)
163 {
164         gchar *msg = g_strdup_printf(
165                         "GisPluginRadar: error loading radar - %s", error);
166         g_warning("%s", msg);
167         gdk_threads_enter();
168         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
169         if (child)
170                 gtk_widget_destroy(child);
171         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new(msg));
172         gtk_widget_show_all(self->config_body);
173         gdk_threads_leave();
174         g_free(msg);
175 }
176
177 /* Clear loading screen and add sweep selectors */
178 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self);
179 static void _load_gui_success(GisPluginRadar *self, Radar *radar)
180 {
181         g_debug("GisPluginRadar: _load_gui_success - %p", radar);
182         /* Clear existing items */
183         gdk_threads_enter();
184         GtkWidget *child = gtk_bin_get_child(GTK_BIN(self->config_body));
185         if (child)
186                 gtk_widget_destroy(child);
187
188         gdouble elev;
189         guint rows = 1, cols = 1, cur_cols;
190         gchar row_label_str[64], col_label_str[64], button_str[64];
191         GtkWidget *row_label, *col_label, *button = NULL, *elev_box = NULL;
192         GtkWidget *table = gtk_table_new(rows, cols, FALSE);
193
194         for (guint vi = 0; vi < radar->h.nvolumes; vi++) {
195                 Volume *vol = radar->v[vi];
196                 if (vol == NULL) continue;
197                 rows++; cols = 1; elev = 0;
198
199                 /* Row label */
200                 g_snprintf(row_label_str, 64, "<b>%s:</b>", vol->h.type_str);
201                 row_label = gtk_label_new(row_label_str);
202                 gtk_label_set_use_markup(GTK_LABEL(row_label), TRUE);
203                 gtk_misc_set_alignment(GTK_MISC(row_label), 1, 0.5);
204                 gtk_table_attach(GTK_TABLE(table), row_label,
205                                 0,1, rows-1,rows, GTK_FILL,GTK_FILL, 5,0);
206
207                 for (guint si = 0; si < vol->h.nsweeps; si++) {
208                         Sweep *sweep = vol->sweep[si];
209                         if (sweep == NULL || sweep->h.elev == 0) continue;
210                         if (sweep->h.elev != elev) {
211                                 cols++;
212                                 elev = sweep->h.elev;
213
214                                 /* Column label */
215                                 g_object_get(table, "n-columns", &cur_cols, NULL);
216                                 if (cols >  cur_cols) {
217                                         g_snprintf(col_label_str, 64, "<b>%.2f°</b>", elev);
218                                         col_label = gtk_label_new(col_label_str);
219                                         gtk_label_set_use_markup(GTK_LABEL(col_label), TRUE);
220                                         gtk_widget_set_size_request(col_label, 50, -1);
221                                         gtk_table_attach(GTK_TABLE(table), col_label,
222                                                         cols-1,cols, 0,1, GTK_FILL,GTK_FILL, 0,0);
223                                 }
224
225                                 elev_box = gtk_hbox_new(TRUE, 0);
226                                 gtk_table_attach(GTK_TABLE(table), elev_box,
227                                                 cols-1,cols, rows-1,rows, GTK_FILL,GTK_FILL, 0,0);
228                         }
229
230
231                         /* Button */
232                         g_snprintf(button_str, 64, "%3.2f", elev);
233                         button = gtk_radio_button_new_with_label_from_widget(
234                                         GTK_RADIO_BUTTON(button), button_str);
235                         gtk_widget_set_size_request(button, -1, 26);
236                         //button = gtk_radio_button_new_from_widget(GTK_RADIO_BUTTON(button));
237                         //gtk_widget_set_size_request(button, -1, 22);
238                         g_object_set(button, "draw-indicator", FALSE, NULL);
239                         gtk_box_pack_end(GTK_BOX(elev_box), button, TRUE, TRUE, 0);
240
241                         g_object_set_data(G_OBJECT(button), "type",  vol->h.type_str);
242                         g_object_set_data(G_OBJECT(button), "sweep", sweep);
243                         g_signal_connect(button, "clicked", G_CALLBACK(_on_sweep_clicked), self);
244                 }
245         }
246         gtk_container_add(GTK_CONTAINER(self->config_body), table);
247         gtk_widget_show_all(table);
248         gdk_threads_leave();
249 }
250
251
252 /*****************
253  * Radar caching *
254  *****************/
255 /* Download a compressed radar file from the remote server */
256 static gchar *_download_radar(GisPluginRadar *self, const gchar *site, const gchar *time)
257 {
258         g_debug("GisPluginRadar: _download_radar - %s, %s", site, time);
259
260         /* format: http://mesonet.agron.iastate.edu/data/nexrd2/raw/KABR/KABR_20090510_0323 */
261         gchar *base  = gis_prefs_get_string(self->prefs, "aweather/nexrad_url", NULL);
262         gchar *local = g_strdup_printf("%s/%s_%s", site, site, time);
263         gchar *uri   = g_strconcat(base, "/", local, NULL);
264         GisCacheType mode = gis_viewer_get_offline(self->viewer) ? GIS_LOCAL : GIS_UPDATE;
265         return gis_http_fetch(self->http, uri, local, mode, _load_gui_update, self);
266 }
267
268 /* Decompress a radar file using wsr88dec */
269 static gchar *_decompress_radar(GisPluginRadar *self, char *compressed)
270 {
271         char *decompressed = g_strconcat(compressed, ".raw", NULL);
272         if (g_file_test(decompressed, G_FILE_TEST_EXISTS)) {
273                 struct stat comp, dec;
274                 g_stat(compressed, &comp);
275                 g_stat(decompressed, &dec);
276                 if (dec.st_mtime >= comp.st_mtime)
277                         return decompressed;
278         }
279         g_debug("GisPluginRadar: _decompress_radar - %s", decompressed);
280         char *argv[] = {"wsr88ddec", compressed, decompressed, NULL};
281         gint status;
282         GError *error = NULL;
283         g_spawn_sync(
284                 NULL,    // const gchar *working_directory
285                 argv,    // gchar **argv
286                 NULL,    // gchar **envp
287                 G_SPAWN_SEARCH_PATH, // GSpawnFlags flags
288                 NULL,    // GSpawnChildSetupFunc child_setup
289                 NULL,    // gpointer user_data
290                 NULL,    // gchar *standard_output
291                 NULL,    // gchar *standard_output
292                 &status, // gint *exit_status
293                 &error); // GError **error
294         if (error) {
295                 g_warning("GisPluginRadar: _decompress_radar - %s", error->message);
296                 g_error_free(error);
297                 return NULL;
298         }
299         if (status != 0) {
300                 gchar *msg = g_strdup_printf("wsr88ddec exited with status %d", status);
301                 g_warning("GisPluginRadar: _decompress_radar - %s", msg);
302                 g_free(msg);
303                 return NULL;
304         }
305         return decompressed;
306 }
307
308
309 /****************
310  * Misc helpers *
311  ****************/
312 /* Set the radar file based on cur_site andcur_time
313  * This should be run in a separatet hread */
314 static gboolean _set_radar_cb(GisPluginRadar *self)
315 {
316         g_debug("GisPluginRadar: _set_radar_cb");
317
318         _load_gui_pre(self);
319
320         /* Download and decompress the radar */
321         gchar *compressed = _download_radar(self,
322                         self->cur_site, self->cur_time);
323         if (!compressed) {
324                 _load_gui_error(self, "Download failed");
325                 goto fail;
326         }
327
328         /* Decompress radar */
329         gchar *decompressed = _decompress_radar(self, compressed);
330         g_free(compressed);
331         if (!decompressed) {
332                 _load_gui_error(self, "Decompression failed");
333                 goto fail;
334         }
335
336         /* Load the radar file */
337         g_debug("GisPluginRadar: _set_radar_cb - loading %s", decompressed);
338         RSL_read_these_sweeps("all", NULL);
339         self->cur_radar = RSL_wsr88d_to_radar(decompressed, self->cur_site);
340         g_free(decompressed);
341         if (!self->cur_radar) {
342                 _load_gui_error(self, "Loading failed");
343                 goto fail;
344         }
345
346         /* Load the first sweep by default */
347         Radar *radar = self->cur_radar;
348         Sweep *sweep = NULL;
349         gchar *type_str = NULL;
350         for (int vi = 0; vi < radar->h.nvolumes; vi++) {
351                 if (radar->v[vi] == NULL)
352                         continue;
353                 for (int si = 0; si < radar->v[vi]->h.nsweeps; si++) {
354                         if (radar->v[vi]->sweep[si] == NULL)
355                                 continue;
356                         sweep    = radar->v[vi]->sweep[si];
357                         type_str = radar->v[vi]->h.type_str;
358                         break;
359                 }
360                 break;
361         }
362         if (!type_str) {
363                 _load_gui_error(self, "No sweeps found");
364                 goto fail;
365         }
366
367         /* Load weep */
368         g_debug("GisPluginRadar: _set_radar_cb - setting sweep");
369         self->cur_sweep = sweep;
370         _load_colormap(self, type_str);
371         g_idle_add(_load_sweep, self);
372         _load_gui_success(self, radar);
373
374         /* Let other threads go */
375         g_mutex_unlock(self->load_mutex);
376         return TRUE;
377
378 fail:
379         g_mutex_unlock(self->load_mutex);
380         return TRUE;
381 }
382
383 static void _set_radar(GisPluginRadar *self,
384                 gchar *site, gchar *time)
385 {
386         if (site) self->cur_site = site;
387         if (time) self->cur_time = time;
388         if (!self->cur_site || !self->cur_time)
389                 return;
390
391         /* Abort any current downloads */
392         soup_session_abort(self->http->soup);
393
394         g_mutex_lock(self->load_mutex);
395
396         g_thread_create((GThreadFunc)_set_radar_cb, self, FALSE, NULL);
397 }
398
399
400 /*************
401  * Callbacks *
402  *************/
403 static void _on_sweep_clicked(GtkRadioButton *button, gpointer _self)
404 {
405         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
406         _load_colormap(self, g_object_get_data(G_OBJECT(button), "type"));
407         self->cur_sweep = g_object_get_data(G_OBJECT(button), "sweep");
408         _load_sweep(self);
409 }
410
411 static void _on_time_changed(GisViewer *viewer, const char *time, gpointer _self)
412 {
413         g_debug("GisPluginRadar: _on_time_changed");
414         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
415         _set_radar(self, self->cur_site, g_strdup(time));
416 }
417
418 static void _on_location_changed(GisViewer *viewer,
419                 gdouble lat, gdouble lon, gdouble elev,
420                 gpointer _self)
421 {
422         g_debug("GisPluginRadar: _on_location_changed");
423         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
424         gdouble min_dist = EARTH_R / 5;
425         city_t *city, *min_city = NULL;
426         for (city = cities; city->type; city++) {
427                 if (city->type != LOCATION_CITY)
428                         continue;
429                 gdouble city_loc[3] = {};
430                 gdouble eye_loc[3]  = {lat, lon, elev};
431                 lle2xyz(city->lat, city->lon, city->elev,
432                                 &city_loc[0], &city_loc[1], &city_loc[2]);
433                 lle2xyz(lat, lon, elev,
434                                 &eye_loc[0], &eye_loc[1], &eye_loc[2]);
435                 gdouble dist = distd(city_loc, eye_loc);
436                 if (dist < min_dist) {
437                         min_dist = dist;
438                         min_city = city;
439                 }
440         }
441         static city_t *last_city = NULL;
442         if (min_city && min_city != last_city)
443                 _set_radar(self, min_city->code, self->cur_time);
444         last_city = min_city;
445 }
446
447 static gpointer _draw_radar(GisCallback *callback, gpointer _self)
448 {
449         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
450
451         /* Draw wsr88d */
452         if (self->cur_sweep == NULL)
453                 return NULL;
454         g_debug("GisPluginRadar: _draw_radar");
455         Sweep *sweep = self->cur_sweep;
456
457         g_debug("GisPluginRadar: _draw_radar - setting camera");
458         Radar_header *h = &self->cur_radar->h;
459         gdouble lat  = (double)h->latd + (double)h->latm/60 + (double)h->lats/(60*60);
460         gdouble lon  = (double)h->lond + (double)h->lonm/60 + (double)h->lons/(60*60);
461         gdouble elev = h->height;
462         gis_viewer_center_position(self->viewer, lat, lon, elev);
463
464         glDisable(GL_ALPHA_TEST);
465         glDisable(GL_CULL_FACE);
466         glDisable(GL_LIGHTING);
467         glEnable(GL_TEXTURE_2D);
468         glEnable(GL_POLYGON_OFFSET_FILL);
469         glPolygonOffset(1.0, -2.0);
470         glColor4f(1,1,1,1);
471
472         /* Draw the rays */
473         glBindTexture(GL_TEXTURE_2D, self->cur_sweep_tex);
474         g_message("Tex = %d", self->cur_sweep_tex);
475         glBegin(GL_TRIANGLE_STRIP);
476         for (int ri = 0; ri <= sweep->h.nrays; ri++) {
477                 Ray  *ray = NULL;
478                 double angle = 0;
479                 if (ri < sweep->h.nrays) {
480                         ray = sweep->ray[ri];
481                         angle = deg2rad(ray->h.azimuth - ((double)ray->h.beam_width/2.));
482                 } else {
483                         /* Do the right side of the last sweep */
484                         ray = sweep->ray[ri-1];
485                         angle = deg2rad(ray->h.azimuth + ((double)ray->h.beam_width/2.));
486                 }
487
488                 double lx = sin(angle);
489                 double ly = cos(angle);
490
491                 double near_dist = ray->h.range_bin1;
492                 double far_dist  = ray->h.nbins*ray->h.gate_size + ray->h.range_bin1;
493
494                 /* (find middle of bin) / scale for opengl */
495                 // near left
496                 glTexCoord2f(0.0, (double)ri/sweep->h.nrays-0.01);
497                 glVertex3f(lx*near_dist, ly*near_dist, 2.0);
498
499                 // far  left
500                 // todo: correct range-height function
501                 double height = sin(deg2rad(ray->h.elev)) * far_dist;
502                 glTexCoord2f(1.0, (double)ri/sweep->h.nrays-0.01);
503                 glVertex3f(lx*far_dist,  ly*far_dist, height);
504         }
505         glEnd();
506         //g_print("ri=%d, nr=%d, bw=%f\n", _ri, sweep->h.nrays, sweep->h.beam_width);
507
508         /* Texture debug */
509         //glBegin(GL_QUADS);
510         //glTexCoord2d( 0.,  0.); glVertex3f(-500.,   0., 0.); // bot left
511         //glTexCoord2d( 0.,  1.); glVertex3f(-500., 500., 0.); // top left
512         //glTexCoord2d( 1.,  1.); glVertex3f( 0.,   500., 3.); // top right
513         //glTexCoord2d( 1.,  0.); glVertex3f( 0.,     0., 3.); // bot right
514         //glEnd();
515
516         return NULL;
517 }
518
519 static gpointer _draw_hud(GisCallback *callback, gpointer _self)
520 {
521         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
522         if (self->cur_sweep == NULL)
523                 return NULL;
524         g_debug("GisPluginRadar: _draw_hud");
525
526         /* Print the color table */
527         glMatrixMode(GL_MODELVIEW ); glLoadIdentity();
528         glMatrixMode(GL_PROJECTION); glLoadIdentity();
529         glDisable(GL_TEXTURE_2D);
530         glDisable(GL_ALPHA_TEST);
531         glDisable(GL_CULL_FACE);
532         glDisable(GL_LIGHTING);
533         glEnable(GL_COLOR_MATERIAL);
534         glBegin(GL_QUADS);
535         int i;
536         for (i = 0; i < 256; i++) {
537                 glColor4ubv(self->cur_colormap->data[i]);
538                 glVertex3f(-1.0, (float)((i  ) - 256/2)/(256/2), 0.0); // bot left
539                 glVertex3f(-1.0, (float)((i+1) - 256/2)/(256/2), 0.0); // top left
540                 glVertex3f(-0.9, (float)((i+1) - 256/2)/(256/2), 0.0); // top right
541                 glVertex3f(-0.9, (float)((i  ) - 256/2)/(256/2), 0.0); // bot right
542         }
543         glEnd();
544
545         return NULL;
546 }
547
548
549 /***********
550  * Methods *
551  ***********/
552 GisPluginRadar *gis_plugin_radar_new(GisViewer *viewer, GisPrefs *prefs)
553 {
554         /* TODO: move to constructor if possible */
555         g_debug("GisPluginRadar: new");
556         GisPluginRadar *self = g_object_new(GIS_TYPE_PLUGIN_RADAR, NULL);
557         self->viewer = viewer;
558         self->prefs  = prefs;
559         self->time_changed_id = g_signal_connect(viewer, "time-changed",
560                         G_CALLBACK(_on_time_changed), self);
561         self->location_changed_id = g_signal_connect(viewer, "location-changed",
562                         G_CALLBACK(_on_location_changed), self);
563
564         for (city_t *city = cities; city->type; city++) {
565                 if (city->type != LOCATION_CITY)
566                         continue;
567                 g_debug("Adding marker for %s %s", city->code, city->label);
568                 GisMarker *marker = gis_marker_new(city->label);
569                 gis_point_set_lle(gis_object_center(GIS_OBJECT(marker)),
570                                 city->lat, city->lon, city->elev);
571                 GIS_OBJECT(marker)->lod = EARTH_R/2;
572                 gis_viewer_add(self->viewer, GIS_OBJECT(marker), GIS_LEVEL_OVERLAY, FALSE);
573         }
574
575         /* Add renderers */
576         GisCallback *radar_cb = gis_callback_new(_draw_radar, self);
577         GisCallback *hud_cb   = gis_callback_new(_draw_hud, self);
578
579         gis_viewer_add(viewer, GIS_OBJECT(radar_cb),    GIS_LEVEL_WORLD, TRUE);
580         gis_viewer_add(viewer, GIS_OBJECT(hud_cb),      GIS_LEVEL_HUD,   FALSE);
581
582         return self;
583 }
584
585 static GtkWidget *gis_plugin_radar_get_config(GisPlugin *_self)
586 {
587         GisPluginRadar *self = GIS_PLUGIN_RADAR(_self);
588         return self->config_body;
589 }
590
591
592 /****************
593  * GObject code *
594  ****************/
595 /* Plugin init */
596 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface);
597 G_DEFINE_TYPE_WITH_CODE(GisPluginRadar, gis_plugin_radar, G_TYPE_OBJECT,
598                 G_IMPLEMENT_INTERFACE(GIS_TYPE_PLUGIN,
599                         gis_plugin_radar_plugin_init));
600 static void gis_plugin_radar_plugin_init(GisPluginInterface *iface)
601 {
602         g_debug("GisPluginRadar: plugin_init");
603         /* Add methods to the interface */
604         iface->get_config = gis_plugin_radar_get_config;
605 }
606 /* Class/Object init */
607 static void gis_plugin_radar_init(GisPluginRadar *self)
608 {
609         g_debug("GisPluginRadar: class_init");
610         /* Set defaults */
611         self->http = gis_http_new("/nexrad/level2/");
612         self->config_body = gtk_alignment_new(0, 0, 1, 1);
613         self->load_mutex = g_mutex_new();
614         gtk_container_set_border_width(GTK_CONTAINER(self->config_body), 5);
615         gtk_container_add(GTK_CONTAINER(self->config_body), gtk_label_new("No radar loaded"));
616 }
617 static void gis_plugin_radar_dispose(GObject *gobject)
618 {
619         g_debug("GisPluginRadar: dispose");
620         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
621         g_signal_handler_disconnect(self->viewer, self->time_changed_id);
622         /* Drop references */
623         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->dispose(gobject);
624 }
625 static void gis_plugin_radar_finalize(GObject *gobject)
626 {
627         g_debug("GisPluginRadar: finalize");
628         GisPluginRadar *self = GIS_PLUGIN_RADAR(gobject);
629         /* Free data */
630         gis_http_free(self->http);
631         g_mutex_free(self->load_mutex);
632         G_OBJECT_CLASS(gis_plugin_radar_parent_class)->finalize(gobject);
633
634 }
635 static void gis_plugin_radar_class_init(GisPluginRadarClass *klass)
636 {
637         g_debug("GisPluginRadar: class_init");
638         GObjectClass *gobject_class = (GObjectClass*)klass;
639         gobject_class->dispose  = gis_plugin_radar_dispose;
640         gobject_class->finalize = gis_plugin_radar_finalize;
641 }
642