]> Pileus Git - grits/blob - src/data/gis-wms.c
Document GisWms
[grits] / src / data / 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  * SECTION:gis-wms
20  * @short_description: Web Map Service
21  *
22  * Provides an API for accessing image tiles form a Web Map Service (WMS)
23  * server. #GisWms integrates closely with #GisTile. 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
76 #include "gis-wms.h"
77 #include "gis-http.h"
78
79 static gchar *_make_uri(GisWms *wms, GisTile *tile)
80 {
81         return g_strdup_printf(
82                 "%s?"
83                 "SERVICE=WMS&"
84                 "VERSION=1.1.0&"
85                 "REQUEST=GetMap&"
86                 "LAYERS=%s&"
87                 "STYLES=&"
88                 "SRS=EPSG:4326&"
89                 "FORMAT=%s&"
90                 "WIDTH=%d&"
91                 "HEIGHT=%d&"
92                 "BBOX=%f,%f,%f,%f",
93                 wms->uri_prefix,
94                 wms->uri_layer,
95                 wms->uri_format,
96                 wms->width,
97                 wms->height,
98                 tile->edge.w,
99                 tile->edge.s,
100                 tile->edge.e,
101                 tile->edge.n);
102 }
103
104 /**
105  * gis_wms_fetch:
106  * @wms:       the #GisWms to fetch the data from 
107  * @tile:      a #GisTile representing the area to be fetched 
108  * @mode:      the update type to use when fetching data
109  * @callback:  callback to call when a chunk of data is received
110  * @user_data: user data to pass to the callback
111  *
112  * Fetch a image coresponding to a #GisTile from a WMS server. 
113  *
114  * Returns: the path to the local file.
115  */
116 gchar *gis_wms_fetch(GisWms *wms, GisTile *tile, GisCacheType mode,
117                 GisChunkCallback callback, gpointer user_data)
118 {
119         gchar *uri   = _make_uri(wms, tile);
120         gchar *tilep = gis_tile_get_path(tile);
121         gchar *local = g_strdup_printf("%s%s", tilep, wms->extension);
122         mode = GIS_ONCE;
123         gchar *path  = gis_http_fetch(wms->http, uri, local,
124                         mode, callback, user_data);
125         g_free(uri);
126         g_free(tilep);
127         g_free(local);
128         return path;
129 }
130
131 /**
132  * gis_wms_new:
133  * @uri_prefix: the base URL for the WMS server
134  * @uri_layer:  the layer the images should be fetched from (wms LAYERS)
135  * @uri_format: the format the images should be fetch in (wms FORMAT)
136  * @prefix:     prefix to use for local files
137  * @extension:  file extension for local files, should correspond to @uri_format
138  * @width:      width in pixels for downloaded images (wms WIDTH)
139  * @height:     height in pixels for downloaded images (wms HEIGHT)
140  *
141  * Creates a #GisWms for some layer on a WMS server. The returned #GisWms
142  * stores information about the images so it does not need to be entered each
143  * time a images is fetched.
144  *
145  * Returns: the new #GisWms
146  */
147 GisWms *gis_wms_new(
148         const gchar *uri_prefix, const gchar *uri_layer,
149         const gchar *uri_format, const gchar *prefix,
150         const gchar *extension, gint width, gint height)
151 {
152         g_debug("GisWms: new - %s", uri_prefix);
153         GisWms *wms = g_new0(GisWms, 1);
154         wms->http         = gis_http_new(prefix);
155         wms->uri_prefix   = g_strdup(uri_prefix);
156         wms->uri_layer    = g_strdup(uri_layer);
157         wms->uri_format   = g_strdup(uri_format);
158         wms->extension    = g_strdup(extension);
159         wms->width        = width;
160         wms->height       = height;
161         return wms;
162 }
163
164 /**
165  * gis_wms_free:
166  * @wms: the #GisWms to free
167  *
168  * Free resources used by @wms and cancel any pending requests.
169  */
170 void gis_wms_free(GisWms *wms)
171 {
172         g_debug("GisWms: free - %s", wms->uri_prefix);
173         gis_http_free(wms->http);
174         g_free(wms->uri_prefix);
175         g_free(wms->uri_layer);
176         g_free(wms->uri_format);
177         g_free(wms->extension);
178         g_free(wms);
179 }