]> Pileus Git - grits/blob - src/data/gis-wms.c
9d388625556a26d0028705cd73b8746fb31d6ae6
[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  * 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 #include "gis-http.h"
67
68 static gchar *_make_uri(GisWms *wms, GisTile *tile)
69 {
70         return g_strdup_printf(
71                 "%s?"
72                 "SERVICE=WMS&"
73                 "VERSION=1.1.0&"
74                 "REQUEST=GetMap&"
75                 "LAYERS=%s&"
76                 "STYLES=&"
77                 "SRS=EPSG:4326&"
78                 "FORMAT=%s&"
79                 "WIDTH=%d&"
80                 "HEIGHT=%d&"
81                 "BBOX=%f,%f,%f,%f",
82                 wms->uri_prefix,
83                 wms->uri_layer,
84                 wms->uri_format,
85                 wms->width,
86                 wms->height,
87                 tile->edge.w,
88                 tile->edge.s,
89                 tile->edge.e,
90                 tile->edge.n);
91 }
92
93 gchar *gis_wms_fetch(GisWms *self, GisTile *tile, GisCacheType mode,
94                 GisChunkCallback callback, gpointer user_data)
95 {
96         gchar *uri   = _make_uri(self, tile);
97         gchar *tilep = gis_tile_get_path(tile);
98         gchar *local = g_strdup_printf("%s%s", tilep, self->extension);
99         mode = GIS_ONCE;
100         gchar *path  = gis_http_fetch(self->http, uri, local,
101                         mode, callback, user_data);
102         g_free(uri);
103         g_free(tilep);
104         g_free(local);
105         return path;
106 }
107
108 GisWms *gis_wms_new(
109         const gchar *uri_prefix, const gchar *uri_layer,
110         const gchar *uri_format, const gchar *prefix,
111         const gchar *extension, gint width, gint height)
112 {
113         g_debug("GisWms: new - %s", uri_prefix);
114         GisWms *self = g_new0(GisWms, 1);
115         self->http         = gis_http_new(prefix);
116         self->uri_prefix   = g_strdup(uri_prefix);
117         self->uri_layer    = g_strdup(uri_layer);
118         self->uri_format   = g_strdup(uri_format);
119         self->extension    = g_strdup(extension);
120         self->width        = width;
121         self->height       = height;
122         return self;
123 }
124
125 void gis_wms_free(GisWms *self)
126 {
127         g_debug("GisWms: free - %s", self->uri_prefix);
128         gis_http_free(self->http);
129         g_free(self->uri_prefix);
130         g_free(self->uri_layer);
131         g_free(self->uri_format);
132         g_free(self->extension);
133         g_free(self);
134 }