]> Pileus Git - grits/blob - src/data/grits-wms.c
Add cube GtkGL example
[grits] / src / data / grits-wms.c
1 /*
2  * Copyright (C) 2009-2011 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  * SECTION:grits-wms
20  * @short_description: Web Map Service
21  *
22  * Provides an API for accessing image tiles form a Web Map Service (WMS)
23  * server. #GritsWms integrates closely with #GritsTile. The remote server must
24  * support the EPSG:4326 cartographic projection.
25  */
26
27 /*
28  * Example WMS URLS
29  * ----------------
30  *
31  * Metacarte Open Street Map:
32  *   http://labs.metacarta.com/wms/vmap0?
33  *   LAYERS=basic&
34  *   SERVICE=WMS&
35  *   VERSION=1.1.1&
36  *   REQUEST=GetMap&
37  *   STYLES=&
38  *   EXCEPTIONS=application/vnd.ogc.se_inimage&
39  *   FORMAT=image/jpeg&
40  *   SRS=EPSG:4326&
41  *   BBOX=0,-90,180,90&
42  *   WIDTH=256&
43  *   HEIGHT=256
44  *
45  * NASA Blue Marble Next Generation:
46  *   http://www.nasa.network.com/elev?
47  *   SERVICE=WMS&
48  *   VERSION=1.1.0&
49  *   REQUEST=GetMap&
50  *   LAYERS=bmng200406&
51  *   STYLES=&
52  *   SRS=EPSG:4326&
53  *   BBOX=-180,-90,180,90&
54  *   FORMAT=image/jpeg&
55  *   WIDTH=600&
56  *   HEIGHT=300
57  *
58  * NASA Shuttle Radar Topography Mission:
59  *   http://www.nasa.network.com/elev?
60  *   SERVICE=WMS&
61  *   VERSION=1.1.0&
62  *   REQUEST=GetMap&
63  *   LAYERS=srtm30&
64  *   STYLES=&
65  *   SRS=EPSG:4326&
66  *   BBOX=-180,-90,180,90&
67  *   FORMAT=application/bil32&
68  *   WIDTH=600&
69  *   HEIGHT=300
70  */
71
72 #include <config.h>
73 #include <stdio.h>
74 #include <glib.h>
75 #include <locale.h>
76
77 #include "grits-wms.h"
78 #include "grits-http.h"
79
80 static gchar *g_strdup_printf_safe(char *fmt, ...)
81 {
82         va_list ap;
83         va_start(ap, fmt);
84         setlocale(LC_ALL, "POSIX.UTF-8");
85         char *str = g_strdup_vprintf(fmt, ap);
86         setlocale(LC_ALL, "");
87         va_end(ap);
88         return str;
89 }
90
91 static gchar *_make_uri(GritsWms *wms, GritsTile *tile)
92 {
93         return g_strdup_printf_safe(
94                 "%s?"
95                 "SERVICE=WMS&"
96                 "VERSION=1.1.0&"
97                 "REQUEST=GetMap&"
98                 "LAYERS=%s&"
99                 "STYLES=&"
100                 "SRS=EPSG:4326&"
101                 "FORMAT=%s&"
102                 "WIDTH=%d&"
103                 "HEIGHT=%d&"
104                 "BBOX=%f,%f,%f,%f",
105                 wms->uri_prefix,
106                 wms->uri_layer,
107                 wms->uri_format,
108                 wms->width,
109                 wms->height,
110                 tile->edge.w,
111                 tile->edge.s,
112                 tile->edge.e,
113                 tile->edge.n);
114 }
115
116 /**
117  * grits_wms_fetch:
118  * @wms:       the #GritsWms to fetch the data from 
119  * @tile:      a #GritsTile representing the area to be fetched 
120  * @mode:      the update type to use when fetching data
121  * @callback:  callback to call when a chunk of data is received
122  * @user_data: user data to pass to the callback
123  *
124  * Fetch a image coresponding to a #GritsTile from a WMS server. 
125  *
126  * Returns: the path to the local file.
127  */
128 gchar *grits_wms_fetch(GritsWms *wms, GritsTile *tile, GritsCacheType mode,
129                 GritsChunkCallback callback, gpointer user_data)
130 {
131         gchar *uri   = _make_uri(wms, tile);
132         gchar *tilep = grits_tile_get_path(tile);
133         gchar *local = g_strdup_printf("%s%s", tilep, wms->extension);
134         gchar *path  = grits_http_fetch(wms->http, uri, local,
135                         mode, callback, user_data);
136         g_free(uri);
137         g_free(tilep);
138         g_free(local);
139         return path;
140 }
141
142 /**
143  * grits_wms_new:
144  * @uri_prefix: the base URL for the WMS server
145  * @uri_layer:  the layer the images should be fetched from (wms LAYERS)
146  * @uri_format: the format the images should be fetch in (wms FORMAT)
147  * @prefix:     prefix to use for local files
148  * @extension:  file extension for local files, should correspond to @uri_format
149  * @width:      width in pixels for downloaded images (wms WIDTH)
150  * @height:     height in pixels for downloaded images (wms HEIGHT)
151  *
152  * Creates a #GritsWms for some layer on a WMS server. The returned #GritsWms
153  * stores information about the images so it does not need to be entered each
154  * time a images is fetched.
155  *
156  * Returns: the new #GritsWms
157  */
158 GritsWms *grits_wms_new(
159         const gchar *uri_prefix, const gchar *uri_layer,
160         const gchar *uri_format, const gchar *prefix,
161         const gchar *extension, gint width, gint height)
162 {
163         g_debug("GritsWms: new - %s", uri_prefix);
164         GritsWms *wms = g_new0(GritsWms, 1);
165         wms->http       = grits_http_new(prefix);
166         wms->uri_prefix = g_strdup(uri_prefix);
167         wms->uri_layer  = g_strdup(uri_layer);
168         wms->uri_format = g_strdup(uri_format);
169         wms->extension  = g_strdup(extension);
170         wms->width      = width;
171         wms->height     = height;
172         return wms;
173 }
174
175 /**
176  * grits_wms_free:
177  * @wms: the #GritsWms to free
178  *
179  * Free resources used by @wms and cancel any pending requests.
180  */
181 void grits_wms_free(GritsWms *wms)
182 {
183         g_debug("GritsWms: free - %s", wms->uri_prefix);
184         grits_http_free(wms->http);
185         g_free(wms->uri_prefix);
186         g_free(wms->uri_layer);
187         g_free(wms->uri_format);
188         g_free(wms->extension);
189         g_free(wms);
190 }