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