]> Pileus Git - grits/blobdiff - src/plugins/elev.c
Document GisPluginElev
[grits] / src / plugins / elev.c
index adb25ee989873a81db0e09b99b63318246549fb4..3a5c031685cba4904fc66c249549fb98e848a901 100644 (file)
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+/**
+ * SECTION:elev
+ * @short_description: Elevation plugin
+ *
+ * #GisPluginElev provides access to ground elevation. It does this in two ways:
+ * First, it provides a height function used by the viewer when drawing the
+ * world. Second, it can load the elevation data into an image and draw a
+ * greyscale elevation overlay on the planets surface.
+ */
+
 #include <gtk/gtkgl.h>
+#include <glib/gstdio.h>
 #include <GL/gl.h>
 
 #include <gis.h>
@@ -25,6 +36,7 @@
 #define MAX_RESOLUTION 500
 #define TILE_WIDTH     1024
 #define TILE_HEIGHT    512
+#define TILE_SIZE      (TILE_WIDTH*TILE_HEIGHT*sizeof(guint16))
 
 struct _TileData {
        /* OpenGL has to be first to make gis_opengl_render_tiles happy */
@@ -32,12 +44,12 @@ struct _TileData {
        guint16   *bil;
 };
 
-static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
+static gdouble _height_func(gdouble lat, gdouble lon, gpointer _elev)
 {
-       GisPluginElev *self = _self;
-       if (!self) return 0;
+       GisPluginElev *elev = _elev;
+       if (!elev) return 0;
 
-       GisTile *tile = gis_tile_find(self->tiles, lat, lon);
+       GisTile *tile = gis_tile_find(elev->tiles, lat, lon);
        if (!tile) return 0;
 
        struct _TileData *data = tile->data;
@@ -77,12 +89,10 @@ static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
        gint16 px01 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr  ),w-1)];
        gint16 px11 = bil[MIN((y_flr+1),h-1)*w + MIN((x_flr+1),w-1)];
 
-       gdouble elev =
-               px00 * (1-x_rem) * (1-y_rem) +
-               px10 * (  x_rem) * (1-y_rem) +
-               px01 * (1-x_rem) * (  y_rem) +
-               px11 * (  x_rem) * (  y_rem);
-       return elev;
+       return px00 * (1-x_rem) * (1-y_rem) +
+              px10 * (  x_rem) * (1-y_rem) +
+              px01 * (1-x_rem) * (  y_rem) +
+              px11 * (  x_rem) * (  y_rem);
 }
 
 /**********************
@@ -91,7 +101,7 @@ static gdouble _height_func(gdouble lat, gdouble lon, gpointer _self)
 #define LOAD_BIL    TRUE
 #define LOAD_OPENGL FALSE
 struct _LoadTileData {
-       GisPluginElev    *self;
+       GisPluginElev    *elev;
        gchar            *path;
        GisTile          *tile;
        GdkPixbuf        *pixbuf;
@@ -99,9 +109,16 @@ struct _LoadTileData {
 };
 static guint16 *_load_bil(gchar *path)
 {
-       gchar *data;
-       g_file_get_contents(path, &data, NULL, NULL);
+       gsize len;
+       gchar *data = NULL;
+       g_file_get_contents(path, &data, &len, NULL);
        g_debug("GisPluginElev: load_bil %p", data);
+       if (len != TILE_SIZE) {
+               g_warning("GisPluginElev: _load_bil - unexpected tile size %d, != %d",
+                               len, TILE_SIZE);
+               g_free(data);
+               return NULL;
+       }
        return (guint16*)data;
 }
 static GdkPixbuf *_load_pixbuf(guint16 *bil)
@@ -156,7 +173,7 @@ static gboolean _load_tile_cb(gpointer _load)
 {
        struct _LoadTileData *load = _load;
        g_debug("GisPluginElev: _load_tile_cb: %s", load->path);
-       GisPluginElev    *self   = load->self;
+       GisPluginElev    *elev   = load->elev;
        GisTile          *tile   = load->tile;
        GdkPixbuf        *pixbuf = load->pixbuf;
        struct _TileData *data   = load->data;
@@ -169,9 +186,9 @@ static gboolean _load_tile_cb(gpointer _load)
        tile->data = data;
 
        /* Do necessasairy processing */
-       /* TODO: Lock this and move to thread, can remove self from _load then */
+       /* TODO: Lock this and move to thread, can remove elev from _load then */
        if (LOAD_BIL)
-               gis_viewer_set_height_func(self->viewer, tile, _height_func, self, TRUE);
+               gis_viewer_set_height_func(elev->viewer, tile, _height_func, elev, TRUE);
 
        /* Cleanup unneeded things */
        if (!LOAD_BIL)
@@ -181,20 +198,29 @@ static gboolean _load_tile_cb(gpointer _load)
 
        return FALSE;
 }
-static void _load_tile(GisTile *tile, gpointer _self)
+static void _load_tile(GisTile *tile, gpointer _elev)
 {
-       GisPluginElev *self = _self;
+       GisPluginElev *elev = _elev;
 
        struct _LoadTileData *load = g_new0(struct _LoadTileData, 1);
-       load->path = gis_wms_make_local(self->wms, tile);
+       load->path = gis_wms_fetch(elev->wms, tile, GIS_ONCE, NULL, NULL);
        g_debug("GisPluginElev: _load_tile: %s", load->path);
-       load->self = self;
+       load->elev = elev;
        load->tile = tile;
        load->data = g_new0(struct _TileData, 1);
-       if (LOAD_BIL || LOAD_OPENGL)
+       if (LOAD_BIL || LOAD_OPENGL) {
                load->data->bil = _load_bil(load->path);
-       if (LOAD_OPENGL)
+               if (!load->data->bil) {
+                       g_remove(load->path);
+                       g_free(load->data);
+                       g_free(load->path);
+                       g_free(load);
+                       return;
+               }
+       }
+       if (LOAD_OPENGL) {
                load->pixbuf = _load_pixbuf(load->data->bil);
+       }
 
        g_idle_add_full(G_PRIORITY_LOW, _load_tile_cb, load, NULL);
 }
@@ -209,26 +235,27 @@ static gboolean _free_tile_cb(gpointer _data)
        g_free(data);
        return FALSE;
 }
-static void _free_tile(GisTile *tile, gpointer _self)
+static void _free_tile(GisTile *tile, gpointer _elev)
 {
-       GisPluginElev *self = _self;
+       GisPluginElev *elev = _elev;
        g_debug("GisPluginElev: _free_tile: %p", tile->data);
-       g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
+       if (tile->data)
+               g_idle_add_full(G_PRIORITY_LOW, _free_tile_cb, tile->data, NULL);
 }
 
-static gpointer _update_tiles(gpointer _self)
+static gpointer _update_tiles(gpointer _elev)
 {
-       GisPluginElev *self = _self;
-       g_mutex_lock(self->mutex);
-       gdouble lat, lon, elev;
-       gis_viewer_get_location(self->viewer, &lat, &lon, &elev);
-       gis_tile_update(self->tiles,
+       GisPluginElev *elev = _elev;
+       g_mutex_lock(elev->mutex);
+       gdouble lat, lon, elevation;
+       gis_viewer_get_location(elev->viewer, &lat, &lon, &elevation);
+       gis_tile_update(elev->tiles,
                        MAX_RESOLUTION, TILE_WIDTH, TILE_WIDTH,
-                       lat, lon, elev,
-                       _load_tile, self);
-       gis_tile_gc(self->tiles, time(NULL)-10,
-                       _free_tile, self);
-       g_mutex_unlock(self->mutex);
+                       lat, lon, elevation,
+                       _load_tile, elev);
+       gis_tile_gc(elev->tiles, time(NULL)-10,
+                       _free_tile, elev);
+       g_mutex_unlock(elev->mutex);
        return NULL;
 }
 
@@ -236,43 +263,41 @@ static gpointer _update_tiles(gpointer _self)
  * Callbacks *
  *************/
 static void _on_location_changed(GisViewer *viewer,
-               gdouble lat, gdouble lon, gdouble elev, GisPluginElev *self)
+               gdouble lat, gdouble lon, gdouble elevation, GisPluginElev *elev)
 {
-       g_thread_create(_update_tiles, self, FALSE, NULL);
-}
-
-static gpointer _expose(GisCallback *callback, gpointer _self)
-{
-       GisPluginElev *self = GIS_PLUGIN_ELEV(_self);
-       g_debug("GisPluginElev: expose tiles=%p data=%p",
-               self->tiles, self->tiles->data);
-       if (LOAD_OPENGL)
-               gis_viewer_render_tiles(self->viewer, self->tiles);
-       return NULL;
+       g_thread_create(_update_tiles, elev, FALSE, NULL);
 }
 
 /***********
  * Methods *
  ***********/
+/**
+ * gis_plugin_elev_new:
+ * @viewer: the #GisViewer to use for drawing
+ *
+ * Create a new instance of the elevation plugin.
+ *
+ * Returns: the new #GisPluginElev
+ */
 GisPluginElev *gis_plugin_elev_new(GisViewer *viewer)
 {
        g_debug("GisPluginElev: new");
-       GisPluginElev *self = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
-       self->viewer = g_object_ref(viewer);
+       GisPluginElev *elev = g_object_new(GIS_TYPE_PLUGIN_ELEV, NULL);
+       elev->viewer = g_object_ref(viewer);
 
        /* Load initial tiles */
-       _load_tile(self->tiles, self);
-       g_thread_create(_update_tiles, self, FALSE, NULL);
+       _load_tile(elev->tiles, elev);
+       g_thread_create(_update_tiles, elev, FALSE, NULL);
 
        /* Connect signals */
-       self->sigid = g_signal_connect(self->viewer, "location-changed",
-                       G_CALLBACK(_on_location_changed), self);
+       elev->sigid = g_signal_connect(elev->viewer, "location-changed",
+                       G_CALLBACK(_on_location_changed), elev);
 
        /* Add renderers */
-       GisCallback *callback = gis_callback_new(_expose, self);
-       gis_viewer_add(viewer, GIS_OBJECT(callback), GIS_LEVEL_WORLD, 0);
+       if (LOAD_OPENGL)
+               gis_viewer_add(viewer, GIS_OBJECT(elev->tiles), GIS_LEVEL_WORLD, 0);
 
-       return self;
+       return elev;
 }
 
 
@@ -290,38 +315,38 @@ static void gis_plugin_elev_plugin_init(GisPluginInterface *iface)
        /* Add methods to the interface */
 }
 /* Class/Object init */
-static void gis_plugin_elev_init(GisPluginElev *self)
+static void gis_plugin_elev_init(GisPluginElev *elev)
 {
        g_debug("GisPluginElev: init");
        /* Set defaults */
-       self->mutex = g_mutex_new();
-       self->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
-       self->wms   = gis_wms_new(
-               "http://www.nasa.network.com/srtm", "srtm30", "application/bil",
+       elev->mutex = g_mutex_new();
+       elev->tiles = gis_tile_new(NULL, NORTH, SOUTH, EAST, WEST);
+       elev->wms   = gis_wms_new(
+               "http://www.nasa.network.com/elev", "srtm30", "application/bil",
                "srtm/", "bil", TILE_WIDTH, TILE_HEIGHT);
 }
 static void gis_plugin_elev_dispose(GObject *gobject)
 {
        g_debug("GisPluginElev: dispose");
-       GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
+       GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
        /* Drop references */
        if (LOAD_BIL)
-               gis_viewer_clear_height_func(self->viewer);
-       if (self->viewer) {
-               g_signal_handler_disconnect(self->viewer, self->sigid);
-               g_object_unref(self->viewer);
-               self->viewer = NULL;
+               gis_viewer_clear_height_func(elev->viewer);
+       if (elev->viewer) {
+               g_signal_handler_disconnect(elev->viewer, elev->sigid);
+               g_object_unref(elev->viewer);
+               elev->viewer = NULL;
        }
        G_OBJECT_CLASS(gis_plugin_elev_parent_class)->dispose(gobject);
 }
 static void gis_plugin_elev_finalize(GObject *gobject)
 {
        g_debug("GisPluginElev: finalize");
-       GisPluginElev *self = GIS_PLUGIN_ELEV(gobject);
+       GisPluginElev *elev = GIS_PLUGIN_ELEV(gobject);
        /* Free data */
-       gis_tile_free(self->tiles, _free_tile, self);
-       gis_wms_free(self->wms);
-       g_mutex_free(self->mutex);
+       gis_tile_free(elev->tiles, _free_tile, elev);
+       gis_wms_free(elev->wms);
+       g_mutex_free(elev->mutex);
        G_OBJECT_CLASS(gis_plugin_elev_parent_class)->finalize(gobject);
 
 }