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