1 : /******************************************************************************
2 : * $Id: wmsdriver.h 23192 2011-10-06 20:31:54Z rouault $
3 : *
4 : * Project: WMS Client Driver
5 : * Purpose: Implementation of Dataset and RasterBand classes for WMS
6 : * and other similar services.
7 : * Author: Adam Nowacki, nowak@xpam.de
8 : *
9 : ******************************************************************************
10 : * Copyright (c) 2007, Adam Nowacki
11 : *
12 : * Permission is hereby granted, free of charge, to any person obtaining a
13 : * copy of this software and associated documentation files (the "Software"),
14 : * to deal in the Software without restriction, including without limitation
15 : * the rights to use, copy, modify, merge, publish, distribute, sublicense,
16 : * and/or sell copies of the Software, and to permit persons to whom the
17 : * Software is furnished to do so, subject to the following conditions:
18 : *
19 : * The above copyright notice and this permission notice shall be included
20 : * in all copies or substantial portions of the Software.
21 : *
22 : * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
23 : * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 : * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
25 : * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26 : * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
27 : * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
28 : * DEALINGS IN THE SOFTWARE.
29 : ****************************************************************************/
30 :
31 : class GDALWMSDataset;
32 : class GDALWMSRasterBand;
33 :
34 : CPLString MD5String(const char *s);
35 : CPLString ProjToWKT(const CPLString &proj);
36 : void URLAppend(CPLString *url, const char *s);
37 : void URLAppendF(CPLString *url, const char *s, ...);
38 : void URLAppend(CPLString *url, const CPLString &s);
39 : CPLString BufferToVSIFile(GByte *buffer, size_t size);
40 : CPLErr MakeDirs(const char *path);
41 :
42 :
43 : int StrToBool(const char *p);
44 : int URLSearchAndReplace (CPLString *base, const char *search, const char *fmt, ...);
45 : /* Convert a.b.c.d to a * 0x1000000 + b * 0x10000 + c * 0x100 + d */
46 : int VersionStringToInt(const char *version);
47 :
48 :
49 : class GDALWMSImageRequestInfo {
50 : public:
51 : double m_x0, m_y0;
52 : double m_x1, m_y1;
53 : int m_sx, m_sy;
54 : };
55 :
56 : class GDALWMSDataWindow {
57 : public:
58 : double m_x0, m_y0;
59 : double m_x1, m_y1;
60 : int m_sx, m_sy;
61 : int m_tx, m_ty, m_tlevel;
62 : enum { BOTTOM = -1, DEFAULT = 0, TOP = 1 } m_y_origin;
63 :
64 20 : GDALWMSDataWindow() : m_x0(-180), m_y0(90), m_x1(180), m_y1(-90),
65 : m_sx(-1), m_sy(-1), m_tx(0), m_ty(0),
66 20 : m_tlevel(-1), m_y_origin(DEFAULT) {}
67 : };
68 :
69 : class GDALWMSTiledImageRequestInfo {
70 : public:
71 : int m_x, m_y;
72 : int m_level;
73 : };
74 :
75 : class GDALWMSRasterIOHint {
76 : public:
77 : int m_x0, m_y0;
78 : int m_sx, m_sy;
79 : int m_overview;
80 : bool m_valid;
81 : };
82 :
83 : class GDALWMSMiniDriverCapabilities {
84 : public:
85 : /* Version N capabilities require all version N and earlier variables to be set to correct values */
86 : int m_capabilities_version;
87 :
88 : /* Version 1 capabilities */
89 : int m_has_image_request; // 1 if ImageRequest method is implemented
90 : int m_has_tiled_image_requeset; // 1 if TiledImageRequest method is implemented
91 : int m_has_arb_overviews; // 1 if ImageRequest method supports arbitrary overviews / resolutions
92 : int m_max_overview_count; // Maximum number of overviews supported if known, -1 otherwise
93 : };
94 :
95 : /* All data returned by mini-driver as pointer should remain valid for mini-driver lifetime
96 : and should be freed by mini-driver destructor unless otherwise specified. */
97 : class GDALWMSMiniDriver {
98 : friend class GDALWMSDataset;
99 : public:
100 : GDALWMSMiniDriver();
101 : virtual ~GDALWMSMiniDriver();
102 :
103 : public:
104 : /* Read mini-driver specific configuration. */
105 : virtual CPLErr Initialize(CPLXMLNode *config);
106 :
107 : public:
108 : virtual void GetCapabilities(GDALWMSMiniDriverCapabilities *caps);
109 : virtual void ImageRequest(CPLString *url, const GDALWMSImageRequestInfo &iri);
110 : virtual void TiledImageRequest(CPLString *url, const GDALWMSImageRequestInfo &iri, const GDALWMSTiledImageRequestInfo &tiri);
111 :
112 : /* Return data projection in WKT format, NULL or empty string if unknown */
113 : virtual const char *GetProjectionInWKT();
114 :
115 : protected:
116 : GDALWMSDataset *m_parent_dataset;
117 : };
118 :
119 : class GDALWMSMiniDriverFactory {
120 : public:
121 : GDALWMSMiniDriverFactory();
122 : virtual ~GDALWMSMiniDriverFactory();
123 :
124 : public:
125 : virtual GDALWMSMiniDriver* New() = 0;
126 : virtual void Delete(GDALWMSMiniDriver *instance) = 0;
127 :
128 : public:
129 29 : const CPLString &GetName() {
130 29 : return m_name;
131 : }
132 :
133 : protected:
134 : CPLString m_name;
135 : };
136 :
137 : class GDALWMSMiniDriverManager {
138 : public:
139 : GDALWMSMiniDriverManager();
140 : ~GDALWMSMiniDriverManager();
141 :
142 : public:
143 : void Register(GDALWMSMiniDriverFactory *mdf);
144 : GDALWMSMiniDriverFactory *Find(const CPLString &name);
145 :
146 : protected:
147 : std::list<GDALWMSMiniDriverFactory *> m_mdfs;
148 : };
149 :
150 : #define H_GDALWMSMiniDriverFactory(name) \
151 : class GDALWMSMiniDriverFactory_##name : public GDALWMSMiniDriverFactory { \
152 : public: \
153 : GDALWMSMiniDriverFactory_##name(); \
154 : virtual ~GDALWMSMiniDriverFactory_##name(); \
155 : \
156 : public: \
157 : virtual GDALWMSMiniDriver* New(); \
158 : virtual void Delete(GDALWMSMiniDriver *instance); \
159 : };
160 :
161 : #define CPP_GDALWMSMiniDriverFactory(name) \
162 : GDALWMSMiniDriverFactory_##name::GDALWMSMiniDriverFactory_##name() { \
163 : m_name = #name;\
164 : } \
165 : \
166 : GDALWMSMiniDriverFactory_##name::~GDALWMSMiniDriverFactory_##name() { \
167 : } \
168 : \
169 : GDALWMSMiniDriver* GDALWMSMiniDriverFactory_##name::New() { \
170 : return new GDALWMSMiniDriver_##name(); \
171 : } \
172 : \
173 : void GDALWMSMiniDriverFactory_##name::Delete(GDALWMSMiniDriver *instance) { \
174 : delete instance; \
175 : }
176 :
177 : class GDALWMSCache {
178 : public:
179 : GDALWMSCache();
180 : ~GDALWMSCache();
181 :
182 : public:
183 : CPLErr Initialize(CPLXMLNode *config);
184 : CPLErr Write(const char *key, const CPLString &file_name);
185 : CPLErr Read(const char *key, CPLString *file_name);
186 :
187 : protected:
188 : CPLString KeyToCacheFile(const char *key);
189 :
190 : protected:
191 : CPLString m_cache_path;
192 : CPLString m_postfix;
193 : int m_cache_depth;
194 : };
195 :
196 : class GDALWMSDataset : public GDALPamDataset {
197 : friend class GDALWMSRasterBand;
198 :
199 : public:
200 : GDALWMSDataset();
201 : virtual ~GDALWMSDataset();
202 :
203 : virtual const char *GetProjectionRef();
204 : virtual CPLErr SetProjection(const char *proj);
205 : virtual CPLErr GetGeoTransform(double *gt);
206 : virtual CPLErr SetGeoTransform(double *gt);
207 : virtual CPLErr AdviseRead(int x0, int y0, int sx, int sy, int bsx, int bsy, GDALDataType bdt, int band_count, int *band_map, char **options);
208 : virtual const char *GetMetadataItem( const char * pszName,
209 : const char * pszDomain = "" );
210 :
211 : const GDALWMSDataWindow *WMSGetDataWindow() const;
212 : void WMSSetClamp(bool flag);
213 : void WMSSetOverviewCount(int count);
214 : void WMSSetBlockSize(int x, int y);
215 : void WMSSetRasterSize(int x, int y);
216 : void WMSSetDataType(GDALDataType type);
217 : void WMSSetDataWindow(GDALWMSDataWindow &window);
218 : void WMSSetBandsCount(int count);
219 4 : void mSetBand(int i, GDALRasterBand *band) { SetBand(i,band); };
220 46 : GDALWMSRasterBand *mGetBand(int i) { return reinterpret_cast<GDALWMSRasterBand *>(GetRasterBand(i)); };
221 :
222 :
223 : void WMSSetDefaultDataWindowCoordinates(double x0, double y0, double x1, double y1);
224 : void WMSSetDefaultTileLevel(int tlevel);
225 : void WMSSetDefaultTileCount(int tilecountx, int tilecounty);
226 : void WMSSetDefaultBlockSize(int x, int y);
227 : void WMSSetDefaultOverviewCount(int overview_count);
228 : void WMSSetNeedsDataWindow(int flag);
229 :
230 : static GDALDataset* Open(GDALOpenInfo *poOpenInfo);
231 : static int Identify(GDALOpenInfo *poOpenInfo);
232 : static GDALDataset *CreateCopy( const char * pszFilename,
233 : GDALDataset *poSrcDS,
234 : int bStrict, char ** papszOptions,
235 : GDALProgressFunc pfnProgress,
236 : void * pProgressData );
237 :
238 : protected:
239 : virtual CPLErr IRasterIO(GDALRWFlag rw, int x0, int y0, int sx, int sy, void *buffer, int bsx, int bsy, GDALDataType bdt, int band_count, int *band_map, int pixel_space, int line_space, int band_space);
240 : CPLErr Initialize(CPLXMLNode *config);
241 :
242 : GDALWMSDataWindow m_data_window;
243 : GDALWMSMiniDriver *m_mini_driver;
244 : GDALWMSMiniDriverCapabilities m_mini_driver_caps;
245 : GDALWMSCache *m_cache;
246 : CPLString m_projection;
247 : int m_overview_count;
248 : GDALDataType m_data_type;
249 : int m_block_size_x, m_block_size_y;
250 : GDALWMSRasterIOHint m_hint;
251 : int m_use_advise_read;
252 : int m_verify_advise_read;
253 : int m_offline_mode;
254 : int m_http_max_conn;
255 : int m_http_timeout;
256 : int m_clamp_requests;
257 : int m_unsafeSsl;
258 : std::vector<int> m_http_zeroblock_codes;
259 : int m_zeroblock_on_serverexceptions;
260 : CPLString m_osUserAgent;
261 : CPLString m_osReferer;
262 :
263 : GDALWMSDataWindow m_default_data_window;
264 : int m_default_block_size_x, m_default_block_size_y;
265 : int m_default_tile_count_x, m_default_tile_count_y;
266 : int m_default_overview_count;
267 :
268 : int m_bNeedsDataWindow;
269 :
270 : CPLString m_osXML;
271 : };
272 :
273 : class GDALWMSRasterBand : public GDALPamRasterBand {
274 : friend class GDALWMSDataset;
275 :
276 : public:
277 : GDALWMSRasterBand(GDALWMSDataset *parent_dataset, int band, double scale);
278 : virtual ~GDALWMSRasterBand();
279 : void AddOverview(double scale);
280 : virtual CPLErr AdviseRead(int x0, int y0, int sx, int sy, int bsx, int bsy, GDALDataType bdt, char **options);
281 :
282 : virtual GDALColorInterp GetColorInterpretation();
283 : virtual CPLErr SetColorInterpretation( GDALColorInterp );
284 : virtual CPLErr IReadBlock(int x, int y, void *buffer);
285 : virtual CPLErr IRasterIO(GDALRWFlag rw, int x0, int y0, int sx, int sy, void *buffer, int bsx, int bsy, GDALDataType bdt, int pixel_space, int line_space);
286 : virtual int HasArbitraryOverviews();
287 : virtual int GetOverviewCount();
288 : virtual GDALRasterBand *GetOverview(int n);
289 :
290 : protected:
291 : CPLErr ReadBlocks(int x, int y, void *buffer, int bx0, int by0, int bx1, int by1, int advise_read);
292 : bool IsBlockInCache(int x, int y);
293 : void AskMiniDriverForBlock(CPLString *url, int x, int y);
294 : CPLErr ReadBlockFromFile(int x, int y, const char *file_name, int to_buffer_band, void *buffer, int advise_read);
295 : CPLErr ZeroBlock(int x, int y, int to_buffer_band, void *buffer);
296 : CPLErr ReportWMSException(const char *file_name);
297 :
298 : protected:
299 : GDALWMSDataset *m_parent_dataset;
300 : double m_scale;
301 : std::vector<GDALWMSRasterBand *> m_overviews;
302 : int m_overview;
303 : GDALColorInterp m_color_interp;
304 : };
305 :
306 : GDALWMSMiniDriverManager *GetGDALWMSMiniDriverManager();
307 : void DestroyWMSMiniDriverManager(void);
|