1 : /******************************************************************************
2 : * $Id: rasterband.cpp 19089 2010-03-15 13:20:41Z nowakpl $
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 : #include "stdinc.h"
32 :
33 222 : GDALWMSRasterBand::GDALWMSRasterBand(GDALWMSDataset *parent_dataset, int band, double scale) {
34 : // printf("[%p] GDALWMSRasterBand::GDALWMSRasterBand(%p, %d, %f)\n", this, parent_dataset, band, scale);
35 222 : m_parent_dataset = parent_dataset;
36 222 : m_scale = scale;
37 222 : m_overview = -1;
38 222 : m_color_interp = GCI_Undefined;
39 :
40 222 : poDS = parent_dataset;
41 222 : nRasterXSize = static_cast<int>(m_parent_dataset->m_data_window.m_sx * scale + 0.5);
42 222 : nRasterYSize = static_cast<int>(m_parent_dataset->m_data_window.m_sy * scale + 0.5);
43 222 : nBand = band + 1;
44 222 : eDataType = m_parent_dataset->m_data_type;
45 222 : nBlockXSize = m_parent_dataset->m_block_size_x;
46 222 : nBlockYSize = m_parent_dataset->m_block_size_y;
47 222 : }
48 :
49 222 : GDALWMSRasterBand::~GDALWMSRasterBand() {
50 429 : for (std::vector<GDALWMSRasterBand *>::iterator it = m_overviews.begin(); it != m_overviews.end(); ++it) {
51 207 : GDALWMSRasterBand *p = *it;
52 207 : delete p;
53 : }
54 222 : }
55 :
56 3 : CPLErr GDALWMSRasterBand::ReadBlocks(int x, int y, void *buffer, int bx0, int by0, int bx1, int by1, int advise_read) {
57 3 : CPLErr ret = CE_None;
58 : int i;
59 :
60 3 : int max_request_count = (bx1 - bx0 + 1) * (by1 - by0 + 1);
61 3 : int request_count = 0;
62 3 : CPLHTTPRequest *download_requests = NULL;
63 3 : GDALWMSCache *cache = m_parent_dataset->m_cache;
64 : struct BlockXY {
65 : int x, y;
66 3 : } *download_blocks = NULL;
67 3 : if (!m_parent_dataset->m_offline_mode) {
68 3 : download_requests = new CPLHTTPRequest[max_request_count];
69 3 : download_blocks = new BlockXY[max_request_count];
70 : }
71 :
72 3 : char **http_request_opts = NULL;
73 3 : if (m_parent_dataset->m_http_timeout != -1) {
74 3 : CPLString http_request_optstr;
75 3 : http_request_optstr.Printf("TIMEOUT=%d", m_parent_dataset->m_http_timeout);
76 3 : http_request_opts = CSLAddString(http_request_opts, http_request_optstr.c_str());
77 : }
78 :
79 3 : if (m_parent_dataset->m_osUserAgent.size() != 0)
80 : {
81 0 : CPLString osUserAgentOptStr("USERAGENT=");
82 0 : osUserAgentOptStr += m_parent_dataset->m_osUserAgent;
83 0 : http_request_opts = CSLAddString(http_request_opts, osUserAgentOptStr.c_str());
84 : }
85 :
86 6 : for (int iy = by0; iy <= by1; ++iy) {
87 8 : for (int ix = bx0; ix <= bx1; ++ix) {
88 5 : bool need_this_block = false;
89 5 : if (!advise_read) {
90 20 : for (int ib = 1; ib <= m_parent_dataset->nBands; ++ib) {
91 18 : if ((ix == x) && (iy == y) && (ib == nBand)) {
92 3 : need_this_block = true;
93 : } else {
94 12 : GDALWMSRasterBand *band = static_cast<GDALWMSRasterBand *>(m_parent_dataset->GetRasterBand(ib));
95 12 : if (m_overview >= 0) band = static_cast<GDALWMSRasterBand *>(band->GetOverview(m_overview));
96 12 : if (!band->IsBlockInCache(ix, iy)) need_this_block = true;
97 : }
98 : }
99 : } else {
100 0 : need_this_block = true;
101 : }
102 5 : CPLString url;
103 5 : if (need_this_block) {
104 5 : CPLString file_name;
105 5 : AskMiniDriverForBlock(&url, ix, iy);
106 5 : if ((cache != NULL) && (cache->Read(url.c_str(), &file_name) == CE_None)) {
107 2 : if (advise_read) {
108 0 : need_this_block = false;
109 : } else {
110 2 : void *p = 0;
111 2 : if ((ix == x) && (iy == y)) p = buffer;
112 2 : if (ReadBlockFromFile(ix, iy, file_name.c_str(), nBand, p, 0) == CE_None) need_this_block = false;
113 : }
114 5 : }
115 : }
116 5 : if (need_this_block) {
117 3 : if (m_parent_dataset->m_offline_mode) {
118 0 : if (!advise_read) {
119 0 : void *p = 0;
120 0 : if ((ix == x) && (iy == y)) p = buffer;
121 0 : if (ZeroBlock(ix, iy, nBand, p) != CE_None) {
122 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: ZeroBlock failed.");
123 0 : ret = CE_Failure;
124 : }
125 : }
126 : } else {
127 3 : CPLHTTPInitializeRequest(&download_requests[request_count], url.c_str(), http_request_opts);
128 3 : download_blocks[request_count].x = ix;
129 3 : download_blocks[request_count].y = iy;
130 3 : ++request_count;
131 : }
132 : }
133 : }
134 : }
135 3 : if (http_request_opts != NULL) {
136 3 : CSLDestroy(http_request_opts);
137 : }
138 :
139 3 : if (request_count > 0) {
140 2 : char **opts = NULL;
141 2 : CPLString optstr;
142 2 : if (m_parent_dataset->m_http_max_conn != -1) {
143 2 : optstr.Printf("MAXCONN=%d", m_parent_dataset->m_http_max_conn);
144 2 : opts = CSLAddString(opts, optstr.c_str());
145 : }
146 2 : if (CPLHTTPFetchMulti(download_requests, request_count, opts) != CE_None) {
147 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: CPLHTTPFetchMulti failed.");
148 0 : ret = CE_Failure;
149 : }
150 2 : if (opts != NULL) {
151 2 : CSLDestroy(opts);
152 2 : }
153 : }
154 :
155 6 : for (i = 0; i < request_count; ++i) {
156 3 : if (ret == CE_None) {
157 6 : if ((download_requests[i].nStatus == 200) && (download_requests[i].pabyData != NULL) && (download_requests[i].nDataLen > 0)) {
158 3 : CPLString file_name(BufferToVSIFile(download_requests[i].pabyData, download_requests[i].nDataLen));
159 3 : if (file_name.size() > 0) {
160 : /* check for error xml */
161 3 : if (download_requests[i].nDataLen >= 20) {
162 3 : const char *download_data = reinterpret_cast<char *>(download_requests[i].pabyData);
163 3 : if (EQUALN(download_data, "<?xml ", 6)
164 : || EQUALN(download_data, "<!DOCTYPE ", 10)
165 : || EQUALN(download_data, "<ServiceException", 17)) {
166 0 : if (ReportWMSException(file_name.c_str()) != CE_None) {
167 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: The server returned unknown exception.");
168 : }
169 0 : ret = CE_Failure;
170 : }
171 : }
172 3 : if (ret == CE_None) {
173 3 : if (advise_read && !m_parent_dataset->m_verify_advise_read) {
174 0 : if (cache != NULL) {
175 0 : cache->Write(download_requests[i].pszURL, file_name);
176 : }
177 : } else {
178 3 : void *p = 0;
179 3 : if ((download_blocks[i].x == x) && (download_blocks[i].y == y)) p = buffer;
180 3 : if (ReadBlockFromFile(download_blocks[i].x, download_blocks[i].y, file_name.c_str(), nBand, p, advise_read) == CE_None) {
181 3 : if (cache != NULL) {
182 0 : cache->Write(download_requests[i].pszURL, file_name);
183 : }
184 : } else {
185 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: ReadBlockFromFile (%s) failed.",
186 0 : download_requests[i].pszURL);
187 0 : ret = CE_Failure;
188 : }
189 : }
190 : }
191 3 : VSIUnlink(file_name.c_str());
192 3 : }
193 0 : } else if (download_requests[i].nStatus == 204) {
194 0 : if (!advise_read) {
195 0 : void *p = 0;
196 0 : if ((download_blocks[i].x == x) && (download_blocks[i].y == y)) p = buffer;
197 0 : if (ZeroBlock(download_blocks[i].x, download_blocks[i].y, nBand, p) != CE_None) {
198 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: ZeroBlock failed.");
199 0 : ret = CE_Failure;
200 : }
201 : }
202 : } else {
203 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Unable to download block %d, %d.\n URL: %s\n HTTP status code: %d, error: %s.",
204 : download_blocks[i].x, download_blocks[i].y, download_requests[i].pszURL, download_requests[i].nStatus,
205 0 : download_requests[i].pszError ? download_requests[i].pszError : "(null)");
206 0 : ret = CE_Failure;
207 : }
208 : }
209 3 : CPLHTTPCleanupRequest(&download_requests[i]);
210 : }
211 3 : if (!m_parent_dataset->m_offline_mode) {
212 3 : delete[] download_blocks;
213 3 : delete[] download_requests;
214 : }
215 :
216 3 : return ret;
217 : }
218 :
219 3 : CPLErr GDALWMSRasterBand::IReadBlock(int x, int y, void *buffer) {
220 3 : int bx0 = x;
221 3 : int by0 = y;
222 3 : int bx1 = x;
223 3 : int by1 = y;
224 :
225 3 : if ((m_parent_dataset->m_hint.m_valid) && (m_parent_dataset->m_hint.m_overview == m_overview)) {
226 3 : int tbx0 = m_parent_dataset->m_hint.m_x0 / nBlockXSize;
227 3 : int tby0 = m_parent_dataset->m_hint.m_y0 / nBlockYSize;
228 3 : int tbx1 = (m_parent_dataset->m_hint.m_x0 + m_parent_dataset->m_hint.m_sx - 1) / nBlockXSize;
229 3 : int tby1 = (m_parent_dataset->m_hint.m_y0 + m_parent_dataset->m_hint.m_sy - 1) / nBlockYSize;
230 3 : if ((tbx0 <= bx0) && (tby0 <= by0) && (tbx1 >= bx1) && (tby1 >= by1)) {
231 3 : bx0 = tbx0;
232 3 : by0 = tby0;
233 3 : bx1 = tbx1;
234 3 : by1 = tby1;
235 : }
236 : }
237 :
238 3 : return ReadBlocks(x, y, buffer, bx0, by0, bx1, by1, 0);
239 : }
240 :
241 102 : CPLErr GDALWMSRasterBand::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) {
242 : CPLErr ret;
243 :
244 102 : if (rw != GF_Read) return CE_Failure;
245 102 : if (buffer == NULL) return CE_Failure;
246 102 : if ((sx == 0) || (sy == 0) || (bsx == 0) || (bsy == 0)) return CE_None;
247 :
248 102 : m_parent_dataset->m_hint.m_x0 = x0;
249 102 : m_parent_dataset->m_hint.m_y0 = y0;
250 102 : m_parent_dataset->m_hint.m_sx = sx;
251 102 : m_parent_dataset->m_hint.m_sy = sy;
252 102 : m_parent_dataset->m_hint.m_overview = m_overview;
253 102 : m_parent_dataset->m_hint.m_valid = true;
254 102 : ret = GDALRasterBand::IRasterIO(rw, x0, y0, sx, sy, buffer, bsx, bsy, bdt, pixel_space, line_space);
255 102 : m_parent_dataset->m_hint.m_valid = false;
256 :
257 102 : return ret;
258 : }
259 :
260 0 : int GDALWMSRasterBand::HasArbitraryOverviews() {
261 : // return m_parent_dataset->m_mini_driver_caps.m_has_arb_overviews;
262 0 : return 0; // not implemented yet
263 : }
264 :
265 1 : int GDALWMSRasterBand::GetOverviewCount() {
266 1 : return m_overviews.size();
267 : }
268 :
269 24 : GDALRasterBand *GDALWMSRasterBand::GetOverview(int n) {
270 24 : if ((m_overviews.size() > 0) && (static_cast<size_t>(n) < m_overviews.size())) return m_overviews[n];
271 0 : else return NULL;
272 : }
273 :
274 207 : void GDALWMSRasterBand::AddOverview(double scale) {
275 : int i;
276 207 : GDALWMSRasterBand *overview = new GDALWMSRasterBand(m_parent_dataset, nBand - 1, scale);
277 207 : std::vector<GDALWMSRasterBand *>::iterator it = m_overviews.begin();
278 1776 : for (; it != m_overviews.end(); ++it) {
279 1569 : GDALWMSRasterBand *p = *it;
280 1569 : if (p->m_scale < scale) break;
281 : }
282 207 : m_overviews.insert(it, overview);
283 207 : it = m_overviews.begin();
284 1983 : for (i = 0; it != m_overviews.end(); ++it, ++i) {
285 1776 : GDALWMSRasterBand *p = *it;
286 1776 : p->m_overview = i;
287 : }
288 207 : }
289 :
290 24 : bool GDALWMSRasterBand::IsBlockInCache(int x, int y) {
291 24 : bool ret = false;
292 24 : GDALRasterBlock *b = TryGetLockedBlockRef(x, y);
293 24 : if (b != NULL) {
294 0 : ret = true;
295 0 : b->DropLock();
296 : }
297 24 : return ret;
298 : }
299 :
300 5 : void GDALWMSRasterBand::AskMiniDriverForBlock(CPLString *url, int x, int y) {
301 : GDALWMSImageRequestInfo iri;
302 : GDALWMSTiledImageRequestInfo tiri;
303 :
304 5 : int x0 = MAX(0, x * nBlockXSize);
305 5 : int y0 = MAX(0, y * nBlockYSize);
306 5 : int x1 = MAX(0, (x + 1) * nBlockXSize);
307 5 : int y1 = MAX(0, (y + 1) * nBlockYSize);
308 5 : if (m_parent_dataset->m_clamp_requests) {
309 5 : x0 = MIN(x0, nRasterXSize);
310 5 : y0 = MIN(y0, nRasterYSize);
311 5 : x1 = MIN(x1, nRasterXSize);
312 5 : y1 = MIN(y1, nRasterYSize);
313 : }
314 :
315 5 : const double rx = (m_parent_dataset->m_data_window.m_x1 - m_parent_dataset->m_data_window.m_x0) / static_cast<double>(nRasterXSize);
316 5 : const double ry = (m_parent_dataset->m_data_window.m_y1 - m_parent_dataset->m_data_window.m_y0) / static_cast<double>(nRasterYSize);
317 : /* Use different method for x0,y0 and x1,y1 to make sure calculated values are exact for corner requests */
318 5 : iri.m_x0 = x0 * rx + m_parent_dataset->m_data_window.m_x0;
319 5 : iri.m_y0 = y0 * ry + m_parent_dataset->m_data_window.m_y0;
320 5 : iri.m_x1 = m_parent_dataset->m_data_window.m_x1 - (nRasterXSize - x1) * rx;
321 5 : iri.m_y1 = m_parent_dataset->m_data_window.m_y1 - (nRasterYSize - y1) * ry;
322 5 : iri.m_sx = x1 - x0;
323 5 : iri.m_sy = y1 - y0;
324 :
325 5 : int level = m_overview + 1;
326 5 : tiri.m_x = (m_parent_dataset->m_data_window.m_tx >> level) + x;
327 5 : tiri.m_y = (m_parent_dataset->m_data_window.m_ty >> level) + y;
328 5 : tiri.m_level = m_parent_dataset->m_data_window.m_tlevel - level;
329 :
330 5 : m_parent_dataset->m_mini_driver->TiledImageRequest(url, iri, tiri);
331 5 : }
332 :
333 5 : CPLErr GDALWMSRasterBand::ReadBlockFromFile(int x, int y, const char *file_name, int to_buffer_band, void *buffer, int advise_read) {
334 5 : CPLErr ret = CE_None;
335 5 : GDALDataset *ds = 0;
336 5 : GByte *color_table = NULL;
337 : int i;
338 :
339 : //CPLDebug("WMS", "ReadBlockFromFile: to_buffer_band=%d, (x,y)=(%d, %d)", to_buffer_band, x, y);
340 :
341 : /* expected size */
342 5 : const int esx = MIN(MAX(0, (x + 1) * nBlockXSize), nRasterXSize) - MIN(MAX(0, x * nBlockXSize), nRasterXSize);
343 5 : const int esy = MIN(MAX(0, (y + 1) * nBlockYSize), nRasterYSize) - MIN(MAX(0, y * nBlockYSize), nRasterYSize);
344 5 : ds = reinterpret_cast<GDALDataset*>(GDALOpen(file_name, GA_ReadOnly));
345 5 : if (ds != NULL) {
346 5 : int sx = ds->GetRasterXSize();
347 5 : int sy = ds->GetRasterYSize();
348 5 : bool accepted_as_no_alpha = false; // if the request is for 4 bands but the wms returns 3
349 : /* Allow bigger than expected so pre-tiled constant size images work on corners */
350 5 : if ((sx > nBlockXSize) || (sy > nBlockYSize) || (sx < esx) || (sy < esy)) {
351 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Incorrect size %d x %d of downloaded block, expected %d x %d, max %d x %d.",
352 0 : sx, sy, esx, esy, nBlockXSize, nBlockYSize);
353 0 : ret = CE_Failure;
354 : }
355 5 : if (ret == CE_None) {
356 5 : int nDSRasterCount = ds->GetRasterCount();
357 5 : if (nDSRasterCount != m_parent_dataset->nBands) {
358 : /* Maybe its an image with color table */
359 0 : bool accepted_as_ct = false;
360 0 : if ((eDataType == GDT_Byte) && (ds->GetRasterCount() == 1)) {
361 0 : GDALRasterBand *rb = ds->GetRasterBand(1);
362 0 : if (rb->GetRasterDataType() == GDT_Byte) {
363 0 : GDALColorTable *ct = rb->GetColorTable();
364 0 : if (ct != NULL) {
365 0 : accepted_as_ct = true;
366 0 : if (!advise_read) {
367 0 : color_table = new GByte[256 * 4];
368 0 : const int count = MIN(256, ct->GetColorEntryCount());
369 0 : for (i = 0; i < count; ++i) {
370 : GDALColorEntry ce;
371 0 : ct->GetColorEntryAsRGB(i, &ce);
372 0 : color_table[i] = static_cast<GByte>(ce.c1);
373 0 : color_table[i + 256] = static_cast<GByte>(ce.c2);
374 0 : color_table[i + 512] = static_cast<GByte>(ce.c3);
375 0 : color_table[i + 768] = static_cast<GByte>(ce.c4);
376 : }
377 0 : for (i = count; i < 256; ++i) {
378 0 : color_table[i] = 0;
379 0 : color_table[i + 256] = 0;
380 0 : color_table[i + 512] = 0;
381 0 : color_table[i + 768] = 0;
382 : }
383 : }
384 : }
385 : }
386 : }
387 :
388 0 : if (nDSRasterCount == 4 && m_parent_dataset->nBands == 3)
389 : {
390 : /* metacarta TMS service sometimes return a 4 band PNG instead of the expected 3 band... */
391 : }
392 0 : else if (!accepted_as_ct) {
393 0 : if (ds->GetRasterCount()==3 && m_parent_dataset->nBands == 4 && (eDataType == GDT_Byte))
394 : { // WMS returned a file with no alpha so we will fill the alpha band with "opaque"
395 0 : accepted_as_no_alpha = true;
396 : }
397 : else
398 : {
399 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Incorrect bands count %d in downloaded block, expected %d.",
400 0 : nDSRasterCount, m_parent_dataset->nBands);
401 0 : ret = CE_Failure;
402 : }
403 : }
404 : }
405 : }
406 5 : if (!advise_read) {
407 20 : for (int ib = 1; ib <= m_parent_dataset->nBands; ++ib) {
408 15 : if (ret == CE_None) {
409 15 : void *p = NULL;
410 15 : GDALRasterBlock *b = NULL;
411 18 : if ((buffer != NULL) && (ib == to_buffer_band)) {
412 3 : p = buffer;
413 : } else {
414 12 : GDALWMSRasterBand *band = static_cast<GDALWMSRasterBand *>(m_parent_dataset->GetRasterBand(ib));
415 12 : if (m_overview >= 0) band = static_cast<GDALWMSRasterBand *>(band->GetOverview(m_overview));
416 12 : if (!band->IsBlockInCache(x, y)) {
417 12 : b = band->GetLockedBlockRef(x, y, true);
418 12 : if (b != NULL) {
419 12 : p = b->GetDataRef();
420 12 : if (p == NULL) {
421 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: GetDataRef returned NULL.");
422 0 : ret = CE_Failure;
423 : }
424 : }
425 : }
426 : else
427 : {
428 : //CPLDebug("WMS", "Band %d, block (x,y)=(%d, %d) already in cache", band->GetBand(), x, y);
429 : }
430 : }
431 15 : if (p != NULL) {
432 15 : int pixel_space = GDALGetDataTypeSize(eDataType) / 8;
433 15 : int line_space = pixel_space * nBlockXSize;
434 15 : if (color_table == NULL) {
435 15 : if( ib <= ds->GetRasterCount()) {
436 15 : if (ds->RasterIO(GF_Read, 0, 0, sx, sy, p, sx, sy, eDataType, 1, &ib, pixel_space, line_space, 0) != CE_None) {
437 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: RasterIO failed on downloaded block.");
438 0 : ret = CE_Failure;
439 : }
440 : }
441 : else
442 : { // parent expects 4 bands but file only has 3 so generate a all "opaque" 4th band
443 0 : if (accepted_as_no_alpha)
444 : {
445 : // the file had 3 bands and we are reading band 4 (Alpha) so fill with 255 (no alpha)
446 0 : GByte *byte_buffer = reinterpret_cast<GByte *>(p);
447 0 : for (int y = 0; y < sy; ++y) {
448 0 : for (int x = 0; x < sx; ++x) {
449 0 : const int offset = x + y * line_space;
450 0 : byte_buffer[offset] = 255; // fill with opaque
451 : }
452 : }
453 : }
454 : else
455 : { // we should never get here because this case was caught above
456 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Incorrect bands count %d in downloaded block, expected %d.",
457 0 : ds->GetRasterCount(), m_parent_dataset->nBands);
458 0 : ret = CE_Failure;
459 : }
460 : }
461 0 : } else if (ib <= 4) {
462 0 : if (ds->RasterIO(GF_Read, 0, 0, sx, sy, p, sx, sy, eDataType, 1, NULL, pixel_space, line_space, 0) != CE_None) {
463 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: RasterIO failed on downloaded block.");
464 0 : ret = CE_Failure;
465 : }
466 0 : if (ret == CE_None) {
467 0 : GByte *band_color_table = color_table + 256 * (ib - 1);
468 0 : GByte *byte_buffer = reinterpret_cast<GByte *>(p);
469 0 : for (int y = 0; y < sy; ++y) {
470 0 : for (int x = 0; x < sx; ++x) {
471 0 : const int offset = x + y * line_space;
472 0 : byte_buffer[offset] = band_color_table[byte_buffer[offset]];
473 : }
474 : }
475 : }
476 : } else {
477 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Color table supports at most 4 components.");
478 0 : ret = CE_Failure;
479 : }
480 : }
481 15 : if (b != NULL) {
482 12 : b->DropLock();
483 : }
484 : }
485 : }
486 : }
487 5 : GDALClose(ds);
488 : } else {
489 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: Unable to open downloaded block.");
490 0 : ret = CE_Failure;
491 : }
492 :
493 5 : if (color_table != NULL) {
494 0 : delete[] color_table;
495 : }
496 :
497 5 : return ret;
498 : }
499 :
500 0 : CPLErr GDALWMSRasterBand::ZeroBlock(int x, int y, int to_buffer_band, void *buffer) {
501 0 : CPLErr ret = CE_None;
502 :
503 0 : for (int ib = 1; ib <= m_parent_dataset->nBands; ++ib) {
504 0 : if (ret == CE_None) {
505 0 : void *p = NULL;
506 0 : GDALRasterBlock *b = NULL;
507 0 : if ((buffer != NULL) && (ib == to_buffer_band)) {
508 0 : p = buffer;
509 : } else {
510 0 : GDALWMSRasterBand *band = static_cast<GDALWMSRasterBand *>(m_parent_dataset->GetRasterBand(ib));
511 0 : if (m_overview >= 0) band = static_cast<GDALWMSRasterBand *>(band->GetOverview(m_overview));
512 0 : if (!band->IsBlockInCache(x, y)) {
513 0 : b = band->GetLockedBlockRef(x, y, true);
514 0 : if (b != NULL) {
515 0 : p = b->GetDataRef();
516 0 : if (p == NULL) {
517 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: GetDataRef returned NULL.");
518 0 : ret = CE_Failure;
519 : }
520 : }
521 : }
522 : }
523 0 : if (p != NULL) {
524 0 : unsigned char *b = reinterpret_cast<unsigned char *>(p);
525 0 : int block_size = nBlockXSize * nBlockYSize * (GDALGetDataTypeSize(eDataType) / 8);
526 0 : for (int i = 0; i < block_size; ++i) b[i] = 0;
527 : }
528 0 : if (b != NULL) {
529 0 : b->DropLock();
530 : }
531 : }
532 : }
533 :
534 0 : return ret;
535 : }
536 :
537 0 : CPLErr GDALWMSRasterBand::ReportWMSException(const char *file_name) {
538 0 : CPLErr ret = CE_None;
539 0 : int reported_errors_count = 0;
540 :
541 0 : CPLXMLNode *orig_root = CPLParseXMLFile(file_name);
542 0 : CPLXMLNode *root = orig_root;
543 0 : if (root != NULL) {
544 0 : root = CPLGetXMLNode(root, "=ServiceExceptionReport");
545 : }
546 0 : if (root != NULL) {
547 0 : CPLXMLNode *n = CPLGetXMLNode(root, "ServiceException");
548 0 : while (n != NULL) {
549 0 : const char *exception = CPLGetXMLValue(n, "=ServiceException", "");
550 0 : const char *exception_code = CPLGetXMLValue(n, "=ServiceException.code", "");
551 0 : if (exception[0] != '\0') {
552 0 : if (exception_code[0] != '\0') {
553 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: The server returned exception code '%s': %s", exception_code, exception);
554 0 : ++reported_errors_count;
555 : } else {
556 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: The server returned exception: %s", exception);
557 0 : ++reported_errors_count;
558 : }
559 0 : } else if (exception_code[0] != '\0') {
560 0 : CPLError(CE_Failure, CPLE_AppDefined, "GDALWMS: The server returned exception code '%s'.", exception_code);
561 0 : ++reported_errors_count;
562 : }
563 :
564 0 : n = n->psNext;
565 0 : if (n != NULL) {
566 0 : n = CPLGetXMLNode(n, "=ServiceException");
567 : }
568 : }
569 : } else {
570 0 : ret = CE_Failure;
571 : }
572 0 : if (orig_root != NULL) {
573 0 : CPLDestroyXMLNode(orig_root);
574 : }
575 :
576 0 : if (reported_errors_count == 0) {
577 0 : ret = CE_Failure;
578 : }
579 :
580 0 : return ret;
581 : }
582 :
583 :
584 0 : CPLErr GDALWMSRasterBand::AdviseRead(int x0, int y0, int sx, int sy, int bsx, int bsy, GDALDataType bdt, char **options) {
585 : // printf("AdviseRead(%d, %d, %d, %d)\n", x0, y0, sx, sy);
586 0 : if (m_parent_dataset->m_offline_mode || !m_parent_dataset->m_use_advise_read) return CE_None;
587 0 : if (m_parent_dataset->m_cache == NULL) return CE_Failure;
588 :
589 0 : int bx0 = x0 / nBlockXSize;
590 0 : int by0 = y0 / nBlockYSize;
591 0 : int bx1 = (x0 + sx - 1) / nBlockXSize;
592 0 : int by1 = (y0 + sy - 1) / nBlockYSize;
593 :
594 0 : return ReadBlocks(0, 0, NULL, bx0, by0, bx1, by1, 1);
595 : }
596 :
597 0 : GDALColorInterp GDALWMSRasterBand::GetColorInterpretation() {
598 0 : return m_color_interp;
599 : }
|