2 * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 * http://www.nasa.network.com/elev?
26 * BBOX=-180,-90,180,90&
31 * http://www.nasa.network.com/elev?
38 * BBOX=-180,-90,180,90&
39 * FORMAT=application/bil32&
50 static gchar *_make_uri(GisWms *wms, GisTile *tile)
52 return g_strdup_printf(
75 void gis_wms_soup_chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _file)
78 if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
80 goffset total = soup_message_headers_get_content_length(message->response_headers);
81 if (fwrite(chunk->data, chunk->length, 1, file) != 1)
82 g_warning("GisWms: soup_chunk_cb - eror writing data");
85 char *gis_wms_make_local(GisWms *self, GisTile *tile)
88 gchar *tile_path = gis_tile_get_path(tile);
89 gchar *path = g_strdup_printf("%s/%s/%s%s%s",
90 g_get_user_cache_dir(), PACKAGE,
91 self->cache_prefix, tile_path, self->cache_ext);
94 /* Return if it already exists */
95 if (g_file_test(path, G_FILE_TEST_EXISTS))
98 /* Open temp file for writing */
99 gchar *tmp_path = g_strconcat(path, ".part", NULL);
100 gchar *dirname = g_path_get_dirname(tmp_path);
101 g_mkdir_with_parents(dirname, 0755);
103 FILE *file = fopen(tmp_path, "a");
106 gchar *uri = _make_uri(self, tile);
107 SoupMessage *message = soup_message_new("GET", uri);
108 g_signal_connect(message, "got-chunk", G_CALLBACK(gis_wms_soup_chunk_cb), file);
109 soup_message_headers_set_range(message->request_headers, ftell(file), -1);
110 int status = soup_session_send_message(self->soup, message);
115 rename(tmp_path, path);
121 gchar *uri_prefix, gchar *uri_layer, gchar *uri_format,
122 gchar *cache_prefix, gchar *cache_ext,
123 gint width, gint height)
125 GisWms *self = g_new0(GisWms, 1);
126 self->uri_prefix = uri_prefix;
127 self->uri_layer = uri_layer;
128 self->uri_format = uri_format;
129 self->cache_prefix = cache_prefix;
130 self->cache_ext = cache_ext;
132 self->height = height;
133 self->soup = soup_session_sync_new();
137 void gis_wms_free(GisWms *self)
139 g_object_unref(self->soup);