]> Pileus Git - grits/blobdiff - src/data/gis-wms.c
Document GisWms
[grits] / src / data / gis-wms.c
index defedb3cd7b516dad3f440c37940df77fdbec004..35eb988d13243cd7b56ed6608e778acf0c5e1d0c 100644 (file)
  */
 
 /**
- * Metacarte
- * ---------
- * http://labs.metacarta.com/wms/vmap0?
- * LAYERS=basic&
- * SERVICE=WMS&
- * VERSION=1.1.1&
- * REQUEST=GetMap&
- * STYLES=&
- * EXCEPTIONS=application/vnd.ogc.se_inimage&
- * FORMAT=image/jpeg&
- * SRS=EPSG:4326&
- * BBOX=0,-90,180,90&
- * WIDTH=256&
- * HEIGHT=256
+ * SECTION:gis-wms
+ * @short_description: Web Map Service
+ *
+ * Provides an API for accessing image tiles form a Web Map Service (WMS)
+ * server. #GisWms integrates closely with #GisTile. The remote server must
+ * support the EPSG:4326 cartographic projection.
  */
 
-/**
- * http://www.nasa.network.com/elev?
- * SERVICE=WMS&
- * VERSION=1.1.0&
- * REQUEST=GetMap&
- * LAYERS=bmng200406&
- * STYLES=&
- * SRS=EPSG:4326&
- * BBOX=-180,-90,180,90&
- * FORMAT=image/jpeg&
- * WIDTH=600&
- * HEIGHT=300
+/*
+ * Example WMS URLS
+ * ----------------
  *
- * http://www.nasa.network.com/elev?
- * SERVICE=WMS&
- * VERSION=1.1.0&
- * REQUEST=GetMap&
- * LAYERS=srtm30&
- * STYLES=&
- * SRS=EPSG:4326&
- * BBOX=-180,-90,180,90&
- * FORMAT=application/bil32&
- * WIDTH=600&
- * HEIGHT=300
+ * Metacarte Open Street Map:
+ *   http://labs.metacarta.com/wms/vmap0?
+ *   LAYERS=basic&
+ *   SERVICE=WMS&
+ *   VERSION=1.1.1&
+ *   REQUEST=GetMap&
+ *   STYLES=&
+ *   EXCEPTIONS=application/vnd.ogc.se_inimage&
+ *   FORMAT=image/jpeg&
+ *   SRS=EPSG:4326&
+ *   BBOX=0,-90,180,90&
+ *   WIDTH=256&
+ *   HEIGHT=256
+ *
+ * NASA Blue Marble Next Generation:
+ *   http://www.nasa.network.com/elev?
+ *   SERVICE=WMS&
+ *   VERSION=1.1.0&
+ *   REQUEST=GetMap&
+ *   LAYERS=bmng200406&
+ *   STYLES=&
+ *   SRS=EPSG:4326&
+ *   BBOX=-180,-90,180,90&
+ *   FORMAT=image/jpeg&
+ *   WIDTH=600&
+ *   HEIGHT=300
+ *
+ * NASA Shuttle Radar Topography Mission:
+ *   http://www.nasa.network.com/elev?
+ *   SERVICE=WMS&
+ *   VERSION=1.1.0&
+ *   REQUEST=GetMap&
+ *   LAYERS=srtm30&
+ *   STYLES=&
+ *   SRS=EPSG:4326&
+ *   BBOX=-180,-90,180,90&
+ *   FORMAT=application/bil32&
+ *   WIDTH=600&
+ *   HEIGHT=300
  */
 
 #include <config.h>
@@ -63,6 +74,7 @@
 #include <glib.h>
 
 #include "gis-wms.h"
+#include "gis-http.h"
 
 static gchar *_make_uri(GisWms *wms, GisTile *tile)
 {
@@ -89,75 +101,79 @@ static gchar *_make_uri(GisWms *wms, GisTile *tile)
                tile->edge.n);
 }
 
-static void _soup_chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _file)
-{
-       FILE *file = _file;
-       if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
-               g_warning("GisWms: soup_chunk_cb - soup failed with %d", message->status_code);
-               return;
-       }
-       goffset total = soup_message_headers_get_content_length(message->response_headers);
-       if (fwrite(chunk->data, chunk->length, 1, file) != 1)
-               g_warning("GisWms: soup_chunk_cb - eror writing data");
-}
-
-char *gis_wms_make_local(GisWms *self, GisTile *tile)
+/**
+ * gis_wms_fetch:
+ * @wms:       the #GisWms to fetch the data from 
+ * @tile:      a #GisTile representing the area to be fetched 
+ * @mode:      the update type to use when fetching data
+ * @callback:  callback to call when a chunk of data is received
+ * @user_data: user data to pass to the callback
+ *
+ * Fetch a image coresponding to a #GisTile from a WMS server. 
+ *
+ * Returns: the path to the local file.
+ */
+gchar *gis_wms_fetch(GisWms *wms, GisTile *tile, GisCacheType mode,
+               GisChunkCallback callback, gpointer user_data)
 {
-       /* Get file path */
-       gchar *tile_path = gis_tile_get_path(tile);
-       gchar *path = g_strdup_printf("%s/%s/%s%s%s",
-               g_get_user_cache_dir(), PACKAGE,
-               self->cache_prefix, tile_path, self->cache_ext);
-       g_free(tile_path);
-
-       /* Return if it already exists */
-       if (g_file_test(path, G_FILE_TEST_EXISTS))
-               return path;
-
-       /* Open temp file for writing */
-       gchar *tmp_path = g_strconcat(path, ".part", NULL);
-       gchar *dirname = g_path_get_dirname(tmp_path);
-       g_mkdir_with_parents(dirname, 0755);
-       g_free(dirname);
-       FILE *file = fopen(tmp_path, "a");
-
-       /* Download file */
-       gchar *uri = _make_uri(self, tile);
-       g_debug("GisWms: make_local - fetching %s", uri);
-       SoupMessage *message = soup_message_new("GET", uri);
-       g_signal_connect(message, "got-chunk", G_CALLBACK(_soup_chunk_cb), file);
-       soup_message_headers_set_range(message->request_headers, ftell(file), -1);
-       int status = soup_session_send_message(self->soup, message);
-       if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
-               g_warning("GisWms: make_local - soup failed with %d", message->status_code);
+       gchar *uri   = _make_uri(wms, tile);
+       gchar *tilep = gis_tile_get_path(tile);
+       gchar *local = g_strdup_printf("%s%s", tilep, wms->extension);
+       mode = GIS_ONCE;
+       gchar *path  = gis_http_fetch(wms->http, uri, local,
+                       mode, callback, user_data);
        g_free(uri);
-
-       /* Clean up */
-       fclose(file);
-       rename(tmp_path, path);
-       g_free(tmp_path);
+       g_free(tilep);
+       g_free(local);
        return path;
 }
 
+/**
+ * gis_wms_new:
+ * @uri_prefix: the base URL for the WMS server
+ * @uri_layer:  the layer the images should be fetched from (wms LAYERS)
+ * @uri_format: the format the images should be fetch in (wms FORMAT)
+ * @prefix:     prefix to use for local files
+ * @extension:  file extension for local files, should correspond to @uri_format
+ * @width:      width in pixels for downloaded images (wms WIDTH)
+ * @height:     height in pixels for downloaded images (wms HEIGHT)
+ *
+ * Creates a #GisWms for some layer on a WMS server. The returned #GisWms
+ * stores information about the images so it does not need to be entered each
+ * time a images is fetched.
+ *
+ * Returns: the new #GisWms
+ */
 GisWms *gis_wms_new(
-       gchar *uri_prefix, gchar *uri_layer, gchar *uri_format,
-       gchar *cache_prefix, gchar *cache_ext,
-       gint width, gint height)
+       const gchar *uri_prefix, const gchar *uri_layer,
+       const gchar *uri_format, const gchar *prefix,
+       const gchar *extension, gint width, gint height)
 {
-       GisWms *self = g_new0(GisWms, 1);
-       self->uri_prefix   = uri_prefix;
-       self->uri_layer    = uri_layer;
-       self->uri_format   = uri_format;
-       self->cache_prefix = cache_prefix;
-       self->cache_ext    = cache_ext;
-       self->width        = width;
-       self->height       = height;
-       self->soup         = soup_session_sync_new();
-       return self;
+       g_debug("GisWms: new - %s", uri_prefix);
+       GisWms *wms = g_new0(GisWms, 1);
+       wms->http         = gis_http_new(prefix);
+       wms->uri_prefix   = g_strdup(uri_prefix);
+       wms->uri_layer    = g_strdup(uri_layer);
+       wms->uri_format   = g_strdup(uri_format);
+       wms->extension    = g_strdup(extension);
+       wms->width        = width;
+       wms->height       = height;
+       return wms;
 }
 
-void gis_wms_free(GisWms *self)
+/**
+ * gis_wms_free:
+ * @wms: the #GisWms to free
+ *
+ * Free resources used by @wms and cancel any pending requests.
+ */
+void gis_wms_free(GisWms *wms)
 {
-       g_object_unref(self->soup);
-       g_free(self);
+       g_debug("GisWms: free - %s", wms->uri_prefix);
+       gis_http_free(wms->http);
+       g_free(wms->uri_prefix);
+       g_free(wms->uri_layer);
+       g_free(wms->uri_format);
+       g_free(wms->extension);
+       g_free(wms);
 }