]> Pileus Git - grits/blob - src/gis-wms.c
Remove properties from gis-viewer
[grits] / src / gis-wms.c
1 /*
2  * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
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 /**
19  * http://www.nasa.network.com/elev?
20  * SERVICE=WMS&
21  * VERSION=1.1.0&
22  * REQUEST=GetMap&
23  * LAYERS=bmng200406&
24  * STYLES=&
25  * SRS=EPSG:4326&
26  * BBOX=-180,-90,180,90&
27  * FORMAT=image/jpeg&
28  * WIDTH=600&
29  * HEIGHT=300
30  *
31  * http://www.nasa.network.com/elev?
32  * SERVICE=WMS&
33  * VERSION=1.1.0&
34  * REQUEST=GetMap&
35  * LAYERS=srtm30&
36  * STYLES=&
37  * SRS=EPSG:4326&
38  * BBOX=-180,-90,180,90&
39  * FORMAT=application/bil32&
40  * WIDTH=600&
41  * HEIGHT=300
42  */
43
44 #include <config.h>
45 #include <stdio.h>
46 #include <glib.h>
47
48 #include "gis-wms.h"
49
50 static gchar *_make_uri(GisWms *wms, GisTile *tile)
51 {
52         return g_strdup_printf(
53                 "%s?"
54                 "SERVICE=WMS&"
55                 "VERSION=1.1.0&"
56                 "REQUEST=GetMap&"
57                 "LAYERS=%s&"
58                 "STYLES=&"
59                 "SRS=EPSG:4326&"
60                 "FORMAT=%s&"
61                 "WIDTH=%d&"
62                 "HEIGHT=%d&"
63                 "BBOX=%f,%f,%f,%f",
64                 wms->uri_prefix,
65                 wms->uri_layer,
66                 wms->uri_format,
67                 wms->width,
68                 wms->height,
69                 tile->edge.w,
70                 tile->edge.s,
71                 tile->edge.e,
72                 tile->edge.n);
73 }
74
75 void gis_wms_soup_chunk_cb(SoupMessage *message, SoupBuffer *chunk, gpointer _file)
76 {
77         FILE *file = _file;
78         if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code)) {
79                 g_warning("GisWms: soup_chunk_cb - soup failed with %d", message->status_code);
80                 return;
81         }
82         goffset total = soup_message_headers_get_content_length(message->response_headers);
83         if (fwrite(chunk->data, chunk->length, 1, file) != 1)
84                 g_warning("GisWms: soup_chunk_cb - eror writing data");
85 }
86
87 char *gis_wms_make_local(GisWms *self, GisTile *tile)
88 {
89         /* Get file path */
90         gchar *tile_path = gis_tile_get_path(tile);
91         gchar *path = g_strdup_printf("%s/%s/%s%s%s",
92                 g_get_user_cache_dir(), PACKAGE,
93                 self->cache_prefix, tile_path, self->cache_ext);
94         g_free(tile_path);
95
96         /* Return if it already exists */
97         if (g_file_test(path, G_FILE_TEST_EXISTS))
98                 return path;
99
100         /* Open temp file for writing */
101         gchar *tmp_path = g_strconcat(path, ".part", NULL);
102         gchar *dirname = g_path_get_dirname(tmp_path);
103         g_mkdir_with_parents(dirname, 0755);
104         g_free(dirname);
105         FILE *file = fopen(tmp_path, "a");
106
107         /* Download file */
108         gchar *uri = _make_uri(self, tile);
109         g_debug("GisWms: make_local - fetching %s", uri);
110         SoupMessage *message = soup_message_new("GET", uri);
111         g_signal_connect(message, "got-chunk", G_CALLBACK(gis_wms_soup_chunk_cb), file);
112         soup_message_headers_set_range(message->request_headers, ftell(file), -1);
113         int status = soup_session_send_message(self->soup, message);
114         if (!SOUP_STATUS_IS_SUCCESSFUL(message->status_code))
115                 g_warning("GisWms: make_local - soup failed with %d", message->status_code);
116         g_free(uri);
117
118         /* Clean up */
119         fclose(file);
120         rename(tmp_path, path);
121         g_free(tmp_path);
122         return path;
123 }
124
125 GisWms *gis_wms_new(
126         gchar *uri_prefix, gchar *uri_layer, gchar *uri_format,
127         gchar *cache_prefix, gchar *cache_ext,
128         gint width, gint height)
129 {
130         GisWms *self = g_new0(GisWms, 1);
131         self->uri_prefix   = uri_prefix;
132         self->uri_layer    = uri_layer;
133         self->uri_format   = uri_format;
134         self->cache_prefix = cache_prefix;
135         self->cache_ext    = cache_ext;
136         self->width        = width;
137         self->height       = height;
138         self->soup         = soup_session_sync_new();
139         return self;
140 }
141
142 void gis_wms_free(GisWms *self)
143 {
144         g_object_unref(self->soup);
145         g_free(self);
146 }