• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

MapServer / MapServer / 20718230642

05 Jan 2026 02:18PM UTC coverage: 41.754% (+0.03%) from 41.725%
20718230642

push

github

web-flow
WMS: rework building of the layer tree to make it faster, and understandable! (#7410)

* WMS: rework building of the layer tree to make it faster, and understandable!

Avoid quadratic performance in the number of layers.

On a 103 MB .map file with 6130 layers and 3 level of nesting, WMS
GetCapabilities response generation goes from 56 seconds to 5 seconds.

* msRenameLayer(): avoid potential overflow

* mapows.cpp: minimal conversion to C++

* msOWSMakeAllLayersUnique(): avoid quadratic performance in number of layers

* loadMap(): optimize CRS creation when they are all the same

360 of 422 new or added lines in 4 files covered. (85.31%)

76 existing lines in 4 files now uncovered.

62888 of 150616 relevant lines covered (41.75%)

25229.23 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

78.56
/src/mapwcs.cpp
1
/******************************************************************************
2
 * $Id$
3
 *
4
 * Project:  MapServer
5
 * Purpose:  OpenGIS Web Coverage Server (WCS) Implementation.
6
 * Author:   Steve Lime and the MapServer team.
7
 *
8
 ******************************************************************************
9
 * Copyright (c) 1996-2005 Regents of the University of Minnesota.
10
 *
11
 * Permission is hereby granted, free of charge, to any person obtaining a
12
 * copy of this software and associated documentation files (the "Software"),
13
 * to deal in the Software without restriction, including without limitation
14
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
15
 * and/or sell copies of the Software, and to permit persons to whom the
16
 * Software is furnished to do so, subject to the following conditions:
17
 *
18
 * The above copyright notice and this permission notice shall be included in
19
 * all copies of this Software or works derived from this Software.
20
 *
21
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
22
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
24
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
25
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
26
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27
 * DEALINGS IN THE SOFTWARE.
28
 *****************************************************************************/
29

30
#include "mapserver.h"
31
#include "maperror.h"
32
#include "mapthread.h"
33
#include "mapows.h"
34
#include <assert.h>
35

36
#include <map>
37
#include <string>
38

39
#if defined(USE_WCS_SVR)
40

41
#include "mapwcs.h"
42

43
#include "maptime.h"
44
#include <time.h>
45

46
#include "gdal.h"
47
#include "cpl_string.h" /* GDAL string handling */
48

49
/************************************************************************/
50
/*                    msWCSValidateRangeSetParam()                      */
51
/************************************************************************/
52
static int msWCSValidateRangeSetParam(layerObj *lp, char *name,
2✔
53
                                      const char *value) {
54
  char **allowed_ri_values;
55
  char **client_ri_values;
56
  int allowed_count, client_count;
57
  int i_client, i, all_match = 1;
58
  char *tmpname = NULL;
59
  const char *ri_values_list;
60

61
  if (name == NULL)
2✔
62
    return MS_FAILURE;
63

64
  /* Fetch the available values list for the rangeset item and tokenize */
65
  const size_t nSize = strlen(name) + strlen("_values") + 1;
2✔
66
  tmpname = (char *)msSmallMalloc(nSize);
2✔
67
  snprintf(tmpname, nSize, "%s_values", name);
68
  ri_values_list = msOWSLookupMetadata(&(lp->metadata), "CO", tmpname);
2✔
69
  msFree(tmpname);
2✔
70

71
  if (ri_values_list == NULL)
2✔
72
    return MS_FAILURE;
73

74
  allowed_ri_values = msStringSplit(ri_values_list, ',', &allowed_count);
2✔
75

76
  /* Parse the client value list into tokens. */
77
  client_ri_values = msStringSplit(value, ',', &client_count);
2✔
78

79
  /* test each client value against the allowed list. */
80

81
  for (i_client = 0; all_match && i_client < client_count; i_client++) {
6✔
82
    for (i = 0; i < allowed_count && strcasecmp(client_ri_values[i_client],
17✔
83
                                                allowed_ri_values[i]) != 0;
16✔
84
         i++) {
85
    }
86

87
    if (i == allowed_count)
4✔
88
      all_match = 0;
89
  }
90

91
  msFreeCharArray(allowed_ri_values, allowed_count);
2✔
92
  msFreeCharArray(client_ri_values, client_count);
2✔
93

94
  if (all_match == 0)
2✔
95
    return MS_FAILURE;
96
  else
97
    return MS_SUCCESS;
98
}
99

100
/************************************************************************/
101
/*                    msWCSConvertRangeSetToString()                    */
102
/************************************************************************/
103
static char *msWCSConvertRangeSetToString(const char *value) {
1✔
104
  char **tokens;
105
  int numtokens;
106
  double min, max, res;
107
  double val;
108
  char buf1[128], *buf2 = NULL;
109

110
  if (strchr(value, '/')) { /* value is min/max/res */
1✔
111
    tokens = msStringSplit(value, '/', &numtokens);
×
112
    if (tokens == NULL || numtokens != 3) {
×
113
      msFreeCharArray(tokens, numtokens);
×
114
      return NULL; /* not a set of equally spaced intervals */
×
115
    }
116

117
    min = atof(tokens[0]);
×
118
    max = atof(tokens[1]);
×
119
    res = atof(tokens[2]);
×
120
    msFreeCharArray(tokens, numtokens);
×
121

122
    for (val = min; val <= max; val += res) {
×
123
      if (val == min)
×
124
        snprintf(buf1, sizeof(buf1), "%g", val);
125
      else
126
        snprintf(buf1, sizeof(buf1), ",%g", val);
127
      buf2 = msStringConcatenate(buf2, buf1);
×
128
    }
129

130
    return buf2;
×
131
  } else
132
    return msStrdup(value);
1✔
133
}
134

135
/************************************************************************/
136
/*                           msWCSException()                           */
137
/************************************************************************/
138
int msWCSException(mapObj *map, const char *code, const char *locator,
52✔
139
                   const char *version) {
140
  char *pszEncodedVal = NULL;
141
  char version_string[OWS_VERSION_MAXLEN];
142

143
  if (version == NULL)
52✔
144
    version = "1.0.0";
145

146
#if defined(USE_LIBXML2)
147
  if (msOWSParseVersionString(version) >= OWS_2_0_0)
52✔
148
    return msWCSException20(
33✔
149
        map, code, locator,
150
        msOWSGetVersionString(msOWSParseVersionString(version),
151
                              version_string));
152
#endif
153

154
  if (msOWSParseVersionString(version) >= OWS_1_1_0)
19✔
155
    return msWCSException11(
7✔
156
        map, code, locator,
157
        msOWSGetVersionString(msOWSParseVersionString(version),
158
                              version_string));
159

160
  msIO_setHeader("Content-Type", "application/vnd.ogc.se_xml; charset=UTF-8");
12✔
161
  msIO_sendHeaders();
12✔
162

163
  /* msIO_printf("Content-Type: text/xml%c%c",10,10); */
164

165
  msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" ?>\n");
12✔
166

167
  msIO_printf("<ServiceExceptionReport version=\"1.2.0\"\n");
12✔
168
  msIO_printf("xmlns=\"http://www.opengis.net/ogc\" ");
12✔
169
  msIO_printf("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
12✔
170
  pszEncodedVal = msEncodeHTMLEntities(msOWSGetSchemasLocation(map));
12✔
171
  msIO_printf("xsi:schemaLocation=\"http://www.opengis.net/ogc "
12✔
172
              "%s/wcs/1.0.0/OGC-exception.xsd\">\n",
173
              pszEncodedVal);
174
  msFree(pszEncodedVal);
12✔
175
  msIO_printf("  <ServiceException");
12✔
176
  if (code) {
12✔
177
    msIO_printf(" code=\"%s\"", code);
12✔
178
  }
179
  if (locator) {
12✔
180
    msIO_printf(" locator=\"%s\"", locator);
12✔
181
  }
182
  msIO_printf(">");
12✔
183
  msWriteErrorXML(stdout);
12✔
184
  msIO_printf("  </ServiceException>\n");
12✔
185
  msIO_printf("</ServiceExceptionReport>\n");
12✔
186

187
  msResetErrorList();
12✔
188

189
  return MS_FAILURE;
190
}
191

192
/************************************************************************/
193
/*                    msWCSPrintRequestCapability()                     */
194
/************************************************************************/
195

196
static void msWCSPrintRequestCapability(const char *request_tag,
29✔
197
                                        const char *script_url) {
198
  msIO_printf("    <%s>\n", request_tag);
29✔
199

200
  msIO_printf("      <DCPType>\n");
29✔
201
  msIO_printf("        <HTTP>\n");
29✔
202
  msIO_printf("          <Get><OnlineResource xlink:type=\"simple\" "
29✔
203
              "xlink:href=\"%s\" /></Get>\n",
204
              script_url);
205
  msIO_printf("        </HTTP>\n");
29✔
206
  msIO_printf("      </DCPType>\n");
29✔
207
  msIO_printf("      <DCPType>\n");
29✔
208
  msIO_printf("        <HTTP>\n");
29✔
209
  msIO_printf("          <Post><OnlineResource xlink:type=\"simple\" "
29✔
210
              "xlink:href=\"%s\" /></Post>\n",
211
              script_url);
212
  msIO_printf("        </HTTP>\n");
29✔
213
  msIO_printf("      </DCPType>\n");
29✔
214

215
  msIO_printf("    </%s>\n", request_tag);
29✔
216
}
29✔
217

218
/************************************************************************/
219
/*                         msWCSCreateParams()                          */
220
/************************************************************************/
221
static wcsParamsObj *msWCSCreateParams() {
81✔
222
  wcsParamsObj *params;
223

224
  params = (wcsParamsObj *)calloc(1, sizeof(wcsParamsObj));
81✔
225
  MS_CHECK_ALLOC(params, sizeof(wcsParamsObj), NULL);
81✔
226

227
  return params;
228
}
229

230
/************************************************************************/
231
/*                          msWCSFreeParams()                           */
232
/************************************************************************/
233
void msWCSFreeParams(wcsParamsObj *params) {
81✔
234
  if (params) {
81✔
235
    /* TODO */
236
    if (params->version)
81✔
237
      free(params->version);
81✔
238
    if (params->updatesequence)
81✔
239
      free(params->updatesequence);
19✔
240
    if (params->request)
81✔
241
      free(params->request);
81✔
242
    if (params->service)
81✔
243
      free(params->service);
81✔
244
    if (params->section)
81✔
245
      free(params->section);
7✔
246
    if (params->crs)
81✔
247
      free(params->crs);
43✔
248
    if (params->response_crs)
81✔
249
      free(params->response_crs);
×
250
    if (params->format)
81✔
251
      free(params->format);
42✔
252
    if (params->exceptions)
81✔
253
      free(params->exceptions);
×
254
    if (params->time)
81✔
255
      free(params->time);
1✔
256
    if (params->interpolation)
81✔
257
      free(params->interpolation);
2✔
258
    CSLDestroy(params->coverages);
81✔
259
  }
260
}
81✔
261

262
/************************************************************************/
263
/*                       msWCSIsLayerSupported()                        */
264
/************************************************************************/
265

266
int msWCSIsLayerSupported(layerObj *layer) {
383✔
267
  /* only raster layers, are elligible to be served via WCS, WMS rasters are not
268
   * ok */
269
  if ((layer->type == MS_LAYER_RASTER) && layer->connectiontype != MS_WMS &&
383✔
270
      layer->name != NULL)
343✔
271
    return MS_TRUE;
343✔
272

273
  return MS_FALSE;
274
}
275

276
/************************************************************************/
277
/*                      msWCSGetRequestParameter()                      */
278
/*                                                                      */
279
/************************************************************************/
280

281
const char *msWCSGetRequestParameter(cgiRequestObj *request, const char *name) {
38✔
282
  int i;
283

284
  if (!request || !name) /* nothing to do */
38✔
285
    return NULL;
286

287
  if (request->NumParams > 0) {
38✔
288
    for (i = 0; i < request->NumParams; i++) {
352✔
289
      if (strcasecmp(request->ParamNames[i], name) == 0)
323✔
290
        return request->ParamValues[i];
2✔
291
    }
292
  }
293

294
  return NULL;
295
}
296

297
/************************************************************************/
298
/*                  msWCSSetDefaultBandsRangeSetInfo()                  */
299
/************************************************************************/
300

301
void msWCSSetDefaultBandsRangeSetInfo(wcsParamsObj *params,
47✔
302
                                      coverageMetadataObj *cm, layerObj *lp) {
303
  (void)params;
304

305
  /* This function will provide default rangeset information for the "special"
306
   */
307
  /* "bands" rangeset if it appears in the axes list but has no specifics
308
   * provided */
309
  /* in the metadata.   */
310

311
  const char *value;
312
  char *bandlist;
313
  size_t bufferSize = 0;
314
  int i;
315

316
  /* Does this item exist in the axes list?  */
317

318
  value = msOWSLookupMetadata(&(lp->metadata), "CO", "rangeset_axes");
47✔
319
  if (value == NULL)
47✔
320
    return;
321

322
  value = strstr(value, "bands");
323
  if (value == NULL || (value[5] != '\0' && value[5] != ' '))
42✔
324
    return;
325

326
  /* Are there any w*s_bands_ metadata already? If so, skip out. */
327
  if (msOWSLookupMetadata(&(lp->metadata), "CO", "bands_description") != NULL ||
84✔
328
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_name") != NULL ||
84✔
329
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_label") != NULL ||
84✔
330
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_values") != NULL ||
84✔
331
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_values_semantic") !=
42✔
332
          NULL ||
42✔
333
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_values_type") != NULL ||
84✔
334
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_rangeitem") != NULL ||
84✔
335
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_semantic") != NULL ||
84✔
336
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_refsys") != NULL ||
84✔
337
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_refsyslabel") != NULL ||
126✔
338
      msOWSLookupMetadata(&(lp->metadata), "CO", "bands_interval") != NULL)
42✔
339
    return;
×
340

341
  /* OK, we have decided to fill in the information. */
342

343
  msInsertHashTable(&(lp->metadata), "wcs_bands_name", "bands");
42✔
344
  msInsertHashTable(&(lp->metadata), "wcs_bands_label",
42✔
345
                    "Bands/Channels/Samples");
346
  msInsertHashTable(&(lp->metadata), "wcs_bands_rangeitem", "_bands"); /* ? */
42✔
347

348
  bufferSize = cm->bandcount * 30 + 30;
42✔
349
  bandlist = (char *)msSmallMalloc(bufferSize);
42✔
350
  strcpy(bandlist, "1");
351
  for (i = 1; i < cm->bandcount; i++)
50✔
352
    snprintf(bandlist + strlen(bandlist), bufferSize - strlen(bandlist), ",%d",
8✔
353
             i + 1);
354

355
  msInsertHashTable(&(lp->metadata), "wcs_bands_values", bandlist);
42✔
356
  free(bandlist);
42✔
357
}
358

359
/************************************************************************/
360
/*                         msWCSParseRequest()                          */
361
/************************************************************************/
362

363
static int msWCSParseRequest(cgiRequestObj *request, wcsParamsObj *params,
81✔
364
                             mapObj *map) {
365
  int i, n;
366
  char **tokens;
367

368
  if (!request || !params) /* nothing to do */
81✔
369
    return MS_SUCCESS;
370

371
  /* -------------------------------------------------------------------- */
372
  /*      Check if this appears to be an XML POST WCS request.            */
373
  /* -------------------------------------------------------------------- */
374

375
  msDebug("msWCSParseRequest(): request is %s.\n",
81✔
376
          (request->type == MS_POST_REQUEST) ? "POST" : "KVP");
81✔
377

378
  if (request->type == MS_POST_REQUEST && request->postrequest) {
81✔
379
#if defined(USE_LIBXML2)
380
    xmlDocPtr doc = NULL;
381
    xmlNodePtr root = NULL, child = NULL;
382
    char *tmp = NULL;
383

384
    /* parse to DOM-Structure and get root element */
385
    if ((doc = xmlParseMemory(request->postrequest,
17✔
386
                              strlen(request->postrequest))) == NULL) {
17✔
387
      const xmlError *error = xmlGetLastError();
×
388
      msSetError(MS_WCSERR, "XML parsing error: %s", "msWCSParseRequest()",
×
389
                 error->message);
×
390
      return MS_FAILURE;
×
391
    }
392
    root = xmlDocGetRootElement(doc);
17✔
393

394
    /* Get service, version and request from root */
395
    params->request = msStrdup((char *)root->name);
17✔
396
    if ((tmp = (char *)xmlGetProp(root, BAD_CAST "service")) != NULL)
17✔
397
      params->service = tmp;
17✔
398
    if ((tmp = (char *)xmlGetProp(root, BAD_CAST "version")) != NULL)
17✔
399
      params->version = tmp;
17✔
400

401
    /* search first level children, either CoverageID,  */
402
    for (child = root->children; child != NULL; child = child->next) {
50✔
403
      if (EQUAL((char *)child->name, "AcceptVersions")) {
33✔
404
        /* will be overridden to 1.1.1 anyway */
405
      } else if (EQUAL((char *)child->name, "UpdateSequence")) {
33✔
406
        params->updatesequence = (char *)xmlNodeGetContent(child);
3✔
407
      } else if (EQUAL((char *)child->name, "Sections")) {
30✔
408
        xmlNodePtr sectionNode = NULL;
409
        /* concatenate all sections by ',' */
410
        for (sectionNode = child->children; sectionNode != NULL;
8✔
411
             sectionNode = sectionNode->next) {
5✔
412
          char *content;
413
          if (!EQUAL((char *)sectionNode->name, "Section"))
5✔
414
            continue;
×
415
          content = (char *)xmlNodeGetContent(sectionNode);
5✔
416
          if (!params->section) {
5✔
417
            params->section = content;
3✔
418
          } else {
419
            params->section = msStringConcatenate(params->section, ",");
2✔
420
            params->section = msStringConcatenate(params->section, content);
2✔
421
            xmlFree(content);
2✔
422
          }
423
        }
424
      } else if (EQUAL((char *)child->name, "AcceptFormats")) {
27✔
425
        /* TODO: implement */
426
      } else if (EQUAL((char *)child->name, "Identifier")) {
27✔
427
        char *content = (char *)xmlNodeGetContent(child);
9✔
428
        params->coverages = CSLAddString(params->coverages, content);
9✔
429
        xmlFree(content);
9✔
430
      } else if (EQUAL((char *)child->name, "DomainSubset")) {
18✔
431
        xmlNodePtr tmpNode = NULL;
432
        for (tmpNode = child->children; tmpNode != NULL;
18✔
433
             tmpNode = tmpNode->next) {
9✔
434
          if (EQUAL((char *)tmpNode->name, "BoundingBox")) {
9✔
435
            xmlNodePtr cornerNode = NULL;
436
            params->crs = (char *)xmlGetProp(tmpNode, BAD_CAST "crs");
9✔
437
            if (strncasecmp(params->crs, "urn:ogc:def:crs:", 16) == 0 &&
9✔
438
                strncasecmp(params->crs + strlen(params->crs) - 8, "imageCRS",
9✔
439
                            8) == 0)
440
              strcpy(params->crs, "imageCRS");
441
            for (cornerNode = tmpNode->children; cornerNode != NULL;
27✔
442
                 cornerNode = cornerNode->next) {
18✔
443
              if (EQUAL((char *)cornerNode->name, "LowerCorner")) {
18✔
444
                char *value = (char *)xmlNodeGetContent(cornerNode);
9✔
445
                tokens = msStringSplit(value, ' ', &n);
9✔
446
                if (tokens == NULL || n < 2) {
9✔
447
                  msSetError(MS_WCSERR,
×
448
                             "Wrong number of arguments for LowerCorner",
449
                             "msWCSParseRequest()");
450
                  return msWCSException(map, "InvalidParameterValue",
×
451
                                        "LowerCorner", params->version);
×
452
                }
453
                params->bbox.minx = atof(tokens[0]);
9✔
454
                params->bbox.miny = atof(tokens[1]);
9✔
455
                msFreeCharArray(tokens, n);
9✔
456
                xmlFree(value);
9✔
457
              }
458
              if (EQUAL((char *)cornerNode->name, "UpperCorner")) {
18✔
459
                char *value = (char *)xmlNodeGetContent(cornerNode);
9✔
460
                tokens = msStringSplit(value, ' ', &n);
9✔
461
                if (tokens == NULL || n < 2) {
9✔
462
                  msSetError(MS_WCSERR,
×
463
                             "Wrong number of arguments for UpperCorner",
464
                             "msWCSParseRequest()");
465
                  return msWCSException(map, "InvalidParameterValue",
×
466
                                        "UpperCorner", params->version);
×
467
                }
468
                params->bbox.maxx = atof(tokens[0]);
9✔
469
                params->bbox.maxy = atof(tokens[1]);
9✔
470
                msFreeCharArray(tokens, n);
9✔
471
                xmlFree(value);
9✔
472
              }
473
            }
474
          }
475
        }
476
      } else if (EQUAL((char *)child->name, "RangeSubset")) {
9✔
477
        /* TODO: not implemented in mapserver WCS 1.1? */
478
      } else if (EQUAL((char *)child->name, "Output")) {
9✔
479
        xmlNodePtr tmpNode = NULL;
480
        params->format = (char *)xmlGetProp(child, BAD_CAST "format");
9✔
481
        for (tmpNode = child->children; tmpNode != NULL;
18✔
482
             tmpNode = tmpNode->next) {
9✔
483
          if (EQUAL((char *)tmpNode->name, "GridCRS")) {
9✔
484
            xmlNodePtr crsNode = NULL;
485
            for (crsNode = tmpNode->children; crsNode != NULL;
45✔
486
                 crsNode = crsNode->next) {
36✔
487
              if (EQUAL((char *)crsNode->name, "GridBaseCRS")) {
36✔
488
                params->response_crs = (char *)xmlNodeGetContent(crsNode);
×
489
              } else if (EQUAL((char *)crsNode->name, "GridOrigin")) {
36✔
490
                char *value = (char *)xmlNodeGetContent(crsNode);
9✔
491
                tokens = msStringSplit(value, ' ', &n);
9✔
492
                if (tokens == NULL || n < 2) {
9✔
493
                  msSetError(MS_WCSERR,
×
494
                             "Wrong number of arguments for GridOrigin",
495
                             "msWCSParseRequest()");
496
                  return msWCSException(map, "InvalidParameterValue",
×
497
                                        "GridOffsets", params->version);
×
498
                }
499
                params->originx = atof(tokens[0]);
9✔
500
                params->originy = atof(tokens[1]);
9✔
501
                msFreeCharArray(tokens, n);
9✔
502
                xmlFree(value);
9✔
503
              } else if (EQUAL((char *)crsNode->name, "GridOffsets")) {
27✔
504
                char *value = (char *)xmlNodeGetContent(crsNode);
9✔
505
                tokens = msStringSplit(value, ' ', &n);
9✔
506
                if (tokens == NULL || n < 2) {
9✔
507
                  msSetError(MS_WCSERR,
×
508
                             "Wrong number of arguments for GridOffsets",
509
                             "msWCSParseRequest()");
510
                  return msWCSException(map, "InvalidParameterValue",
×
511
                                        "GridOffsets", params->version);
×
512
                }
513
                /* take absolute values to convert to positive RESX/RESY style
514
                WCS 1.0 behavior.  *but* this does break some possibilities! */
515
                params->resx = fabs(atof(tokens[0]));
9✔
516
                params->resy = fabs(atof(tokens[1]));
9✔
517
                msFreeCharArray(tokens, n);
9✔
518
                xmlFree(value);
9✔
519
              }
520
            }
521
          }
522
        }
523
      }
524
    }
525
    xmlFreeDoc(doc);
17✔
526
    xmlCleanupParser();
17✔
527
    return MS_SUCCESS;
17✔
528
#else  /* defined(USE_LIBXML2) */
529
    msSetError(MS_WCSERR,
530
               "To enable POST requests, MapServer has to "
531
               "be compiled with libxml2.",
532
               "msWCSParseRequest()");
533
    return MS_FAILURE;
534
#endif /* defined(USE_LIBXML2) */
535
  }
536

537
  /* -------------------------------------------------------------------- */
538
  /*      Extract WCS KVP Parameters.                                     */
539
  /* -------------------------------------------------------------------- */
540
  if (request->NumParams > 0) {
64✔
541
    for (i = 0; i < request->NumParams; i++) {
556✔
542

543
      if (strcasecmp(request->ParamNames[i], "VERSION") == 0)
492✔
544
        params->version = msStrdup(request->ParamValues[i]);
64✔
545
      if (strcasecmp(request->ParamNames[i], "UPDATESEQUENCE") == 0)
492✔
546
        params->updatesequence = msStrdup(request->ParamValues[i]);
6✔
547
      else if (strcasecmp(request->ParamNames[i], "REQUEST") == 0)
486✔
548
        params->request = msStrdup(request->ParamValues[i]);
64✔
549
      else if (strcasecmp(request->ParamNames[i], "INTERPOLATION") == 0)
422✔
550
        params->interpolation = msStrdup(request->ParamValues[i]);
2✔
551
      else if (strcasecmp(request->ParamNames[i], "SERVICE") == 0)
420✔
552
        params->service = msStrdup(request->ParamValues[i]);
64✔
553
      else if (strcasecmp(request->ParamNames[i], "SECTION") == 0) /* 1.0 */
356✔
554
        params->section =
1✔
555
            msStrdup(request->ParamValues[i]); /* TODO: validate value here */
1✔
556
      else if (strcasecmp(request->ParamNames[i], "SECTIONS") == 0) /* 1.1 */
355✔
557
        params->section =
3✔
558
            msStrdup(request->ParamValues[i]); /* TODO: validate value here */
3✔
559

560
      /* GetCoverage parameters. */
561
      else if (strcasecmp(request->ParamNames[i], "BBOX") == 0) {
352✔
562
        tokens = msStringSplit(request->ParamValues[i], ',', &n);
22✔
563
        if (tokens == NULL || n != 4) {
22✔
564
          msSetError(MS_WCSERR, "Wrong number of arguments for BBOX.",
×
565
                     "msWCSParseRequest()");
566
          return msWCSException(map, "InvalidParameterValue", "bbox",
×
567
                                params->version);
×
568
        }
569
        params->bbox.minx = atof(tokens[0]);
22✔
570
        params->bbox.miny = atof(tokens[1]);
22✔
571
        params->bbox.maxx = atof(tokens[2]);
22✔
572
        params->bbox.maxy = atof(tokens[3]);
22✔
573

574
        msFreeCharArray(tokens, n);
22✔
575
      } else if (strcasecmp(request->ParamNames[i], "RESX") == 0)
330✔
576
        params->resx = atof(request->ParamValues[i]);
6✔
577
      else if (strcasecmp(request->ParamNames[i], "RESY") == 0)
324✔
578
        params->resy = atof(request->ParamValues[i]);
6✔
579
      else if (strcasecmp(request->ParamNames[i], "WIDTH") == 0)
318✔
580
        params->width = atoi(request->ParamValues[i]);
16✔
581
      else if (strcasecmp(request->ParamNames[i], "HEIGHT") == 0)
302✔
582
        params->height = atoi(request->ParamValues[i]);
16✔
583
      else if (strcasecmp(request->ParamNames[i], "COVERAGE") == 0)
286✔
584
        params->coverages =
26✔
585
            CSLAddString(params->coverages, request->ParamValues[i]);
26✔
586
      else if (strcasecmp(request->ParamNames[i], "TIME") == 0)
260✔
587
        params->time = msStrdup(request->ParamValues[i]);
1✔
588
      else if (strcasecmp(request->ParamNames[i], "FORMAT") == 0)
259✔
589
        params->format = msStrdup(request->ParamValues[i]);
34✔
590
      else if (strcasecmp(request->ParamNames[i], "CRS") == 0)
225✔
591
        params->crs = msStrdup(request->ParamValues[i]);
22✔
592
      else if (strcasecmp(request->ParamNames[i], "RESPONSE_CRS") == 0)
203✔
593
        params->response_crs = msStrdup(request->ParamValues[i]);
×
594

595
      /* WCS 1.1 DescribeCoverage and GetCoverage ... */
596
      else if (strcasecmp(request->ParamNames[i], "IDENTIFIER") == 0 ||
203✔
597
               strcasecmp(request->ParamNames[i], "IDENTIFIERS") == 0) {
190✔
598
        msDebug("msWCSParseRequest(): Whole String: %s\n",
13✔
599
                request->ParamValues[i]);
13✔
600
        params->coverages =
13✔
601
            CSLAddString(params->coverages, request->ParamValues[i]);
13✔
602
      }
603
      /* WCS 1.1 style BOUNDINGBOX */
604
      else if (strcasecmp(request->ParamNames[i], "BOUNDINGBOX") == 0) {
190✔
605
        tokens = msStringSplit(request->ParamValues[i], ',', &n);
12✔
606
        if (tokens == NULL || n < 5) {
12✔
607
          msSetError(MS_WCSERR, "Wrong number of arguments for BOUNDINGBOX.",
×
608
                     "msWCSParseRequest()");
609
          return msWCSException(map, "InvalidParameterValue", "boundingbox",
×
610
                                params->version);
×
611
        }
612

613
        /* NOTE: WCS 1.1 boundingbox is center of pixel oriented, not edge
614
           like in WCS 1.0.  So bbox semantics are wonky till this is fixed
615
           later in the GetCoverage processing. */
616
        params->bbox.minx = atof(tokens[0]);
12✔
617
        params->bbox.miny = atof(tokens[1]);
12✔
618
        params->bbox.maxx = atof(tokens[2]);
12✔
619
        params->bbox.maxy = atof(tokens[3]);
12✔
620

621
        params->crs = msStrdup(tokens[4]);
12✔
622
        msFreeCharArray(tokens, n);
12✔
623
        /* normalize imageCRS urns to simply "imageCRS" */
624
        if (strncasecmp(params->crs, "urn:ogc:def:crs:", 16) == 0 &&
12✔
625
            strncasecmp(params->crs + strlen(params->crs) - 8, "imageCRS", 8) ==
12✔
626
                0)
627
          strcpy(params->crs, "imageCRS");
628
      } else if (strcasecmp(request->ParamNames[i], "GridOffsets") == 0) {
178✔
629
        tokens = msStringSplit(request->ParamValues[i], ',', &n);
12✔
630
        if (tokens == NULL || n < 2) {
12✔
631
          msSetError(MS_WCSERR, "Wrong number of arguments for GridOffsets",
×
632
                     "msWCSParseRequest()");
633
          return msWCSException(map, "InvalidParameterValue", "GridOffsets",
×
634
                                params->version);
×
635
        }
636
        /* take absolute values to convert to positive RESX/RESY style
637
           WCS 1.0 behavior.  *but* this does break some possibilities! */
638
        params->resx = fabs(atof(tokens[0]));
12✔
639
        params->resy = fabs(atof(tokens[1]));
12✔
640
        msFreeCharArray(tokens, n);
12✔
641
      } else if (strcasecmp(request->ParamNames[i], "GridOrigin") == 0) {
166✔
642
        tokens = msStringSplit(request->ParamValues[i], ',', &n);
12✔
643
        if (tokens == NULL || n < 2) {
12✔
644
          msSetError(MS_WCSERR, "Wrong number of arguments for GridOrigin",
×
645
                     "msWCSParseRequest()");
646
          return msWCSException(map, "InvalidParameterValue", "GridOffsets",
×
647
                                params->version);
×
648
        }
649
        params->originx = atof(tokens[0]);
12✔
650
        params->originy = atof(tokens[1]);
12✔
651
        msFreeCharArray(tokens, n);
12✔
652
      }
653

654
      /* and so on... */
655
    }
656
  }
657
  /* we are not dealing with an XML encoded request at this point */
658
  return MS_SUCCESS;
659
}
660

661
/************************************************************************/
662
/*           msWCSGetCapabilities_Service_ResponsibleParty()            */
663
/************************************************************************/
664

665
static void msWCSGetCapabilities_Service_ResponsibleParty(mapObj *map) {
10✔
666
  int bEnableTelephone = MS_FALSE, bEnableAddress = MS_FALSE,
667
      bEnableOnlineResource = MS_FALSE;
668

669
  /* the WCS-specific way */
670
  if (msOWSLookupMetadata(&(map->web.metadata), "CO",
10✔
671
                          "responsibleparty_individualname") ||
20✔
672
      msOWSLookupMetadata(&(map->web.metadata), "CO",
10✔
673
                          "responsibleparty_organizationname")) {
674

675
    msIO_printf("<responsibleParty>\n");
×
676
    msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
677
                             "responsibleparty_individualname", OWS_NOERR,
678
                             "    <individualName>%s</individualName>\n", NULL);
679
    msOWSPrintEncodeMetadata(
×
680
        stdout, &(map->web.metadata), "CO", "responsibleparty_organizationname",
681
        OWS_NOERR, "    <organisationName>%s</organisationName>\n", NULL);
682
    msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
683
                             "responsibleparty_positionname", OWS_NOERR,
684
                             "    <positionName>%s</positionName>\n", NULL);
685

686
    if (msOWSLookupMetadata(&(map->web.metadata), "CO",
×
687
                            "responsibleparty_phone_voice") ||
×
688
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
689
                            "responsibleparty_phone_facsimile"))
690
      bEnableTelephone = MS_TRUE;
691

692
    if (msOWSLookupMetadata(&(map->web.metadata), "CO",
×
693
                            "responsibleparty_address_deliverypoint") ||
×
694
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
695
                            "responsibleparty_address_city") ||
×
696
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
697
                            "responsibleparty_address_administrativearea") ||
×
698
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
699
                            "responsibleparty_address_postalcode") ||
×
700
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
701
                            "responsibleparty_address_country") ||
×
702
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
703
                            "responsibleparty_address_electronicmailaddress"))
704
      bEnableAddress = MS_TRUE;
705

706
    if (msOWSLookupMetadata(&(map->web.metadata), "CO",
×
707
                            "responsibleparty_onlineresource"))
708
      bEnableOnlineResource = MS_TRUE;
709

710
    if (bEnableTelephone || bEnableAddress || bEnableOnlineResource) {
×
711
      msIO_printf("  <contactInfo>\n");
×
712
      if (bEnableTelephone) {
×
713
        msIO_printf("    <phone>\n");
×
714
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
715
                                 "responsibleparty_phone_voice", OWS_NOERR,
716
                                 "    <voice>%s</voice>\n", NULL);
717
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
718
                                 "responsibleparty_phone_facsimile", OWS_NOERR,
719
                                 "    <facsimile>%s</facsimile>\n", NULL);
720
        msIO_printf("    </phone>\n");
×
721
      }
722
      if (bEnableAddress) {
×
723
        msIO_printf("    <address>\n");
×
724
        msOWSPrintEncodeMetadata(
×
725
            stdout, &(map->web.metadata), "CO",
726
            "responsibleparty_address_deliverypoint", OWS_NOERR,
727
            "    <deliveryPoint>%s</deliveryPoint>\n", NULL);
728
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
729
                                 "responsibleparty_address_city", OWS_NOERR,
730
                                 "    <city>%s</city>\n", NULL);
731
        msOWSPrintEncodeMetadata(
×
732
            stdout, &(map->web.metadata), "CO",
733
            "responsibleparty_address_administrativearea", OWS_NOERR,
734
            "    <administrativeArea>%s</administrativeArea>\n", NULL);
735
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
736
                                 "responsibleparty_address_postalcode",
737
                                 OWS_NOERR, "    <postalCode>%s</postalCode>\n",
738
                                 NULL);
739
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
×
740
                                 "responsibleparty_address_country", OWS_NOERR,
741
                                 "    <country>%s</country>\n", NULL);
742
        msOWSPrintEncodeMetadata(
×
743
            stdout, &(map->web.metadata), "CO",
744
            "responsibleparty_address_electronicmailaddress", OWS_NOERR,
745
            "    <electronicMailAddress>%s</electronicMailAddress>\n", NULL);
746
        msIO_printf("    </address>\n");
×
747
      }
748
      msOWSPrintEncodeMetadata(
×
749
          stdout, &(map->web.metadata), "CO", "responsibleparty_onlineresource",
750
          OWS_NOERR,
751
          "    <onlineResource xlink:type=\"simple\" xlink:href=\"%s\"/>\n",
752
          NULL);
753
      msIO_printf("  </contactInfo>\n");
×
754
    }
755

756
    msIO_printf("</responsibleParty>\n");
×
757

758
  } else if (msOWSLookupMetadata(&(map->web.metadata), "CO", "contactperson") ||
13✔
759
             msOWSLookupMetadata(
3✔
760
                 &(map->web.metadata), "CO",
761
                 "contactorganization")) { /* leverage WMS contact information
762
                                            */
763

764
    msIO_printf("<responsibleParty>\n");
7✔
765
    msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
7✔
766
                             "contactperson", OWS_NOERR,
767
                             "    <individualName>%s</individualName>\n", NULL);
768
    msOWSPrintEncodeMetadata(
7✔
769
        stdout, &(map->web.metadata), "CO", "contactorganization", OWS_NOERR,
770
        "    <organisationName>%s</organisationName>\n", NULL);
771
    msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
7✔
772
                             "contactposition", OWS_NOERR,
773
                             "    <positionName>%s</positionName>\n", NULL);
774

775
    if (msOWSLookupMetadata(&(map->web.metadata), "CO",
7✔
776
                            "contactvoicetelephone") ||
7✔
777
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
778
                            "contactfacsimiletelephone"))
779
      bEnableTelephone = MS_TRUE;
780

781
    if (msOWSLookupMetadata(&(map->web.metadata), "CO", "address") ||
7✔
782
        msOWSLookupMetadata(&(map->web.metadata), "CO", "city") ||
×
783
        msOWSLookupMetadata(&(map->web.metadata), "CO", "stateorprovince") ||
×
784
        msOWSLookupMetadata(&(map->web.metadata), "CO", "postcode") ||
×
785
        msOWSLookupMetadata(&(map->web.metadata), "CO", "country") ||
7✔
786
        msOWSLookupMetadata(&(map->web.metadata), "CO",
×
787
                            "contactelectronicmailaddress"))
788
      bEnableAddress = MS_TRUE;
789

790
    if (msOWSLookupMetadata(&(map->web.metadata), "CO",
7✔
791
                            "service_onlineresource"))
792
      bEnableOnlineResource = MS_TRUE;
793

794
    if (bEnableTelephone || bEnableAddress || bEnableOnlineResource) {
7✔
795
      msIO_printf("  <contactInfo>\n");
7✔
796
      if (bEnableTelephone) {
7✔
797
        msIO_printf("    <phone>\n");
7✔
798
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
7✔
799
                                 "contactvoicetelephone", OWS_NOERR,
800
                                 "    <voice>%s</voice>\n", NULL);
801
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO",
7✔
802
                                 "contactfacsimiletelephone", OWS_NOERR,
803
                                 "    <facsimile>%s</facsimile>\n", NULL);
804
        msIO_printf("    </phone>\n");
7✔
805
      }
806
      if (bEnableAddress) {
7✔
807
        msIO_printf("    <address>\n");
7✔
808
        msOWSPrintEncodeMetadata(
7✔
809
            stdout, &(map->web.metadata), "CO", "address", OWS_NOERR,
810
            "    <deliveryPoint>%s</deliveryPoint>\n", NULL);
811
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "city",
7✔
812
                                 OWS_NOERR, "    <city>%s</city>\n", NULL);
813
        msOWSPrintEncodeMetadata(
7✔
814
            stdout, &(map->web.metadata), "CO", "stateorprovince", OWS_NOERR,
815
            "    <administrativeArea>%s</administrativeArea>\n", NULL);
816
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "postcode",
7✔
817
                                 OWS_NOERR, "    <postalCode>%s</postalCode>\n",
818
                                 NULL);
819
        msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "country",
7✔
820
                                 OWS_NOERR, "    <country>%s</country>\n",
821
                                 NULL);
822
        msOWSPrintEncodeMetadata(
7✔
823
            stdout, &(map->web.metadata), "CO", "contactelectronicmailaddress",
824
            OWS_NOERR,
825
            "    <electronicMailAddress>%s</electronicMailAddress>\n", NULL);
826
        msIO_printf("    </address>\n");
7✔
827
      }
828
      msOWSPrintEncodeMetadata(
7✔
829
          stdout, &(map->web.metadata), "CO", "service_onlineresource",
830
          OWS_NOERR,
831
          "    <onlineResource xlink:type=\"simple\" xlink:href=\"%s\"/>\n",
832
          NULL);
833
      msIO_printf("  </contactInfo>\n");
7✔
834
    }
835
    msIO_printf("</responsibleParty>\n");
7✔
836
  }
837

838
  return;
10✔
839
}
840

841
/************************************************************************/
842
/*                    msWCSGetCapabilities_Service()                    */
843
/************************************************************************/
844

845
static int msWCSGetCapabilities_Service(mapObj *map, wcsParamsObj *params) {
10✔
846
  /* start the Service section, only need the full start tag if this is the only
847
   * section requested */
848
  if (!params->section ||
10✔
849
      (params->section && strcasecmp(params->section, "/") == 0))
×
850
    msIO_printf("<Service>\n");
10✔
851
  else
852
    msIO_printf("<Service\n"
×
853
                "   version=\"%s\" \n"
854
                "   updateSequence=\"%s\" \n"
855
                "   xmlns=\"http://www.opengis.net/wcs\" \n"
856
                "   xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n"
857
                "   xmlns:gml=\"http://www.opengis.net/gml\" \n"
858
                "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
859
                "   xsi:schemaLocation=\"http://www.opengis.net/wcs "
860
                "%s/wcs/%s/wcsCapabilities.xsd\">\n",
861
                params->version, params->updatesequence,
862
                msOWSGetSchemasLocation(map), params->version);
863

864
  /* optional metadataLink */
865
  msOWSPrintURLType(stdout, &(map->web.metadata), "CO", "metadatalink",
10✔
866
                    OWS_NOERR,
867
                    "  <metadataLink%s%s%s%s xlink:type=\"simple\"%s/>", NULL,
868
                    " metadataType=\"%s\"", NULL, NULL, NULL,
869
                    " xlink:href=\"%s\"", MS_FALSE, MS_FALSE, MS_FALSE,
870
                    MS_FALSE, MS_TRUE, "other", NULL, NULL, NULL, NULL, NULL);
871

872
  msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "description",
10✔
873
                           OWS_NOERR, "  <description>%s</description>\n",
874
                           NULL);
875
  msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "name",
10✔
876
                           OWS_NOERR, "  <name>%s</name>\n", "MapServer WCS");
877
  msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "label",
10✔
878
                           OWS_WARN, "  <label>%s</label>\n", NULL);
879

880
  /* we are not supporting the optional keyword type, at least not yet */
881
  msOWSPrintEncodeMetadataList(
10✔
882
      stdout, &(map->web.metadata), "CO", "keywordlist", "  <keywords>\n",
883
      "  </keywords>\n", "    <keyword>%s</keyword>\n", NULL);
884

885
  msWCSGetCapabilities_Service_ResponsibleParty(map);
10✔
886

887
  msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "CO", "fees",
10✔
888
                           OWS_NOERR, "  <fees>%s</fees>\n", "NONE");
889
  msOWSPrintEncodeMetadataList(stdout, &(map->web.metadata), "CO",
10✔
890
                               "accessconstraints", "  <accessConstraints>\n",
891
                               "  </accessConstraints>\n", "    %s\n", "NONE");
892

893
  /* done */
894
  msIO_printf("</Service>\n");
10✔
895

896
  return MS_SUCCESS;
10✔
897
}
898

899
/************************************************************************/
900
/*                  msWCSGetCapabilities_Capability()                   */
901
/************************************************************************/
902

903
static int msWCSGetCapabilities_Capability(mapObj *map, wcsParamsObj *params,
10✔
904
                                           cgiRequestObj *req) {
905
  char *script_url = NULL, *script_url_encoded = NULL;
906

907
  /* we need this server's onlineresource for the request section */
908
  if ((script_url = msOWSGetOnlineResource(map, "CO", "onlineresource", req)) ==
10✔
909
          NULL ||
20✔
910
      (script_url_encoded = msEncodeHTMLEntities(script_url)) == NULL) {
10✔
911
    free(script_url);
×
912
    free(script_url_encoded);
913
    return msWCSException(map, NULL, NULL, params->version);
×
914
  }
915

916
  /* start the Capability section, only need the full start tag if this is the
917
   * only section requested */
918
  if (!params->section ||
10✔
919
      (params->section && strcasecmp(params->section, "/") == 0))
×
920
    msIO_printf("<Capability>\n");
10✔
921
  else
922
    msIO_printf("<Capability\n"
×
923
                "   version=\"%s\" \n"
924
                "   updateSequence=\"%s\" \n"
925
                "   xmlns=\"http://www.opengis.net/wcs\" \n"
926
                "   xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n"
927
                "   xmlns:gml=\"http://www.opengis.net/gml\" \n"
928
                "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
929
                "   xsi:schemaLocation=\"http://www.opengis.net/wcs "
930
                "%s/wcs/%s/wcsCapabilities.xsd\">\n",
931
                params->version, params->updatesequence,
932
                msOWSGetSchemasLocation(map), params->version);
933

934
  /* describe the types of requests the server can handle */
935
  msIO_printf("  <Request>\n");
10✔
936

937
  msWCSPrintRequestCapability("GetCapabilities", script_url_encoded);
10✔
938
  if (msOWSRequestIsEnabled(map, NULL, "C", "DescribeCoverage", MS_FALSE))
10✔
939
    msWCSPrintRequestCapability("DescribeCoverage", script_url_encoded);
10✔
940
  if (msOWSRequestIsEnabled(map, NULL, "C", "GetCoverage", MS_FALSE))
10✔
941
    msWCSPrintRequestCapability("GetCoverage", script_url_encoded);
9✔
942

943
  msIO_printf("  </Request>\n");
10✔
944

945
  /* describe the exception formats the server can produce */
946
  msIO_printf("  <Exception>\n");
10✔
947
  msIO_printf("    <Format>application/vnd.ogc.se_xml</Format>\n");
10✔
948
  msIO_printf("  </Exception>\n");
10✔
949

950
  /* describe any vendor specific capabilities */
951
  /* msIO_printf("  <VendorSpecificCapabilities />\n"); */ /* none yet */
952

953
  /* done */
954
  msIO_printf("</Capability>\n");
10✔
955

956
  free(script_url);
10✔
957
  free(script_url_encoded);
10✔
958

959
  return MS_SUCCESS;
10✔
960
}
961

962
/************************************************************************/
963
/*                    msWCSPrintMetadataLink()                          */
964
/************************************************************************/
965

966
static void msWCSPrintMetadataLink(layerObj *layer,
15✔
967
                                   const char *script_url_encoded) {
968
  const char *list =
969
      msOWSLookupMetadata(&(layer->metadata), "CO", "metadatalink_list");
15✔
970
  if (list) {
15✔
971
    int ntokens = 0;
1✔
972
    char **tokens = msStringSplit(list, ' ', &ntokens);
1✔
973
    for (int i = 0; i < ntokens; i++) {
3✔
974
      std::string key("metadatalink_");
2✔
975
      key += tokens[i];
2✔
976
      msOWSPrintURLType(
2✔
977
          stdout, &(layer->metadata), "CO", key.c_str(), OWS_NOERR,
978
          "  <metadataLink%s%s%s%s xlink:type=\"simple\"%s/>", NULL,
979
          " metadataType=\"%s\"", NULL, NULL, NULL, " xlink:href=\"%s\"",
980
          MS_FALSE, MS_FALSE, MS_FALSE, MS_FALSE, MS_TRUE, "other", NULL, NULL,
981
          NULL, NULL, NULL);
982
    }
983
    msFreeCharArray(tokens, ntokens);
1✔
984
    return;
985
  }
986

987
  /* optional metadataLink */
988
  if (!msOWSLookupMetadata(&(layer->metadata), "CO", "metadatalink_href"))
14✔
989
    msMetadataSetGetMetadataURL(layer, script_url_encoded);
7✔
990

991
  msOWSPrintURLType(stdout, &(layer->metadata), "CO", "metadatalink", OWS_NOERR,
14✔
992
                    "  <metadataLink%s%s%s%s xlink:type=\"simple\"%s/>", NULL,
993
                    " metadataType=\"%s\"", NULL, NULL, NULL,
994
                    " xlink:href=\"%s\"", MS_FALSE, MS_FALSE, MS_FALSE,
995
                    MS_FALSE, MS_TRUE, "other", NULL, NULL, NULL, NULL, NULL);
996
}
997

998
/************************************************************************/
999
/*             msWCSGetCapabilities_CoverageOfferingBrief()             */
1000
/************************************************************************/
1001

1002
static int
1003
msWCSGetCapabilities_CoverageOfferingBrief(layerObj *layer,
15✔
1004
                                           const char *script_url_encoded) {
1005
  coverageMetadataObj cm;
1006
  int status;
1007

1008
  if ((layer->status == MS_DELETE) || !msWCSIsLayerSupported(layer))
15✔
1009
    return MS_SUCCESS; /* not an error, this layer cannot be served via WCS */
4✔
1010

1011
  status = msWCSGetCoverageMetadata(layer, &cm);
11✔
1012
  if (status != MS_SUCCESS)
11✔
1013
    return MS_FAILURE;
1014

1015
  /* start the CoverageOfferingBrief section */
1016
  msIO_printf(
11✔
1017
      "  <CoverageOfferingBrief>\n"); /* is this tag right? (I hate schemas
1018
                                         without ANY examples) */
1019

1020
  msWCSPrintMetadataLink(layer, script_url_encoded);
11✔
1021

1022
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "description",
11✔
1023
                           OWS_NOERR, "    <description>%s</description>\n",
1024
                           NULL);
1025
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "name", OWS_NOERR,
11✔
1026
                           "    <name>%s</name>\n", layer->name);
11✔
1027

1028
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "label", OWS_WARN,
11✔
1029
                           "    <label>%s</label>\n", NULL);
1030

1031
  /* TODO: add elevation ranges to lonLatEnvelope (optional) */
1032
  msIO_printf(
11✔
1033
      "    <lonLatEnvelope srsName=\"urn:ogc:def:crs:OGC:1.3:CRS84\">\n");
1034
  msIO_printf("      <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.minx,
11✔
1035
              cm.llextent.miny); /* TODO: don't know if this is right */
1036
  msIO_printf("      <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.maxx,
11✔
1037
              cm.llextent.maxy);
1038

1039
  msOWSPrintEncodeMetadataList(
11✔
1040
      stdout, &(layer->metadata), "CO", "timeposition", NULL, NULL,
1041
      "      <gml:timePosition>%s</gml:timePosition>\n", NULL);
1042

1043
  msIO_printf("    </lonLatEnvelope>\n");
11✔
1044

1045
  /* we are not supporting the optional keyword type, at least not yet */
1046
  msOWSPrintEncodeMetadataList(stdout, &(layer->metadata), "CO", "keywordlist",
11✔
1047
                               "  <keywords>\n", "  </keywords>\n",
1048
                               "    <keyword>%s</keyword>\n", NULL);
1049

1050
  /* done */
1051
  msIO_printf("  </CoverageOfferingBrief>\n");
11✔
1052

1053
  msWCSFreeCoverageMetadata(&cm);
11✔
1054

1055
  return MS_SUCCESS;
1056
}
1057

1058
/************************************************************************/
1059
/*                msWCSGetCapabilities_ContentMetadata()                */
1060
/************************************************************************/
1061

1062
static int msWCSGetCapabilities_ContentMetadata(mapObj *map,
10✔
1063
                                                wcsParamsObj *params,
1064
                                                owsRequestObj *ows_request,
1065
                                                cgiRequestObj *req) {
1066
  int i;
1067
  char *script_url_encoded = NULL;
1068

1069
  {
1070
    char *pszTmp = msOWSGetOnlineResource(map, "CO", "onlineresource", req);
10✔
1071
    script_url_encoded = msEncodeHTMLEntities(pszTmp);
10✔
1072
    msFree(pszTmp);
10✔
1073
  }
1074

1075
  /* start the ContentMetadata section, only need the full start tag if this is
1076
   * the only section requested */
1077
  /* TODO: add Xlink attributes for other sources of this information  */
1078
  if (!params->section ||
10✔
1079
      (params->section && strcasecmp(params->section, "/") == 0))
×
1080
    msIO_printf("<ContentMetadata>\n");
10✔
1081
  else
1082
    msIO_printf("<ContentMetadata\n"
×
1083
                "   version=\"%s\" \n"
1084
                "   updateSequence=\"%s\" \n"
1085
                "   xmlns=\"http://www.opengis.net/wcs\" \n"
1086
                "   xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n"
1087
                "   xmlns:gml=\"http://www.opengis.net/gml\" \n"
1088
                "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1089
                "   xsi:schemaLocation=\"http://www.opengis.net/wcs "
1090
                "%s/wcs/%s/wcsCapabilities.xsd\">\n",
1091
                params->version, params->updatesequence,
1092
                msOWSGetSchemasLocation(map), params->version);
1093

1094
  if (ows_request->numlayers == 0) {
10✔
1095
    msIO_printf("  <!-- WARNING: No WCS layers are enabled. Check "
1✔
1096
                "wcs/ows_enable_request settings. -->\n");
1097
  } else {
1098
    for (i = 0; i < map->numlayers; i++) {
24✔
1099
      if (!msIntegerInArray(GET_LAYER(map, i)->index,
15✔
1100
                            ows_request->enabled_layers,
1101
                            ows_request->numlayers))
1102
        continue;
×
1103

1104
      if (msWCSGetCapabilities_CoverageOfferingBrief(
15✔
1105
              (GET_LAYER(map, i)), script_url_encoded) != MS_SUCCESS) {
15✔
1106
        msIO_printf("  <!-- WARNING: There was a problem with one of layers. "
×
1107
                    "See server log for details. -->\n");
1108
      }
1109
    }
1110
  }
1111

1112
  msFree(script_url_encoded);
10✔
1113

1114
  /* done */
1115
  msIO_printf("</ContentMetadata>\n");
10✔
1116

1117
  return MS_SUCCESS;
10✔
1118
}
1119

1120
/************************************************************************/
1121
/*                        msWCSGetCapabilities()                        */
1122
/************************************************************************/
1123

1124
static int msWCSGetCapabilities(mapObj *map, wcsParamsObj *params,
31✔
1125
                                cgiRequestObj *req,
1126
                                owsRequestObj *ows_request) {
1127
  char tmpString[OWS_VERSION_MAXLEN];
1128
  int i, tmpInt = 0;
1129
  int wcsSupportedVersions[] = {OWS_1_1_2, OWS_1_1_1, OWS_1_1_0, OWS_1_0_0};
31✔
1130
  int wcsNumSupportedVersions = 4;
1131
  const char *updatesequence = NULL;
1132

1133
  /* check version is valid */
1134
  tmpInt = msOWSParseVersionString(params->version);
31✔
1135
  if (tmpInt == OWS_VERSION_BADFORMAT) {
31✔
1136
    return msWCSException(map, "InvalidParameterValue", "version", "1.0.0 ");
×
1137
  }
1138

1139
  /* negotiate version */
1140
  tmpInt = msOWSNegotiateVersion(tmpInt, wcsSupportedVersions,
31✔
1141
                                 wcsNumSupportedVersions);
1142

1143
  /* set result as string and carry on */
1144
  free(params->version);
31✔
1145
  params->version = msStrdup(msOWSGetVersionString(tmpInt, tmpString));
31✔
1146

1147
  /* -------------------------------------------------------------------- */
1148
  /*      1.1.x is sufficiently different we have a whole case for        */
1149
  /*      it.  The remainder of this function is for 1.0.0.               */
1150
  /* -------------------------------------------------------------------- */
1151
  if (strncmp(params->version, "1.1", 3) == 0)
31✔
1152
    return msWCSGetCapabilities11(map, params, req, ows_request);
18✔
1153

1154
  updatesequence =
1155
      msOWSLookupMetadata(&(map->web.metadata), "CO", "updatesequence");
13✔
1156

1157
  if (params->updatesequence != NULL) {
13✔
1158
    i = msOWSNegotiateUpdateSequence(params->updatesequence, updatesequence);
3✔
1159
    if (i == 0) { /* current */
3✔
1160
      msSetError(
1✔
1161
          MS_WCSERR, "UPDATESEQUENCE parameter (%s) is equal to server (%s)",
1162
          "msWCSGetCapabilities()", params->updatesequence, updatesequence);
1163
      return msWCSException(map, "CurrentUpdateSequence", "updatesequence",
1✔
1164
                            params->version);
1✔
1165
    }
1166
    if (i > 0) { /* invalid */
2✔
1167
      msSetError(
1✔
1168
          MS_WCSERR, "UPDATESEQUENCE parameter (%s) is higher than server (%s)",
1169
          "msWCSGetCapabilities()", params->updatesequence, updatesequence);
1170
      return msWCSException(map, "InvalidUpdateSequence", "updatesequence",
1✔
1171
                            params->version);
1✔
1172
    }
1173
  }
1174

1175
  else { /* set default updatesequence */
1176
    if (!updatesequence)
10✔
1177
      updatesequence = "0";
1178
    params->updatesequence = msStrdup(updatesequence);
10✔
1179
  }
1180

1181
  /* if a bum section param is passed, throw exception */
1182
  if (params->section &&
11✔
1183
      strcasecmp(params->section, "/WCS_Capabilities/Service") != 0 &&
1✔
1184
      strcasecmp(params->section, "/WCS_Capabilities/Capability") != 0 &&
1✔
1185
      strcasecmp(params->section, "/WCS_Capabilities/ContentMetadata") != 0 &&
1✔
1186
      strcasecmp(params->section, "/") != 0) {
1✔
1187
    msSetError(MS_WCSERR, "Invalid SECTION parameter \"%s\"",
1✔
1188
               "msWCSGetCapabilities()", params->section);
1189
    return msWCSException(map, "InvalidParameterValue", "section",
1✔
1190
                          params->version);
1✔
1191
  }
1192

1193
  else {
1194
    msIO_setHeader("Content-Type", "text/xml; charset=UTF-8");
10✔
1195
    msIO_sendHeaders();
10✔
1196

1197
    /* print common capability elements  */
1198
    /* TODO: DocType? */
1199

1200
    if (!updatesequence)
10✔
1201
      updatesequence = "0";
1202

1203
    msIO_printf(
10✔
1204
        "<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
1205

1206
    if (!params->section ||
10✔
1207
        (params->section && strcasecmp(params->section, "/") == 0))
×
1208
      msIO_printf("<WCS_Capabilities\n"
10✔
1209
                  "   version=\"%s\" \n"
1210
                  "   updateSequence=\"%s\" \n"
1211
                  "   xmlns=\"http://www.opengis.net/wcs\" \n"
1212
                  "   xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n"
1213
                  "   xmlns:gml=\"http://www.opengis.net/gml\" \n"
1214
                  "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1215
                  "   xsi:schemaLocation=\"http://www.opengis.net/wcs "
1216
                  "%s/wcs/%s/wcsCapabilities.xsd\">\n",
1217
                  params->version, updatesequence, msOWSGetSchemasLocation(map),
1218
                  params->version);
1219

1220
    /* print the various capability sections */
1221
    if (!params->section ||
10✔
1222
        strcasecmp(params->section, "/WCS_Capabilities/Service") == 0)
×
1223
      msWCSGetCapabilities_Service(map, params);
10✔
1224

1225
    if (!params->section ||
10✔
1226
        strcasecmp(params->section, "/WCS_Capabilities/Capability") == 0)
×
1227
      msWCSGetCapabilities_Capability(map, params, req);
10✔
1228

1229
    if (!params->section ||
10✔
1230
        strcasecmp(params->section, "/WCS_Capabilities/ContentMetadata") == 0)
×
1231
      msWCSGetCapabilities_ContentMetadata(map, params, ows_request, req);
10✔
1232

1233
    if (params->section && strcasecmp(params->section, "/") == 0) {
10✔
1234
      msWCSGetCapabilities_Service(map, params);
×
1235
      msWCSGetCapabilities_Capability(map, params, req);
×
1236
      msWCSGetCapabilities_ContentMetadata(map, params, ows_request, req);
×
1237
    }
1238

1239
    /* done */
1240
    if (!params->section ||
10✔
1241
        (params->section && strcasecmp(params->section, "/") == 0))
×
1242
      msIO_printf("</WCS_Capabilities>\n");
10✔
1243
  }
1244

1245
  return MS_SUCCESS;
1246
}
1247

1248
/************************************************************************/
1249
/*               msWCSDescribeCoverage_AxisDescription()                */
1250
/************************************************************************/
1251

1252
static int msWCSDescribeCoverage_AxisDescription(layerObj *layer, char *name) {
2✔
1253
  const char *value;
1254
  char tag[100]; /* should be plenty of space */
1255

1256
  msIO_printf("        <axisDescription>\n");
2✔
1257
  msIO_printf("          <AxisDescription");
2✔
1258
  snprintf(tag, sizeof(tag), "%s_semantic",
1259
           name); /* optional attributes follow (should escape?) */
1260
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1261
                           " semantic=\"%s\"", NULL);
1262
  snprintf(tag, sizeof(tag), "%s_refsys", name);
1263
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1264
                           " refSys=\"%s\"", NULL);
1265
  snprintf(tag, sizeof(tag), "%s_refsyslabel", name);
1266
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1267
                           " refSysLabel=\"%s\"", NULL);
1268
  msIO_printf(">\n");
2✔
1269

1270
  /* TODO: add metadataLink (optional) */
1271

1272
  snprintf(tag, sizeof(tag), "%s_description", name);
1273
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1274
                           "            <description>%s</description>\n", NULL);
1275
  /* snprintf(tag, sizeof(tag), "%s_name", name); */
1276
  /* msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_WARN, "
1277
   * <name>%s</name>\n", NULL); */
1278
  msIO_printf("            <name>%s</name>\n", name);
2✔
1279

1280
  snprintf(tag, sizeof(tag), "%s_label", name);
1281
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_WARN,
2✔
1282
                           "            <label>%s</label>\n", NULL);
1283

1284
  /* Values */
1285
  msIO_printf("            <values");
2✔
1286
  snprintf(tag, sizeof(tag), "%s_values_semantic",
1287
           name); /* optional attributes follow (should escape?) */
1288
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1289
                           " semantic=\"%s\"", NULL);
1290
  snprintf(tag, sizeof(tag), "%s_values_type", name);
1291
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", tag, OWS_NOERR,
2✔
1292
                           " type=\"%s\"", NULL);
1293
  msIO_printf(">\n");
2✔
1294

1295
  /* single values, we do not support optional type and semantic attributes */
1296
  snprintf(tag, sizeof(tag), "%s_values", name);
1297
  if (msOWSLookupMetadata(&(layer->metadata), "CO", tag))
2✔
1298
    msOWSPrintEncodeMetadataList(
2✔
1299
        stdout, &(layer->metadata), "CO", tag, NULL, NULL,
1300
        "              <singleValue>%s</singleValue>\n", NULL);
1301

1302
  /* intervals, only one per axis for now, we do not support optional type,
1303
   * atomic and semantic attributes */
1304
  snprintf(tag, sizeof(tag), "%s_interval", name);
1305
  if ((value = msOWSLookupMetadata(&(layer->metadata), "CO", tag)) != NULL) {
2✔
1306
    int numtokens = 0;
×
1307
    char **tokens = msStringSplit(value, '/', &numtokens);
×
1308
    if (tokens && numtokens > 0) {
×
1309
      msIO_printf("            <interval>\n");
×
1310
      if (numtokens >= 1)
×
1311
        msIO_printf("            <min>%s</min>\n",
×
1312
                    tokens[0]); /* TODO: handle closure */
1313
      if (numtokens >= 2)
×
1314
        msIO_printf("            <max>%s</max>\n", tokens[1]);
×
1315
      if (numtokens >= 3)
×
1316
        msIO_printf("            <res>%s</res>\n", tokens[2]);
×
1317
      msIO_printf("            </interval>\n");
×
1318
    }
1319
    msFreeCharArray(tokens, numtokens);
×
1320
  }
1321

1322
  /* TODO: add default (optional) */
1323

1324
  msIO_printf("            </values>\n");
2✔
1325

1326
  msIO_printf("          </AxisDescription>\n");
2✔
1327
  msIO_printf("        </axisDescription>\n");
2✔
1328

1329
  return MS_SUCCESS;
2✔
1330
}
1331

1332
/************************************************************************/
1333
/*               msWCSDescribeCoverage_CoverageOffering()               */
1334
/************************************************************************/
1335

1336
static int msWCSDescribeCoverage_CoverageOffering(layerObj *layer,
4✔
1337
                                                  wcsParamsObj *params,
1338
                                                  char *script_url_encoded) {
1339
  const char *value;
1340
  char *epsg_buf, *encoded_format;
1341
  coverageMetadataObj cm;
1342
  int i, status;
1343

1344
  if (msCheckParentPointer(layer->map, "map") == MS_FAILURE)
4✔
1345
    return MS_FAILURE;
1346

1347
  if (!msWCSIsLayerSupported(layer))
4✔
1348
    return MS_SUCCESS; /* not an error, this layer cannot be served via WCS */
1349

1350
  status = msWCSGetCoverageMetadata(layer, &cm);
4✔
1351
  if (status != MS_SUCCESS)
4✔
1352
    return MS_FAILURE;
1353

1354
  /* fill in bands rangeset info, if required.  */
1355
  msWCSSetDefaultBandsRangeSetInfo(params, &cm, layer);
4✔
1356

1357
  /* start the Coverage section */
1358
  msIO_printf("  <CoverageOffering>\n");
4✔
1359

1360
  msWCSPrintMetadataLink(layer, script_url_encoded);
4✔
1361

1362
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "description",
4✔
1363
                           OWS_NOERR, "  <description>%s</description>\n",
1364
                           NULL);
1365
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "name", OWS_NOERR,
4✔
1366
                           "  <name>%s</name>\n", layer->name);
4✔
1367

1368
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "label", OWS_WARN,
4✔
1369
                           "  <label>%s</label>\n", NULL);
1370

1371
  /* TODO: add elevation ranges to lonLatEnvelope (optional) */
1372
  msIO_printf(
4✔
1373
      "    <lonLatEnvelope srsName=\"urn:ogc:def:crs:OGC:1.3:CRS84\">\n");
1374
  msIO_printf("      <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.minx,
4✔
1375
              cm.llextent.miny);
1376
  msIO_printf("      <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.maxx,
4✔
1377
              cm.llextent.maxy);
1378

1379
  msOWSPrintEncodeMetadataList(
4✔
1380
      stdout, &(layer->metadata), "CO", "timeposition", NULL, NULL,
1381
      "      <gml:timePosition>%s</gml:timePosition>\n", NULL);
1382

1383
  msIO_printf("    </lonLatEnvelope>\n");
4✔
1384

1385
  /* we are not supporting the optional keyword type, at least not yet */
1386
  msOWSPrintEncodeMetadataList(stdout, &(layer->metadata), "CO", "keywordlist",
4✔
1387
                               "  <keywords>\n", "  </keywords>\n",
1388
                               "    <keyword>%s</keyword>\n", NULL);
1389

1390
  /* DomainSet: starting simple, just a spatial domain (gml:envelope) and
1391
   * optionally a temporal domain */
1392
  msIO_printf("    <domainSet>\n");
4✔
1393

1394
  /* SpatialDomain */
1395
  msIO_printf("      <spatialDomain>\n");
4✔
1396

1397
  /* envelope in lat/lon */
1398
  msIO_printf("        <gml:Envelope srsName=\"EPSG:4326\">\n");
4✔
1399
  msIO_printf("          <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.minx,
4✔
1400
              cm.llextent.miny);
1401
  msIO_printf("          <gml:pos>%.15g %.15g</gml:pos>\n", cm.llextent.maxx,
4✔
1402
              cm.llextent.maxy);
1403
  msIO_printf("        </gml:Envelope>\n");
4✔
1404

1405
  /* envelope in the native srs */
1406
  msOWSGetEPSGProj(&(layer->projection), &(layer->metadata), "CO", MS_TRUE,
4✔
1407
                   &epsg_buf);
1408
  if (!epsg_buf) {
4✔
1409
    msOWSGetEPSGProj(&(layer->map->projection), &(layer->map->web.metadata),
×
1410
                     "CO", MS_TRUE, &epsg_buf);
1411
  }
1412
  if (epsg_buf) {
4✔
1413
    msIO_printf("        <gml:Envelope srsName=\"%s\">\n", epsg_buf);
4✔
1414
    msFree(epsg_buf);
4✔
1415
  } else {
1416
    msIO_printf("        <!-- NativeCRSs ERROR: missing required information, "
×
1417
                "no SRSs defined -->\n");
1418
  }
1419
  msIO_printf("          <gml:pos>%.15g %.15g</gml:pos>\n", cm.extent.minx,
4✔
1420
              cm.extent.miny);
1421
  msIO_printf("          <gml:pos>%.15g %.15g</gml:pos>\n", cm.extent.maxx,
4✔
1422
              cm.extent.maxy);
1423
  msIO_printf("        </gml:Envelope>\n");
4✔
1424

1425
  /* gml:rectifiedGrid */
1426
  msIO_printf("        <gml:RectifiedGrid dimension=\"2\">\n");
4✔
1427
  msIO_printf("          <gml:limits>\n");
4✔
1428
  msIO_printf("            <gml:GridEnvelope>\n");
4✔
1429
  msIO_printf("              <gml:low>0 0</gml:low>\n");
4✔
1430
  msIO_printf("              <gml:high>%d %d</gml:high>\n", cm.xsize - 1,
4✔
1431
              cm.ysize - 1);
4✔
1432
  msIO_printf("            </gml:GridEnvelope>\n");
4✔
1433
  msIO_printf("          </gml:limits>\n");
4✔
1434
  msIO_printf("          <gml:axisName>x</gml:axisName>\n");
4✔
1435
  msIO_printf("          <gml:axisName>y</gml:axisName>\n");
4✔
1436
  msIO_printf("          <gml:origin>\n");
4✔
1437
  msIO_printf("            <gml:pos>%.15g %.15g</gml:pos>\n",
4✔
1438
              cm.geotransform[0], cm.geotransform[3]);
1439
  msIO_printf("          </gml:origin>\n");
4✔
1440
  msIO_printf("          <gml:offsetVector>%.15g %.15g</gml:offsetVector>\n",
4✔
1441
              cm.geotransform[1],
1442
              cm.geotransform[2]); /* offset vector in X direction */
1443
  msIO_printf("          <gml:offsetVector>%.15g %.15g</gml:offsetVector>\n",
4✔
1444
              cm.geotransform[4],
1445
              cm.geotransform[5]); /* offset vector in Y direction */
1446
  msIO_printf("        </gml:RectifiedGrid>\n");
4✔
1447

1448
  msIO_printf("      </spatialDomain>\n");
4✔
1449

1450
  msWCSFreeCoverageMetadata(&cm);
4✔
1451

1452
  /* TemporalDomain */
1453

1454
  /* TODO: figure out when a temporal domain is valid, for example only tiled
1455
   * rasters support time as a domain, plus we need a timeitem */
1456
  if (msOWSLookupMetadata(&(layer->metadata), "CO", "timeposition") ||
7✔
1457
      msOWSLookupMetadata(&(layer->metadata), "CO", "timeperiod")) {
3✔
1458
    msIO_printf("      <temporalDomain>\n");
1✔
1459

1460
    /* TimePosition (should support a value AUTO, then we could mine positions
1461
     * from the timeitem) */
1462
    msOWSPrintEncodeMetadataList(
1✔
1463
        stdout, &(layer->metadata), "CO", "timeposition", NULL, NULL,
1464
        "        <gml:timePosition>%s</gml:timePosition>\n", NULL);
1465

1466
    /* TODO:  add TimePeriod (only one per layer)  */
1467

1468
    msIO_printf("      </temporalDomain>\n");
1✔
1469
  }
1470

1471
  msIO_printf("    </domainSet>\n");
4✔
1472

1473
  /* rangeSet */
1474
  msIO_printf("    <rangeSet>\n");
4✔
1475
  msIO_printf(
4✔
1476
      "      <RangeSet>\n"); /* TODO: there are some optional attributes */
1477

1478
  /* TODO: add metadataLink (optional) */
1479

1480
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO",
4✔
1481
                           "rangeset_description", OWS_NOERR,
1482
                           "        <description>%s</description>\n", NULL);
1483
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "rangeset_name",
4✔
1484
                           OWS_WARN, "        <name>%s</name>\n", NULL);
1485

1486
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "rangeset_label",
4✔
1487
                           OWS_WARN, "        <label>%s</label>\n", NULL);
1488

1489
  /* compound range sets */
1490
  if ((value = msOWSLookupMetadata(&(layer->metadata), "CO",
4✔
1491
                                   "rangeset_axes")) != NULL) {
1492
    int numtokens = 0;
2✔
1493
    char **tokens = msStringSplit(value, ',', &numtokens);
2✔
1494
    if (tokens && numtokens > 0) {
2✔
1495
      for (i = 0; i < numtokens; i++)
4✔
1496
        msWCSDescribeCoverage_AxisDescription(layer, tokens[i]);
2✔
1497
    }
1498
    msFreeCharArray(tokens, numtokens);
2✔
1499
  }
1500

1501
  if ((value = msOWSLookupMetadata(&(layer->metadata), "CO",
4✔
1502
                                   "rangeset_nullvalue")) != NULL) {
1503
    msIO_printf("        <nullValues>\n");
3✔
1504
    msIO_printf("          <singleValue>%s</singleValue>\n", value);
3✔
1505
    msIO_printf("        </nullValues>\n");
3✔
1506
  }
1507

1508
  msIO_printf("      </RangeSet>\n");
4✔
1509
  msIO_printf("    </rangeSet>\n");
4✔
1510

1511
  /* supportedCRSs */
1512
  msIO_printf("    <supportedCRSs>\n");
4✔
1513

1514
  /* requestResponseCRSs: check the layer metadata/projection, and then the map
1515
   * metadata/projection if necessary (should never get to the error message) */
1516
  msOWSGetEPSGProj(&(layer->projection), &(layer->metadata), "CO", MS_FALSE,
4✔
1517
                   &epsg_buf);
1518
  if (!epsg_buf) {
4✔
1519
    msOWSGetEPSGProj(&(layer->map->projection), &(layer->map->web.metadata),
×
1520
                     "CO", MS_FALSE, &epsg_buf);
1521
  }
1522
  if (epsg_buf) {
4✔
1523
    int numtokens = 0;
4✔
1524
    char **tokens = msStringSplit(epsg_buf, ' ', &numtokens);
4✔
1525
    if (tokens && numtokens > 0) {
4✔
1526
      for (i = 0; i < numtokens; i++)
10✔
1527
        msIO_printf("      <requestResponseCRSs>%s</requestResponseCRSs>\n",
6✔
1528
                    tokens[i]);
6✔
1529
    }
1530
    msFreeCharArray(tokens, numtokens);
4✔
1531
    msFree(epsg_buf);
4✔
1532
  } else {
1533
    msIO_printf("      <!-- requestResponseCRSs ERROR: missing required "
×
1534
                "information, no SRSs defined -->\n");
1535
  }
1536

1537
  /* nativeCRSs (only one in our case) */
1538
  msOWSGetEPSGProj(&(layer->projection), &(layer->metadata), "CO", MS_TRUE,
4✔
1539
                   &epsg_buf);
1540
  if (!epsg_buf) {
4✔
1541
    msOWSGetEPSGProj(&(layer->map->projection), &(layer->map->web.metadata),
×
1542
                     "CO", MS_TRUE, &epsg_buf);
1543
  }
1544
  if (epsg_buf) {
4✔
1545
    msIO_printf("      <nativeCRSs>%s</nativeCRSs>\n", epsg_buf);
4✔
1546
    msFree(epsg_buf);
4✔
1547
  } else {
1548
    msIO_printf("      <!-- nativeCRSs ERROR: missing required information, no "
×
1549
                "SRSs defined -->\n");
1550
  }
1551

1552
  msIO_printf("    </supportedCRSs>\n");
4✔
1553

1554
  /* supportedFormats */
1555
  msIO_printf("    <supportedFormats");
4✔
1556
  msOWSPrintEncodeMetadata(stdout, &(layer->metadata), "CO", "nativeformat",
4✔
1557
                           OWS_NOERR, " nativeFormat=\"%s\"", NULL);
1558
  msIO_printf(">\n");
4✔
1559

1560
  if ((encoded_format = msOWSGetEncodeMetadata(&(layer->metadata), "CO",
4✔
1561
                                               "formats", "GTiff")) != NULL) {
1562
    int numtokens = 0;
4✔
1563
    char **tokens = msStringSplit(encoded_format, ' ', &numtokens);
4✔
1564
    if (tokens && numtokens > 0) {
4✔
1565
      for (i = 0; i < numtokens; i++)
12✔
1566
        msIO_printf("      <formats>%s</formats>\n", tokens[i]);
8✔
1567
    }
1568
    msFreeCharArray(tokens, numtokens);
4✔
1569
    msFree(encoded_format);
4✔
1570
  }
1571
  msIO_printf("    </supportedFormats>\n");
4✔
1572

1573
  msIO_printf("    <supportedInterpolations default=\"nearest neighbor\">\n");
4✔
1574
  msIO_printf(
4✔
1575
      "      <interpolationMethod>nearest neighbor</interpolationMethod>\n");
1576
  msIO_printf("      <interpolationMethod>bilinear</interpolationMethod>\n");
4✔
1577
  /*  msIO_printf("      <interpolationMethod>bicubic</interpolationMethod>\n"
1578
   * ); */
1579
  msIO_printf("    </supportedInterpolations>\n");
4✔
1580

1581
  /* done */
1582
  msIO_printf("  </CoverageOffering>\n");
4✔
1583

1584
  return MS_SUCCESS;
1585
}
1586

1587
/************************************************************************/
1588
/*                       msWCSDescribeCoverage()                        */
1589
/************************************************************************/
1590

1591
static int msWCSDescribeCoverage(mapObj *map, wcsParamsObj *params,
6✔
1592
                                 owsRequestObj *ows_request,
1593
                                 cgiRequestObj *req) {
1594
  int i = 0, j = 0, k = 0;
1595
  const char *updatesequence = NULL;
1596
  char **coverages = NULL;
1597
  int numcoverages = 0;
6✔
1598

1599
  char *coverageName = NULL;
1600
  char *script_url_encoded = NULL;
1601

1602
  /* -------------------------------------------------------------------- */
1603
  /*      1.1.x is sufficiently different we have a whole case for        */
1604
  /*      it.  The remainder of this function is for 1.0.0.               */
1605
  /* -------------------------------------------------------------------- */
1606
  if (strncmp(params->version, "1.1", 3) == 0)
6✔
1607
    return msWCSDescribeCoverage11(map, params, ows_request);
2✔
1608

1609
  /* -------------------------------------------------------------------- */
1610
  /*      Process 1.0.0...                                                */
1611
  /* -------------------------------------------------------------------- */
1612

1613
  if (params->coverages) { /* use the list, but validate it first */
4✔
1614
    for (j = 0; params->coverages[j]; j++) {
8✔
1615
      coverages = msStringSplit(params->coverages[j], ',', &numcoverages);
4✔
1616
      for (k = 0; k < numcoverages; k++) {
8✔
1617

1618
        for (i = 0; i < map->numlayers; i++) {
5✔
1619
          coverageName =
1620
              msOWSGetEncodeMetadata(&(GET_LAYER(map, i)->metadata), "CO",
5✔
1621
                                     "name", GET_LAYER(map, i)->name);
5✔
1622
          if (coverageName != NULL && EQUAL(coverageName, coverages[k]) &&
9✔
1623
              (msIntegerInArray(GET_LAYER(map, i)->index,
4✔
1624
                                ows_request->enabled_layers,
1625
                                ows_request->numlayers))) {
1626
            msFree(coverageName);
4✔
1627
            break;
4✔
1628
          }
1629
          msFree(coverageName);
1✔
1630
        }
1631

1632
        /* i = msGetLayerIndex(map, coverages[k]); */
1633
        if (i == map->numlayers) { /* coverage not found */
4✔
1634
          msSetError(
×
1635
              MS_WCSERR,
1636
              "COVERAGE %s cannot be opened / does not exist. A layer might be disabled for \
1637
this request. Check wcs/ows_enable_request settings.",
1638
              "msWCSDescribeCoverage()", coverages[k]);
×
1639
          return msWCSException(map, "CoverageNotDefined", "coverage",
×
1640
                                params->version);
×
1641
        }
1642
      } /* next coverage */
1643
      msFreeCharArray(coverages, numcoverages);
4✔
1644
    }
1645
  }
1646

1647
  updatesequence =
1648
      msOWSLookupMetadata(&(map->web.metadata), "CO", "updatesequence");
4✔
1649
  if (!updatesequence)
4✔
1650
    updatesequence = "0";
1651

1652
  /* printf("Content-Type: application/vnd.ogc.se_xml%c%c",10,10); */
1653
  msIO_setHeader("Content-Type", "text/xml; charset=UTF-8");
4✔
1654
  msIO_sendHeaders();
4✔
1655

1656
  /* print common capability elements  */
1657
  msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" ?>\n");
4✔
1658

1659
  /* start the DescribeCoverage section */
1660
  msIO_printf("<CoverageDescription\n"
4✔
1661
              "   version=\"%s\" \n"
1662
              "   updateSequence=\"%s\" \n"
1663
              "   xmlns=\"http://www.opengis.net/wcs\" \n"
1664
              "   xmlns:xlink=\"http://www.w3.org/1999/xlink\" \n"
1665
              "   xmlns:gml=\"http://www.opengis.net/gml\" \n"
1666
              "   xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
1667
              "   xsi:schemaLocation=\"http://www.opengis.net/wcs "
1668
              "%s/wcs/%s/describeCoverage.xsd\">\n",
1669
              params->version, updatesequence, msOWSGetSchemasLocation(map),
1670
              params->version);
1671

1672
  {
1673
    char *pszTmp = msOWSGetOnlineResource(map, "CO", "onlineresource", req);
4✔
1674
    script_url_encoded = msEncodeHTMLEntities(pszTmp);
4✔
1675
    msFree(pszTmp);
4✔
1676
  }
1677

1678
  if (params->coverages) { /* use the list */
4✔
1679
    for (j = 0; params->coverages[j]; j++) {
8✔
1680
      coverages = msStringSplit(params->coverages[j], ',', &numcoverages);
4✔
1681
      for (k = 0; k < numcoverages; k++) {
8✔
1682
        for (i = 0; i < map->numlayers; i++) {
5✔
1683
          coverageName =
1684
              msOWSGetEncodeMetadata(&(GET_LAYER(map, i)->metadata), "CO",
5✔
1685
                                     "name", GET_LAYER(map, i)->name);
5✔
1686
          if (coverageName != NULL && EQUAL(coverageName, coverages[k])) {
5✔
1687
            msFree(coverageName);
4✔
1688
            break;
4✔
1689
          }
1690
          msFree(coverageName);
1✔
1691
        }
1692
        msWCSDescribeCoverage_CoverageOffering((GET_LAYER(map, i)), params,
4✔
1693
                                               script_url_encoded);
1694
      }
1695
      msFreeCharArray(coverages, numcoverages);
4✔
1696
    }
1697
  } else { /* return all layers */
1698
    for (i = 0; i < map->numlayers; i++) {
×
1699
      if (!msIntegerInArray(GET_LAYER(map, i)->index,
×
1700
                            ows_request->enabled_layers,
1701
                            ows_request->numlayers))
1702
        continue;
×
1703

1704
      msWCSDescribeCoverage_CoverageOffering((GET_LAYER(map, i)), params,
×
1705
                                             script_url_encoded);
1706
    }
1707
  }
1708

1709
  msFree(script_url_encoded);
4✔
1710

1711
  /* done */
1712
  msIO_printf("</CoverageDescription>\n");
4✔
1713

1714
  return MS_SUCCESS;
1715
}
1716

1717
/************************************************************************/
1718
/*                       msWCSGetCoverageBands10()                      */
1719
/************************************************************************/
1720

1721
static int msWCSGetCoverageBands10(mapObj *map, cgiRequestObj *request,
22✔
1722
                                   wcsParamsObj *params, layerObj *lp,
1723
                                   char **p_bandlist)
1724

1725
{
1726
  const char *value = NULL;
1727
  int i;
1728

1729
  /* Are there any non-spatio/temporal ranges to do subsetting on (e.g. bands)
1730
   */
1731
  value = msOWSLookupMetadata(
22✔
1732
      &(lp->metadata), "CO",
1733
      "rangeset_axes"); /* this will get all the compound range sets */
1734
  if (value) {
22✔
1735
    char **tokens;
1736
    int numtokens;
1737
    char tag[100];
1738
    const char *rangeitem;
1739

1740
    tokens = msStringSplit(value, ',', &numtokens);
19✔
1741

1742
    for (i = 0; i < numtokens; i++) {
37✔
1743
      if ((value = msWCSGetRequestParameter(request, tokens[i])) == NULL)
19✔
1744
        continue; /* next rangeset parameter */
17✔
1745

1746
      /* ok, a parameter has been passed which matches a token in
1747
       * wcs_rangeset_axes */
1748
      if (msWCSValidateRangeSetParam(lp, tokens[i], value) != MS_SUCCESS) {
2✔
1749
        int ret;
1750
        msSetError(MS_WCSERR, "Error specifying \"%s\" parameter value(s).",
1✔
1751
                   "msWCSGetCoverage()", tokens[i]);
1752
        ret = msWCSException(map, "InvalidParameterValue", tokens[i],
1✔
1753
                             params->version);
1✔
1754
        msFreeCharArray(tokens, numtokens);
1✔
1755
        return ret;
1✔
1756
      }
1757

1758
      /* xxxxx_rangeitem tells us how to subset */
1759
      snprintf(tag, sizeof(tag), "%s_rangeitem", tokens[i]);
1✔
1760
      if ((rangeitem = msOWSLookupMetadata(&(lp->metadata), "CO", tag)) ==
1✔
1761
          NULL) {
1762
        msSetError(MS_WCSERR,
×
1763
                   "Missing required metadata element \"%s\", unable to "
1764
                   "process %s=%s.",
1765
                   "msWCSGetCoverage()", tag, tokens[i], value);
1766
        msFreeCharArray(tokens, numtokens);
×
1767
        return msWCSException(map, NULL, NULL, params->version);
×
1768
      }
1769

1770
      if (strcasecmp(rangeitem, "_bands") ==
1✔
1771
          0) { /* special case, subset bands */
1772
        *p_bandlist = msWCSConvertRangeSetToString(value);
1✔
1773

1774
        if (!*p_bandlist) {
1✔
1775
          msSetError(MS_WCSERR, "Error specifying \"%s\" parameter value(s).",
×
1776
                     "msWCSGetCoverage()", tokens[i]);
1777
          msFreeCharArray(tokens, numtokens);
×
1778
          return msWCSException(map, NULL, NULL, params->version);
×
1779
        }
1780
      } else if (strcasecmp(rangeitem, "_pixels") ==
×
1781
                 0) { /* special case, subset pixels */
1782
        msFreeCharArray(tokens, numtokens);
×
1783
        msSetError(
×
1784
            MS_WCSERR,
1785
            "Arbitrary range sets based on pixel values are not yet supported.",
1786
            "msWCSGetCoverage()");
1787
        return msWCSException(map, NULL, NULL, params->version);
×
1788
      } else {
1789
        msFreeCharArray(tokens, numtokens);
×
1790
        msSetError(MS_WCSERR,
×
1791
                   "Arbitrary range sets based on tile (i.e. image) attributes "
1792
                   "are not yet supported.",
1793
                   "msWCSGetCoverage()");
1794
        return msWCSException(map, NULL, NULL, params->version);
×
1795
      }
1796
    }
1797
    /* clean-up */
1798
    msFreeCharArray(tokens, numtokens);
18✔
1799
  }
1800

1801
  return MS_SUCCESS;
1802
}
1803

1804
/************************************************************************/
1805
/*                   msWCSGetCoverage_ImageCRSSetup()                   */
1806
/*                                                                      */
1807
/*      The request was in imageCRS - update the map projection to      */
1808
/*      map the native projection of the layer, and reset the           */
1809
/*      bounding box to match the projected bounds corresponding to     */
1810
/*      the imageCRS request.                                           */
1811
/************************************************************************/
1812

1813
static int msWCSGetCoverage_ImageCRSSetup(mapObj *map, wcsParamsObj *params,
3✔
1814
                                          coverageMetadataObj *cm,
1815
                                          layerObj *layer)
1816

1817
{
1818
  /* -------------------------------------------------------------------- */
1819
  /*      Load map with the layer (coverage) coordinate system.  We       */
1820
  /*      really need a set projectionObj from projectionObj function!    */
1821
  /* -------------------------------------------------------------------- */
1822
  char *layer_proj = msGetProjectionString(&(layer->projection));
3✔
1823

1824
  if (msLoadProjectionString(&(map->projection), layer_proj) != 0) {
3✔
1825
    msFree(layer_proj);
×
1826
    return msWCSException(map, NULL, NULL, params->version);
×
1827
  }
1828

1829
  free(layer_proj);
3✔
1830
  layer_proj = NULL;
1831

1832
  /* -------------------------------------------------------------------- */
1833
  /*      Reset bounding box.                                             */
1834
  /* -------------------------------------------------------------------- */
1835
  if (params->bbox.maxx != params->bbox.minx) {
3✔
1836
    rectObj orig_bbox = params->bbox;
3✔
1837

1838
    params->bbox.minx = cm->geotransform[0] +
3✔
1839
                        orig_bbox.minx * cm->geotransform[1] +
3✔
1840
                        orig_bbox.miny * cm->geotransform[2];
3✔
1841
    params->bbox.maxy = cm->geotransform[3] +
3✔
1842
                        orig_bbox.minx * cm->geotransform[4] +
3✔
1843
                        orig_bbox.miny * cm->geotransform[5];
3✔
1844
    params->bbox.maxx = cm->geotransform[0] +
3✔
1845
                        (orig_bbox.maxx + 1) * cm->geotransform[1] +
3✔
1846
                        (orig_bbox.maxy + 1) * cm->geotransform[2];
3✔
1847
    params->bbox.miny = cm->geotransform[3] +
3✔
1848
                        (orig_bbox.maxx + 1) * cm->geotransform[4] +
3✔
1849
                        (orig_bbox.maxy + 1) * cm->geotransform[5];
3✔
1850

1851
    /* WCS 1.1 boundbox is center of pixel oriented. */
1852
    if (strncasecmp(params->version, "1.1", 3) == 0) {
3✔
1853
      params->bbox.minx += cm->geotransform[1] / 2 + cm->geotransform[2] / 2;
3✔
1854
      params->bbox.maxx -= cm->geotransform[1] / 2 + cm->geotransform[2] / 2;
3✔
1855
      params->bbox.maxy += cm->geotransform[4] / 2 + cm->geotransform[5] / 2;
3✔
1856
      params->bbox.miny -= cm->geotransform[4] / 2 + cm->geotransform[5] / 2;
3✔
1857
    }
1858
  }
1859

1860
  /* -------------------------------------------------------------------- */
1861
  /*      Reset resolution.                                               */
1862
  /* -------------------------------------------------------------------- */
1863
  if (params->resx != 0.0) {
3✔
1864
    params->resx = cm->geotransform[1] * params->resx;
3✔
1865
    params->resy = fabs(cm->geotransform[5] * params->resy);
3✔
1866
  }
1867

1868
  return MS_SUCCESS;
1869
}
1870

1871
/************************************************************************/
1872
/*                    msWCSApplyLayerCreationOptions()                  */
1873
/************************************************************************/
1874

1875
void msWCSApplyLayerCreationOptions(layerObj *lp, outputFormatObj *format,
183✔
1876
                                    const char *bandlist)
1877

1878
{
1879
  const char *pszKey;
1880
  char szKeyBeginning[256];
1881
  size_t nKeyBeginningLength;
1882
  int nBands = 0;
183✔
1883
  char **papszBandNumbers = msStringSplit(bandlist, ',', &nBands);
183✔
1884

1885
  snprintf(szKeyBeginning, sizeof(szKeyBeginning),
183✔
1886
           "wcs_outputformat_%s_creationoption_", format->name);
1887
  nKeyBeginningLength = strlen(szKeyBeginning);
183✔
1888

1889
  pszKey = msFirstKeyFromHashTable(&(lp->metadata));
183✔
1890
  for (; pszKey != NULL;
3,743✔
1891
       pszKey = msNextKeyFromHashTable(&(lp->metadata), pszKey)) {
3,560✔
1892
    if (strncmp(pszKey, szKeyBeginning, nKeyBeginningLength) == 0) {
3,560✔
1893
      const char *pszValue = msLookupHashTable(&(lp->metadata), pszKey);
18✔
1894
      const char *pszGDALKey = pszKey + nKeyBeginningLength;
18✔
1895
      if (EQUALN(pszGDALKey, "BAND_", strlen("BAND_"))) {
18✔
1896
        /* Remap BAND specific creation option to the real output
1897
         * band number, given the band subset of the request */
1898
        int nKeyOriBandNumber = atoi(pszGDALKey + strlen("BAND_"));
5✔
1899
        int nTargetBandNumber = -1;
1900
        int i;
1901
        for (i = 0; i < nBands; i++) {
6✔
1902
          if (nKeyOriBandNumber == atoi(papszBandNumbers[i])) {
5✔
1903
            nTargetBandNumber = i + 1;
4✔
1904
            break;
4✔
1905
          }
1906
        }
1907
        if (nTargetBandNumber > 0) {
5✔
1908
          char szModKey[256];
1909
          const char *pszAfterBand = strchr(pszGDALKey + strlen("BAND_"), '_');
1910
          if (pszAfterBand != NULL) {
4✔
1911
            snprintf(szModKey, sizeof(szModKey), "BAND_%d%s", nTargetBandNumber,
1912
                     pszAfterBand);
1913
            if (lp->debug >= MS_DEBUGLEVEL_VVV) {
4✔
1914
              msDebug("Setting GDAL %s=%s creation option\n", szModKey,
×
1915
                      pszValue);
1916
            }
1917
            msSetOutputFormatOption(format, szModKey, pszValue);
4✔
1918
          }
1919
        }
1920
      } else {
1921
        if (lp->debug >= MS_DEBUGLEVEL_VVV) {
13✔
1922
          msDebug("Setting GDAL %s=%s creation option\n", pszGDALKey, pszValue);
×
1923
        }
1924
        msSetOutputFormatOption(format, pszGDALKey, pszValue);
13✔
1925
      }
1926
    }
1927
  }
1928

1929
  msFreeCharArray(papszBandNumbers, nBands);
183✔
1930
}
183✔
1931

1932
/************************************************************************/
1933
/*               msWCSApplyDatasetMetadataAsCreationOptions()           */
1934
/************************************************************************/
1935

1936
void msWCSApplyDatasetMetadataAsCreationOptions(layerObj *lp,
40✔
1937
                                                outputFormatObj *format,
1938
                                                const char *bandlist,
1939
                                                void *hDSIn) {
1940
  /* Requires GDAL 2.3 in practice. */
1941
  /* Automatic forwarding of input dataset metadata if it is GRIB and the */
1942
  /* output is GRIB as well, and wcs_outputformat_GRIB_creationoption* are */
1943
  /* not defined. */
1944
  GDALDatasetH hDS = (GDALDatasetH)hDSIn;
1945
  if (hDS && GDALGetDatasetDriver(hDS) &&
40✔
1946
      EQUAL(GDALGetDriverShortName(GDALGetDatasetDriver(hDS)), "GRIB") &&
80✔
1947
      EQUAL(format->driver, "GDAL/GRIB")) {
3✔
1948
    const char *pszKey;
1949
    char szKeyBeginning[256];
1950
    size_t nKeyBeginningLength;
1951
    int bWCSMetadataFound = MS_FALSE;
1952

1953
    snprintf(szKeyBeginning, sizeof(szKeyBeginning),
3✔
1954
             "wcs_outputformat_%s_creationoption_", format->name);
1955
    nKeyBeginningLength = strlen(szKeyBeginning);
3✔
1956

1957
    for (pszKey = msFirstKeyFromHashTable(&(lp->metadata)); pszKey != NULL;
83✔
1958
         pszKey = msNextKeyFromHashTable(&(lp->metadata), pszKey)) {
80✔
1959
      if (strncmp(pszKey, szKeyBeginning, nKeyBeginningLength) == 0) {
80✔
1960
        bWCSMetadataFound = MS_TRUE;
1961
        break;
1962
      }
1963
    }
1964
    if (!bWCSMetadataFound) {
3✔
1965
      int nBands = 0;
3✔
1966
      char **papszBandNumbers = msStringSplit(bandlist, ',', &nBands);
3✔
1967
      int i;
1968
      for (i = 0; i < nBands; i++) {
6✔
1969
        int nSrcBand = atoi(papszBandNumbers[i]);
3✔
1970
        int nDstBand = i + 1;
3✔
1971
        GDALRasterBandH hBand = GDALGetRasterBand(hDS, nSrcBand);
3✔
1972
        if (hBand) {
3✔
1973
          char **papszMD = GDALGetMetadata(hBand, NULL);
3✔
1974
          const char *pszMDI = CSLFetchNameValue(papszMD, "GRIB_IDS");
3✔
1975
          // Make sure it is a GRIB2 band
1976
          if (pszMDI) {
3✔
1977
            char szKey[256];
1978
            snprintf(szKey, sizeof(szKey), "BAND_%d_IDS", nDstBand);
1979
            msSetOutputFormatOption(format, szKey, pszMDI);
3✔
1980

1981
            snprintf(szKey, sizeof(szKey), "BAND_%d_DISCIPLINE", nDstBand);
1982
            msSetOutputFormatOption(format, szKey,
3✔
1983
                                    CSLFetchNameValue(papszMD, "DISCIPLINE"));
1984

1985
            snprintf(szKey, sizeof(szKey), "BAND_%d_PDS_PDTN", nDstBand);
1986
            msSetOutputFormatOption(
3✔
1987
                format, szKey, CSLFetchNameValue(papszMD, "GRIB_PDS_PDTN"));
1988

1989
            snprintf(szKey, sizeof(szKey), "BAND_%d_PDS_TEMPLATE_NUMBERS",
1990
                     nDstBand);
1991
            msSetOutputFormatOption(
3✔
1992
                format, szKey,
1993
                CSLFetchNameValue(papszMD, "GRIB_PDS_TEMPLATE_NUMBERS"));
1994
          }
1995
        }
1996
      }
1997
      msFreeCharArray(papszBandNumbers, nBands);
3✔
1998
    }
1999
  }
2000
}
40✔
2001

2002
/************************************************************************/
2003
/*                  msWCSApplyLayerMetadataItemOptions()                */
2004
/************************************************************************/
2005

2006
void msWCSApplyLayerMetadataItemOptions(layerObj *lp, outputFormatObj *format,
183✔
2007
                                        const char *bandlist)
2008

2009
{
2010
  if (!STARTS_WITH(format->driver, "GDAL/"))
183✔
2011
    return;
10✔
2012

2013
  const bool bIsNetCDFOutput = EQUAL(format->driver, "GDAL/netCDF");
173✔
2014
  const char *pszKey;
2015
  char szKeyBeginning[256];
2016
  size_t nKeyBeginningLength;
2017
  int nBands = 0;
173✔
2018
  char **papszBandNumbers = msStringSplit(bandlist, ',', &nBands);
173✔
2019

2020
  snprintf(szKeyBeginning, sizeof(szKeyBeginning), "wcs_outputformat_%s_mdi_",
173✔
2021
           format->name);
2022
  nKeyBeginningLength = strlen(szKeyBeginning);
173✔
2023

2024
  // Transform wcs_outputformat_{formatname}_mdi_{key} to mdi_{key}
2025
  // and Transform wcs_outputformat_{formatname}_mdi_BAND_X_{key} to
2026
  // mdi_BAND_Y_{key} MDI stands for MetaDataItem
2027

2028
  // For netCDF 3D output
2029
  std::map<int, std::string> oMapExtraDimValues;
2030
  std::string osExtraDimName;
2031
  bool bExtraDimValid = false;
2032

2033
  pszKey = msFirstKeyFromHashTable(&(lp->metadata));
173✔
2034
  for (; pszKey != NULL;
3,526✔
2035
       pszKey = msNextKeyFromHashTable(&(lp->metadata), pszKey)) {
3,353✔
2036
    if (strncmp(pszKey, szKeyBeginning, nKeyBeginningLength) == 0) {
3,353✔
2037
      const char *pszValue = msLookupHashTable(&(lp->metadata), pszKey);
15✔
2038
      const char *pszGDALKey = pszKey + nKeyBeginningLength;
15✔
2039

2040
      if (EQUALN(pszGDALKey, "BAND_", strlen("BAND_"))) {
15✔
2041
        /* Remap BAND specific creation option to the real output
2042
         * band number, given the band subset of the request */
2043
        int nKeyOriBandNumber = atoi(pszGDALKey + strlen("BAND_"));
11✔
2044
        int nTargetBandNumber = -1;
11✔
2045
        int i;
2046
        for (i = 0; i < nBands; i++) {
33✔
2047
          if (nKeyOriBandNumber == atoi(papszBandNumbers[i])) {
26✔
2048
            nTargetBandNumber = i + 1;
4✔
2049
            break;
4✔
2050
          }
2051
        }
2052
        if (nTargetBandNumber > 0) {
11✔
2053
          char szModKey[256];
2054
          const char *pszAfterBand = strchr(pszGDALKey + strlen("BAND_"), '_');
2055
          if (pszAfterBand != NULL) {
4✔
2056
            // Special case to generate 3D netCDF files
2057
            if (bIsNetCDFOutput &&
4✔
2058
                strncmp(pszAfterBand, "_default_NETCDF_DIM_",
3✔
2059
                        strlen("_default_NETCDF_DIM_")) == 0) {
2060
              const char *pszDimName =
3✔
2061
                  pszAfterBand + strlen("_default_NETCDF_DIM_");
2062
              if (osExtraDimName.empty()) {
3✔
2063
                bExtraDimValid = true;
2064
                osExtraDimName = pszDimName;
2065
                oMapExtraDimValues[nTargetBandNumber] = pszValue;
1✔
2066
              } else if (bExtraDimValid) {
2✔
2067
                if (osExtraDimName != pszDimName) {
2✔
2068
                  msDebug("One band has several %sdefault_NETCDF_DIM_ metadata "
×
2069
                          "items, "
2070
                          "or different bands have a different value. "
2071
                          "Only a single extra dimension is supported.",
2072
                          szKeyBeginning);
2073
                  bExtraDimValid = false;
2074
                } else {
2075
                  oMapExtraDimValues[nTargetBandNumber] = pszValue;
2✔
2076
                }
2077
              }
2078
            } else {
2079
              snprintf(szModKey, sizeof(szModKey), "mdi_BAND_%d%s",
2080
                       nTargetBandNumber, pszAfterBand);
2081
              if (lp->debug >= MS_DEBUGLEVEL_VVV) {
1✔
2082
                msDebug("Setting GDAL %s=%s metadata item option\n", szModKey,
×
2083
                        pszValue);
2084
              }
2085
              msSetOutputFormatOption(format, szModKey, pszValue);
1✔
2086
            }
2087
          }
2088
        }
2089
      } else {
2090
        char szModKey[256];
2091
        snprintf(szModKey, sizeof(szModKey), "mdi_%s", pszGDALKey);
2092
        if (lp->debug >= MS_DEBUGLEVEL_VVV) {
4✔
2093
          msDebug("Setting GDAL %s=%s metadata item option\n", szModKey,
×
2094
                  pszValue);
2095
        }
2096
        msSetOutputFormatOption(format, szModKey, pszValue);
4✔
2097
      }
2098
    }
2099
  }
2100

2101
  // netCDF 3D output
2102
  // Tested in msautotest/wxs/wcs_netcdf_3d_output.map
2103
  std::string osExtraDimDataType;
2104
  if (bExtraDimValid && static_cast<int>(oMapExtraDimValues.size()) != nBands) {
173✔
2105
    msDebug("One of the band lack a NETCDF_DIM_%s metadata item.",
×
2106
            osExtraDimName.c_str());
2107
    bExtraDimValid = false;
2108
  }
2109
  if (bExtraDimValid) {
173✔
2110
    for (const auto &keyValue : oMapExtraDimValues) {
4✔
2111
      const auto &osVal = keyValue.second;
2112
      const CPLValueType eType = CPLGetValueType(osVal.c_str());
3✔
2113
      if (eType == CPL_VALUE_STRING) {
3✔
2114
        osExtraDimDataType = "string";
2115
        break;
2116
      } else if (eType == CPL_VALUE_INTEGER) {
3✔
2117
        if (osExtraDimDataType.empty() || osExtraDimDataType == "integer") {
3✔
2118
          osExtraDimDataType = "integer";
2119
          const GIntBig nVal = CPLAtoGIntBig(osVal.c_str());
3✔
2120
          if (nVal > INT_MAX || nVal < INT_MIN) {
3✔
2121
            osExtraDimDataType = "integer64";
2122
          }
2123
        }
2124
      } else {
2125
        osExtraDimDataType = "double";
2126
      }
2127
    }
2128
    if (osExtraDimDataType == "string") {
1✔
2129
      msDebug("One of the value for the NETCDF_DIM_%s metadata is a string. "
×
2130
              "This is not supported",
2131
              osExtraDimName.c_str());
2132
      bExtraDimValid = false;
2133
    }
2134
  }
2135
  if (bExtraDimValid) {
2136
    // Cf
2137
    // https://gdal.org/drivers/raster/netcdf.html#creation-of-multidimensional-files-with-createcopy-2d-raster-api
2138

2139
    // Define the name of the extra dimensions
2140
    {
2141
      std::string osValue;
2142
      osValue = '{';
2143
      osValue += osExtraDimName;
2144
      osValue += '}';
2145

2146
      msSetOutputFormatOption(format, "mdi_default_NETCDF_DIM_EXTRA",
1✔
2147
                              osValue.c_str());
2148
    }
2149

2150
    // Define the size (in number of samples) and type of the extra dimension
2151
    {
2152
      std::string osKey;
2153
      osKey = "mdi_default_NETCDF_DIM_";
2154
      osKey += osExtraDimName;
2155
      osKey += "_DEF";
2156

2157
      std::string osValue;
2158
      osValue = '{';
2159
      osValue += CPLSPrintf("%d", nBands);
1✔
2160
      osValue += ',';
2161
      if (osExtraDimDataType == "integer")
1✔
2162
        osValue += '4';
2163
      else if (osExtraDimDataType == "integer64")
×
2164
        osValue += "10";
2165
      else /* if ( osExtraDimDataType == "double" ) */
2166
        osValue += '6';
2167
      osValue += '}';
2168

2169
      msSetOutputFormatOption(format, osKey.c_str(), osValue.c_str());
1✔
2170
    }
2171

2172
    // Define the values along the extra dimension
2173
    {
2174
      std::string osKey;
2175
      osKey = "mdi_default_NETCDF_DIM_";
2176
      osKey += osExtraDimName;
2177
      osKey += "_VALUES";
2178
      std::string osValue;
2179
      osValue = '{';
2180
      bool bFirstVal = true;
2181
      for (const auto &keyValue : oMapExtraDimValues) {
4✔
2182
        const auto &osVal = keyValue.second;
2183
        if (!bFirstVal)
3✔
2184
          osValue += ',';
2185
        else
2186
          bFirstVal = false;
2187
        osValue += osVal;
2188
      }
2189
      osValue += '}';
2190

2191
      msSetOutputFormatOption(format, osKey.c_str(), osValue.c_str());
1✔
2192
    }
2193
  }
2194

2195
  msFreeCharArray(papszBandNumbers, nBands);
173✔
2196
}
2197

2198
/************************************************************************/
2199
/*               msWCSApplySourceDatasetMetadata()                      */
2200
/************************************************************************/
2201

2202
void msWCSApplySourceDatasetMetadata(layerObj *lp, outputFormatObj *format,
40✔
2203
                                     const char *bandlist, void *hDSIn) {
2204
  /* Automatic forwarding of input dataset metadata if it is netCDF and the */
2205
  /* output is netCDF as well, and wcs_outputformat_netCDF_mdi* are */
2206
  /* not defined. */
2207
  GDALDatasetH hDS = (GDALDatasetH)hDSIn;
2208
  if (hDS && GDALGetDatasetDriver(hDS) &&
40✔
2209
      EQUAL(GDALGetDriverShortName(GDALGetDatasetDriver(hDS)), "netCDF") &&
80✔
2210
      EQUAL(format->driver, "GDAL/netCDF")) {
3✔
2211
    const char *pszKey;
2212
    char szKeyBeginning[256];
2213
    size_t nKeyBeginningLength;
2214
    int bWCSMetadataFound = MS_FALSE;
2215

2216
    snprintf(szKeyBeginning, sizeof(szKeyBeginning), "wcs_outputformat_%s_mdi_",
3✔
2217
             format->name);
2218
    nKeyBeginningLength = strlen(szKeyBeginning);
3✔
2219

2220
    for (pszKey = msFirstKeyFromHashTable(&(lp->metadata)); pszKey != NULL;
73✔
2221
         pszKey = msNextKeyFromHashTable(&(lp->metadata), pszKey)) {
70✔
2222
      if (strncmp(pszKey, szKeyBeginning, nKeyBeginningLength) == 0) {
70✔
2223
        bWCSMetadataFound = MS_TRUE;
2224
        break;
2225
      }
2226
    }
2227
    if (!bWCSMetadataFound) {
3✔
2228
      int nBands = 0;
3✔
2229
      char **papszBandNumbers = msStringSplit(bandlist, ',', &nBands);
3✔
2230

2231
      std::string osExtraDimName;
2232
      // Special processing if the input dataset is a 3D one
2233
      {
2234
        // Check if extra dimensions are declared on the source dataset,
2235
        // and if so, if there's just a single one.
2236
        const char *pszDimExtraWithCurl =
2237
            GDALGetMetadataItem(hDS, "NETCDF_DIM_EXTRA", nullptr);
3✔
2238
        if (pszDimExtraWithCurl &&
3✔
2239
            strchr(pszDimExtraWithCurl, ',') == nullptr &&
3✔
2240
            pszDimExtraWithCurl[0] == '{' &&
3✔
2241
            pszDimExtraWithCurl[strlen(pszDimExtraWithCurl) - 1] == '}') {
3✔
2242
          osExtraDimName.append(pszDimExtraWithCurl + 1,
3✔
2243
                                strlen(pszDimExtraWithCurl) - 2);
2244

2245
          // Declare the extra dimension name
2246
          msSetOutputFormatOption(format, "mdi_default_NETCDF_DIM_EXTRA",
3✔
2247
                                  pszDimExtraWithCurl);
2248

2249
          // Declare the extra dimension definition: size + data type
2250
          const char *pszDimExtraDef = GDALGetMetadataItem(
3✔
2251
              hDS, ("NETCDF_DIM_" + osExtraDimName + "_DEF").c_str(), nullptr);
3✔
2252
          if (pszDimExtraDef && pszDimExtraDef[0] == '{' &&
3✔
2253
              pszDimExtraDef[strlen(pszDimExtraDef) - 1] == '}') {
3✔
2254
            const auto tokens = msStringSplit(
2255
                std::string(pszDimExtraDef + 1, strlen(pszDimExtraDef) - 2)
6✔
2256
                    .c_str(),
2257
                ',');
3✔
2258
            if (tokens.size() == 2) {
3✔
2259
              const auto &varType = tokens[1];
2260
              msSetOutputFormatOption(
3✔
2261
                  format,
2262
                  ("mdi_default_NETCDF_DIM_" + osExtraDimName + "_DEF").c_str(),
6✔
2263
                  (std::string("{") + CPLSPrintf("%d", nBands) + ',' + varType +
12✔
2264
                   '}')
2265
                      .c_str());
2266
            }
2267
          }
3✔
2268

2269
          // Declare the extra dimension values
2270
          const char *pszDimExtraValues = GDALGetMetadataItem(
3✔
2271
              hDS, ("NETCDF_DIM_" + osExtraDimName + "_VALUES").c_str(),
3✔
2272
              nullptr);
2273
          if (pszDimExtraValues && pszDimExtraValues[0] == '{' &&
3✔
2274
              pszDimExtraValues[strlen(pszDimExtraValues) - 1] == '}') {
1✔
2275
            const auto tokens =
2276
                msStringSplit(std::string(pszDimExtraValues + 1,
2✔
2277
                                          strlen(pszDimExtraValues) - 2)
2278
                                  .c_str(),
2279
                              ',');
1✔
2280
            if (static_cast<int>(tokens.size()) == GDALGetRasterCount(hDS)) {
1✔
2281
              std::string osValue = "{";
1✔
2282
              for (int i = 0; i < nBands; i++) {
3✔
2283
                int nSrcBand = atoi(papszBandNumbers[i]);
2✔
2284
                assert(nSrcBand >= 1 &&
2285
                       nSrcBand <= static_cast<int>(tokens.size()));
2286
                if (i > 0)
2✔
2287
                  osValue += ',';
2288
                osValue += tokens[nSrcBand - 1];
2✔
2289
              }
2290
              osValue += '}';
2291

2292
              msSetOutputFormatOption(
1✔
2293
                  format,
2294
                  ("mdi_default_NETCDF_DIM_" + osExtraDimName + "_VALUES")
2✔
2295
                      .c_str(),
2296
                  osValue.c_str());
2297
            }
2298
          } else if (pszDimExtraValues) {
1✔
2299
            // If there's a single value
2300
            msSetOutputFormatOption(
2✔
2301
                format,
2302
                ("mdi_default_NETCDF_DIM_" + osExtraDimName + "_VALUES")
4✔
2303
                    .c_str(),
2304
                pszDimExtraValues);
2305
          }
2306
        }
2307
      }
2308

2309
      {
2310
        char **papszMD = GDALGetMetadata(hDS, NULL);
3✔
2311
        if (papszMD) {
3✔
2312
          for (char **papszIter = papszMD; *papszIter; ++papszIter) {
88✔
2313
            // Copy netCDF global attributes, as well as the ones
2314
            // of the extra dimension for 3D netCDF files
2315
            if (STARTS_WITH(*papszIter, "NC_GLOBAL#") ||
85✔
2316
                (!osExtraDimName.empty() &&
63✔
2317
                 STARTS_WITH(*papszIter, osExtraDimName.c_str()) &&
63✔
2318
                 (*papszIter)[osExtraDimName.size()] == '#')) {
9✔
2319
              char *pszKey = nullptr;
31✔
2320
              const char *pszValue = CPLParseNameValue(*papszIter, &pszKey);
31✔
2321
              if (pszKey && pszValue) {
31✔
2322
                char szKey[256];
2323
                snprintf(szKey, sizeof(szKey), "mdi_default_%s", pszKey);
2324
                msSetOutputFormatOption(format, szKey, pszValue);
31✔
2325
              }
2326
              CPLFree(pszKey);
31✔
2327
            }
2328
          }
2329
        }
2330
      }
2331

2332
      for (int i = 0; i < nBands; i++) {
7✔
2333
        int nSrcBand = atoi(papszBandNumbers[i]);
4✔
2334
        int nDstBand = i + 1;
4✔
2335
        GDALRasterBandH hBand = GDALGetRasterBand(hDS, nSrcBand);
4✔
2336
        if (hBand) {
4✔
2337
          char **papszMD = GDALGetMetadata(hBand, NULL);
4✔
2338
          if (papszMD) {
4✔
2339
            for (char **papszIter = papszMD; *papszIter; ++papszIter) {
30✔
2340
              char *pszKey = nullptr;
26✔
2341
              const char *pszValue = CPLParseNameValue(*papszIter, &pszKey);
26✔
2342
              if (pszKey && pszValue && !EQUAL(pszKey, "grid_name") &&
26✔
2343
                  !EQUAL(pszKey, "grid_mapping")) {
24✔
2344
                char szKey[256];
2345
                snprintf(szKey, sizeof(szKey), "mdi_BAND_%d_default_%s",
2346
                         nDstBand, pszKey);
2347
                msSetOutputFormatOption(format, szKey, pszValue);
22✔
2348
              }
2349
              CPLFree(pszKey);
26✔
2350
            }
2351
          }
2352
        }
2353
      }
2354
      msFreeCharArray(papszBandNumbers, nBands);
3✔
2355
    }
2356
  }
2357
}
40✔
2358

2359
/************************************************************************/
2360
/*                          msWCSGetCoverage()                          */
2361
/************************************************************************/
2362

2363
static int msWCSGetCoverage(mapObj *map, cgiRequestObj *request,
44✔
2364
                            wcsParamsObj *params, owsRequestObj *ows_request) {
2365
  imageObj *image;
2366
  layerObj *lp;
2367
  int status, i;
2368
  const char *value;
2369
  outputFormatObj *format;
2370
  char *bandlist = NULL;
44✔
2371
  size_t bufferSize = 0;
2372
  char numbands[12]; /* should be large enough to hold the number of bands in
2373
                        the bandlist */
2374
  coverageMetadataObj cm;
2375
  rectObj reqextent;
2376
  rectObj covextent;
2377
  rasterBufferObj rb;
2378
  int doDrawRasterLayerDraw = MS_TRUE;
2379
  GDALDatasetH hDS = NULL;
2380

2381
  char *coverageName;
2382

2383
  /* make sure all required parameters are available (at least the easy ones) */
2384
  if (!params->crs) {
44✔
2385
    msSetError(MS_WCSERR, "Required parameter CRS was not supplied.",
1✔
2386
               "msWCSGetCoverage()");
2387
    return msWCSException(map, "MissingParameterValue", "crs", params->version);
1✔
2388
  }
2389

2390
  if (!params->time && !params->bbox.minx && !params->bbox.miny &&
43✔
2391
      !params->bbox.maxx && !params->bbox.maxy) {
13✔
2392
    msSetError(MS_WCSERR, "One of BBOX or TIME is required",
×
2393
               "msWCSGetCoverage()");
2394
    return msWCSException(map, "MissingParameterValue", "bbox/time",
×
2395
                          params->version);
×
2396
  }
2397

2398
  if (params->coverages == NULL || params->coverages[0] == NULL) {
43✔
2399
    msSetError(MS_WCSERR, "Required parameter COVERAGE was not supplied.",
1✔
2400
               "msWCSGetCoverage()");
2401
    return msWCSException(map, "MissingParameterValue", "coverage",
1✔
2402
                          params->version);
1✔
2403
  }
2404

2405
  /* For WCS 1.1, we need to normalize the axis order of the BBOX and
2406
     resolution values some coordinate systems (eg. EPSG geographic) */
2407
  if (strncasecmp(params->version, "1.0", 3) != 0 && params->crs != NULL &&
42✔
2408
      strncasecmp(params->crs, "urn:", 4) == 0) {
20✔
2409
    projectionObj proj;
2410

2411
    msInitProjection(&proj);
17✔
2412
    msProjectionInheritContextFrom(&proj, &(map->projection));
17✔
2413
    if (msLoadProjectionString(&proj, (char *)params->crs) == 0) {
17✔
2414
      msAxisNormalizePoints(&proj, 1, &(params->bbox.minx),
17✔
2415
                            &(params->bbox.miny));
2416
      msAxisNormalizePoints(&proj, 1, &(params->bbox.maxx),
17✔
2417
                            &(params->bbox.maxy));
2418
      msAxisNormalizePoints(&proj, 1, &(params->resx), &(params->resy));
17✔
2419
      msAxisNormalizePoints(&proj, 1, &(params->originx), &(params->originy));
17✔
2420
    } else
2421
      msResetErrorList();
×
2422
    msFreeProjection(&proj);
17✔
2423
  }
2424

2425
  /* find the layer we are working with */
2426
  lp = NULL;
2427
  for (i = 0; i < map->numlayers; i++) {
57✔
2428
    coverageName = msOWSGetEncodeMetadata(&(GET_LAYER(map, i)->metadata), "CO",
56✔
2429
                                          "name", GET_LAYER(map, i)->name);
56✔
2430
    if (coverageName != NULL && EQUAL(coverageName, params->coverages[0]) &&
97✔
2431
        (msIntegerInArray(GET_LAYER(map, i)->index, ows_request->enabled_layers,
41✔
2432
                          ows_request->numlayers))) {
2433
      lp = GET_LAYER(map, i);
41✔
2434
      free(coverageName);
41✔
2435
      break;
41✔
2436
    }
2437
    free(coverageName);
15✔
2438
  }
2439

2440
  if (lp == NULL) {
42✔
2441
    msSetError(
1✔
2442
        MS_WCSERR,
2443
        "COVERAGE=%s not found, not in supported layer list. A layer might be disabled for \
2444
this request. Check wcs/ows_enable_request settings.",
2445
        "msWCSGetCoverage()", params->coverages[0]);
1✔
2446
    return msWCSException(map, "InvalidParameterValue", "coverage",
1✔
2447
                          params->version);
1✔
2448
  }
2449

2450
  /* make sure the layer is on */
2451
  lp->status = MS_ON;
41✔
2452

2453
  /* If the layer has no projection set, set it to the map's projection (#4079)
2454
   */
2455
  if (lp->projection.numargs <= 0) {
41✔
2456
    char *map_original_srs = msGetProjectionString(&(map->projection));
×
2457
    if (msLoadProjectionString(&(lp->projection), map_original_srs) != 0) {
×
2458
      msSetError(
×
2459
          MS_WCSERR,
2460
          "Error when setting map projection to a layer with no projection",
2461
          "msWCSGetCoverage()");
2462
      free(map_original_srs);
×
2463
      return msWCSException(map, NULL, NULL, params->version);
×
2464
    }
2465
    free(map_original_srs);
×
2466
  }
2467

2468
  /* we need the coverage metadata, since things like numbands may not be
2469
   * available otherwise */
2470
  status = msWCSGetCoverageMetadata(lp, &cm);
41✔
2471
  if (status != MS_SUCCESS)
41✔
2472
    return MS_FAILURE;
2473

2474
  /* fill in bands rangeset info, if required.  */
2475
  msWCSSetDefaultBandsRangeSetInfo(params, &cm, lp);
41✔
2476

2477
  /* handle the response CRS, that is, set the map object projection */
2478
  if (params->response_crs || params->crs) {
41✔
2479
    int iUnits;
2480
    const char *crs_to_use = params->response_crs;
2481

2482
    if (crs_to_use == NULL)
41✔
2483
      crs_to_use = params->crs;
41✔
2484

2485
    if (strncasecmp(crs_to_use, "EPSG:", 5) == 0 ||
41✔
2486
        strncasecmp(crs_to_use, "urn:ogc:def:crs:", 16) == 0) {
19✔
2487
      if (msLoadProjectionString(&(map->projection), (char *)crs_to_use) != 0)
38✔
2488
        return msWCSException(map, NULL, NULL, params->version);
×
2489
    } else if (strcasecmp(crs_to_use, "imageCRS") == 0) {
3✔
2490
      /* use layer native CRS, and rework bounding box accordingly */
2491
      if (msWCSGetCoverage_ImageCRSSetup(map, params, &cm, lp) != MS_SUCCESS) {
3✔
2492
        msWCSFreeCoverageMetadata(&cm);
×
2493
        return MS_FAILURE;
×
2494
      }
2495
    } else { /* should we support WMS style AUTO: projections? (not for now) */
2496
      msSetError(MS_WCSERR,
×
2497
                 "Unsupported SRS namespace (only EPSG currently supported).",
2498
                 "msWCSGetCoverage()");
2499
      return msWCSException(map, "InvalidParameterValue", "srs",
×
2500
                            params->version);
×
2501
    }
2502

2503
    iUnits = GetMapserverUnitUsingProj(&(map->projection));
41✔
2504
    if (iUnits != -1)
41✔
2505
      map->units = static_cast<MS_UNITS>(iUnits);
41✔
2506
  }
2507

2508
  /* did we get a TIME value (support only a single value for now) */
2509
  if (params->time) {
41✔
2510
    int tli;
2511
    layerObj *tlp = NULL;
2512

2513
    /* need to handle NOW case */
2514

2515
    /* check format of TIME parameter */
2516
    if (strchr(params->time, ',')) {
1✔
2517
      msWCSFreeCoverageMetadata(&cm);
×
2518
      msSetError(MS_WCSERR,
×
2519
                 "Temporal lists are not supported, only individual values.",
2520
                 "msWCSGetCoverage()");
2521
      return msWCSException(map, "InvalidParameterValue", "time",
×
2522
                            params->version);
×
2523
    }
2524
    if (strchr(params->time, '/')) {
1✔
2525
      msWCSFreeCoverageMetadata(&cm);
×
2526
      msSetError(MS_WCSERR,
×
2527
                 "Temporal ranges are not supported, only individual values.",
2528
                 "msWCSGetCoverage()");
2529
      return msWCSException(map, "InvalidParameterValue", "time",
×
2530
                            params->version);
×
2531
    }
2532

2533
    /* TODO: will need to expand this check if a time period is supported */
2534
    value = msOWSLookupMetadata(&(lp->metadata), "CO", "timeposition");
1✔
2535
    if (!value) {
1✔
2536
      msWCSFreeCoverageMetadata(&cm);
×
2537
      msSetError(MS_WCSERR,
×
2538
                 "The coverage does not support temporal subsetting.",
2539
                 "msWCSGetCoverage()");
2540
      return msWCSException(map, "InvalidParameterValue", "time",
×
2541
                            params->version);
×
2542
    }
2543

2544
    /* check if timestamp is covered by the wcs_timeposition definition */
2545
    if (msValidateTimeValue(params->time, value) == MS_FALSE) {
1✔
2546
      msWCSFreeCoverageMetadata(&cm);
×
2547
      msSetError(MS_WCSERR, "The coverage does not have a time position of %s.",
×
2548
                 "msWCSGetCoverage()", params->time);
2549
      return msWCSException(map, "InvalidParameterValue", "time",
×
2550
                            params->version);
×
2551
    }
2552

2553
    /* make sure layer is tiled appropriately */
2554
    if (!lp->tileindex) {
1✔
2555
      msWCSFreeCoverageMetadata(&cm);
×
2556
      msSetError(
×
2557
          MS_WCSERR,
2558
          "Underlying layer is not tiled, unable to do temporal subsetting.",
2559
          "msWCSGetCoverage()");
2560
      return msWCSException(map, NULL, NULL, params->version);
×
2561
    }
2562
    tli = msGetLayerIndex(map, lp->tileindex);
1✔
2563
    if (tli == -1) {
1✔
2564
      msWCSFreeCoverageMetadata(&cm);
×
2565
      msSetError(MS_WCSERR,
×
2566
                 "Underlying layer does not use appropriate tiling mechanism.",
2567
                 "msWCSGetCoverage()");
2568
      return msWCSException(map, NULL, NULL, params->version);
×
2569
    }
2570

2571
    tlp = (GET_LAYER(map, tli));
1✔
2572

2573
    /* make sure there is enough information to filter */
2574
    value = msOWSLookupMetadata(&(lp->metadata), "CO", "timeitem");
1✔
2575
    if (!tlp->filteritem && !value) {
1✔
2576
      msWCSFreeCoverageMetadata(&cm);
×
2577
      msSetError(MS_WCSERR, "Not enough information available to filter.",
×
2578
                 "msWCSGetCoverage()");
2579
      return msWCSException(map, NULL, NULL, params->version);
×
2580
    }
2581

2582
    /* override filteritem if specified in metadata */
2583
    if (value) {
×
2584
      if (tlp->filteritem)
1✔
2585
        free(tlp->filteritem);
×
2586
      tlp->filteritem = msStrdup(value);
1✔
2587
    }
2588

2589
    /* finally set the filter */
2590
    msLayerSetTimeFilter(tlp, params->time, value);
1✔
2591
  }
2592

2593
  if (strncasecmp(params->version, "1.0", 3) == 0)
41✔
2594
    status = msWCSGetCoverageBands10(map, request, params, lp, &bandlist);
22✔
2595
  else
2596
    status = msWCSGetCoverageBands11(map, request, params, lp, &bandlist);
19✔
2597
  if (status != MS_SUCCESS) {
41✔
2598
    msWCSFreeCoverageMetadata(&cm);
1✔
2599
    return status;
1✔
2600
  }
2601

2602
  /* did we get BBOX values? if not use the extent stored in the
2603
   * coverageMetadataObj */
2604
  if (fabs((params->bbox.maxx - params->bbox.minx)) < 0.000000000001 ||
40✔
2605
      fabs(params->bbox.maxy - params->bbox.miny) < 0.000000000001) {
40✔
2606
    params->bbox = cm.extent;
×
2607

2608
    /* WCS 1.1 boundbox is center of pixel oriented. */
2609
    if (strncasecmp(params->version, "1.1", 3) == 0) {
×
2610
      params->bbox.minx += cm.geotransform[1] / 2 + cm.geotransform[2] / 2;
×
2611
      params->bbox.maxx -= cm.geotransform[1] / 2 + cm.geotransform[2] / 2;
×
2612
      params->bbox.maxy += cm.geotransform[4] / 2 + cm.geotransform[5] / 2;
×
2613
      params->bbox.miny -= cm.geotransform[4] / 2 + cm.geotransform[5] / 2;
×
2614
    }
2615
  }
2616

2617
  /* WCS 1.1+ GridOrigin is effectively resetting the minx/maxy
2618
     BOUNDINGBOX values, so apply that here */
2619
  if (params->originx != 0.0 || params->originy != 0.0) {
40✔
2620
    assert(strncasecmp(params->version, "1.0", 3) !=
2621
           0); /* should always be 1.0 in this logic. */
2622
    params->bbox.minx = params->originx;
16✔
2623
    params->bbox.maxy = params->originy;
16✔
2624
  }
2625

2626
  /* if necessary, project the BBOX to the map->projection */
2627
  if (params->response_crs && params->crs) {
40✔
2628
    projectionObj tmp_proj;
2629

2630
    msInitProjection(&tmp_proj);
×
2631
    msProjectionInheritContextFrom(&tmp_proj, &(map->projection));
×
2632
    if (msLoadProjectionString(&tmp_proj, (char *)params->crs) != 0) {
×
2633
      msWCSFreeCoverageMetadata(&cm);
×
2634
      return msWCSException(map, NULL, NULL, params->version);
×
2635
    }
2636
    msProjectRect(&tmp_proj, &map->projection, &(params->bbox));
×
2637
    msFreeProjection(&tmp_proj);
×
2638
  }
2639

2640
  /* in WCS 1.1 the default is full resolution */
2641
  if (strncasecmp(params->version, "1.1", 3) == 0 && params->resx == 0.0 &&
40✔
2642
      params->resy == 0.0) {
×
2643
    params->resx = cm.geotransform[1];
×
2644
    params->resy = fabs(cm.geotransform[5]);
×
2645
  }
2646

2647
  /* compute width/height from BBOX and cellsize.  */
2648
  if ((params->resx == 0.0 || params->resy == 0.0) && params->width != 0 &&
40✔
2649
      params->height != 0) {
16✔
2650
    assert(strncasecmp(params->version, "1.0", 3) ==
2651
           0); /* should always be 1.0 in this logic. */
2652
    params->resx = (params->bbox.maxx - params->bbox.minx) / params->width;
16✔
2653
    params->resy = (params->bbox.maxy - params->bbox.miny) / params->height;
16✔
2654
  }
2655

2656
  /* compute cellsize/res from bbox and raster size. */
2657
  if ((params->width == 0 || params->height == 0) && params->resx != 0 &&
40✔
2658
      params->resy != 0) {
24✔
2659

2660
    /* WCS 1.0 boundbox is edge of pixel oriented. */
2661
    if (strncasecmp(params->version, "1.0", 3) == 0) {
24✔
2662
      params->width =
5✔
2663
          (int)((params->bbox.maxx - params->bbox.minx) / params->resx + 0.5);
5✔
2664
      params->height =
5✔
2665
          (int)((params->bbox.maxy - params->bbox.miny) / params->resy + 0.5);
5✔
2666
    } else {
2667
      params->width =
19✔
2668
          (int)((params->bbox.maxx - params->bbox.minx) / params->resx +
19✔
2669
                1.000001);
2670
      params->height =
19✔
2671
          (int)((params->bbox.maxy - params->bbox.miny) / params->resy +
19✔
2672
                1.000001);
2673

2674
      /* recompute bounding box so we get exactly the origin and
2675
         resolution requested. */
2676
      params->bbox.maxx =
19✔
2677
          params->bbox.minx + (params->width - 1) * params->resx;
19✔
2678
      params->bbox.miny =
19✔
2679
          params->bbox.maxy - (params->height - 1) * params->resy;
19✔
2680
    }
2681
  }
2682

2683
  /* are we still underspecified?  */
2684
  if ((params->width == 0 || params->height == 0) &&
40✔
2685
      (params->resx == 0.0 || params->resy == 0.0)) {
×
2686
    msWCSFreeCoverageMetadata(&cm);
×
2687
    msSetError(MS_WCSERR,
×
2688
               "A non-zero RESX/RESY or WIDTH/HEIGHT is required but neither "
2689
               "was provided.",
2690
               "msWCSGetCoverage()");
2691
    return msWCSException(map, "MissingParameterValue",
×
2692
                          "width/height/resx/resy", params->version);
×
2693
  }
2694

2695
  map->cellsize = params->resx;
40✔
2696

2697
  /* Do we need to force special handling?  */
2698
  if (fabs(params->resx / params->resy - 1.0) > 0.001) {
40✔
2699
    map->gt.need_geotransform = MS_TRUE;
5✔
2700
    if (map->debug)
5✔
2701
      msDebug("RESX and RESY don't match.  Using geotransform/resample.\n");
×
2702
  }
2703

2704
  /* Do we have a specified interpolation method */
2705
  if (params->interpolation != NULL) {
40✔
2706
    if (strncasecmp(params->interpolation, "NEAREST", 7) == 0)
2✔
2707
      msLayerSetProcessingKey(lp, "RESAMPLE", "NEAREST");
×
2708
    else if (strcasecmp(params->interpolation, "BILINEAR") == 0)
2✔
2709
      msLayerSetProcessingKey(lp, "RESAMPLE", "BILINEAR");
2✔
2710
    else if (strcasecmp(params->interpolation, "AVERAGE") == 0)
×
2711
      msLayerSetProcessingKey(lp, "RESAMPLE", "AVERAGE");
×
2712
    else {
2713
      msWCSFreeCoverageMetadata(&cm);
×
2714
      msSetError(
×
2715
          MS_WCSERR,
2716
          "INTERPOLATION=%s specifies an unsupported interpolation method.",
2717
          "msWCSGetCoverage()", params->interpolation);
2718
      return msWCSException(map, "InvalidParameterValue", "interpolation",
×
2719
                            params->version);
×
2720
    }
2721
  }
2722

2723
  /* apply region and size to map object.  */
2724
  map->width = params->width;
40✔
2725
  map->height = params->height;
40✔
2726

2727
  /* Are we exceeding the MAXSIZE limit on result size? */
2728
  if (map->width > map->maxsize || map->height > map->maxsize) {
40✔
2729
    msWCSFreeCoverageMetadata(&cm);
1✔
2730
    msSetError(MS_WCSERR,
1✔
2731
               "Raster size out of range, width and height of resulting "
2732
               "coverage must be no more than MAXSIZE=%d.",
2733
               "msWCSGetCoverage()", map->maxsize);
2734

2735
    return msWCSException(map, "InvalidParameterValue", "width/height",
1✔
2736
                          params->version);
1✔
2737
  }
2738

2739
  /* adjust OWS BBOX to MapServer's pixel model */
2740
  if (strncasecmp(params->version, "1.0", 3) == 0) {
39✔
2741
    params->bbox.minx += params->resx * 0.5;
20✔
2742
    params->bbox.miny += params->resy * 0.5;
20✔
2743
    params->bbox.maxx -= params->resx * 0.5;
20✔
2744
    params->bbox.maxy -= params->resy * 0.5;
20✔
2745
  }
2746

2747
  map->extent = params->bbox;
39✔
2748

2749
  map->cellsize = params->resx; /* pick one, MapServer only supports square
39✔
2750
                                   cells (what about msAdjustExtent here!) */
2751

2752
  if (params->width == 1 || params->height == 1)
39✔
2753
    msMapComputeGeotransformEx(map, params->resx, params->resy);
×
2754
  else
2755
    msMapComputeGeotransform(map);
39✔
2756

2757
  /* Do we need to fake out stuff for rotated support? */
2758
  if (map->gt.need_geotransform)
39✔
2759
    msMapSetFakedExtent(map);
5✔
2760

2761
  map->projection.gt = map->gt;
39✔
2762

2763
  /* check for overlap */
2764

2765
  /* get extent of bbox passed, and reproject */
2766
  reqextent.minx = map->extent.minx;
39✔
2767
  reqextent.miny = map->extent.miny;
39✔
2768
  reqextent.maxx = map->extent.maxx;
39✔
2769
  reqextent.maxy = map->extent.maxy;
39✔
2770

2771
  /* reproject incoming bbox */
2772
  msProjectRect(&map->projection, &lp->projection, &(reqextent));
39✔
2773

2774
  /* get extent of layer */
2775
  covextent.minx = cm.extent.minx;
39✔
2776
  covextent.miny = cm.extent.miny;
39✔
2777
  covextent.maxx = cm.extent.maxx;
39✔
2778
  covextent.maxy = cm.extent.maxy;
39✔
2779

2780
  if (msRectOverlap(&reqextent, &covextent) == MS_FALSE) {
39✔
2781
    msWCSFreeCoverageMetadata(&cm);
1✔
2782
    msSetError(MS_WCSERR,
1✔
2783
               "Requested BBOX (%.15g,%.15g,%.15g,%.15g) is outside requested "
2784
               "coverage BBOX (%.15g,%.15g,%.15g,%.15g)",
2785
               "msWCSGetCoverage()", reqextent.minx, reqextent.miny,
2786
               reqextent.maxx, reqextent.maxy, covextent.minx, covextent.miny,
2787
               covextent.maxx, covextent.maxy);
2788
    return msWCSException(map, "NoApplicableCode", "bbox", params->version);
1✔
2789
  }
2790

2791
  /* check and make sure there is a format, and that it's valid (TODO: make sure
2792
   * in the layer metadata) */
2793
  if (!params->format) {
38✔
2794
    msWCSFreeCoverageMetadata(&cm);
1✔
2795
    msSetError(MS_WCSERR, "Missing required FORMAT parameter.",
1✔
2796
               "msWCSGetCoverage()");
2797
    return msWCSException(map, "MissingParameterValue", "format",
1✔
2798
                          params->version);
1✔
2799
  }
2800
  msApplyDefaultOutputFormats(map);
37✔
2801
  if (msGetOutputFormatIndex(map, params->format) == -1) {
37✔
2802
    msWCSFreeCoverageMetadata(&cm);
1✔
2803
    msSetError(MS_WCSERR, "Unrecognized value for the FORMAT parameter.",
1✔
2804
               "msWCSGetCoverage()");
2805
    return msWCSException(map, "InvalidParameterValue", "format",
1✔
2806
                          params->version);
1✔
2807
  }
2808

2809
  /* create a temporary outputformat (we likely will need to tweak parts) */
2810
  format = msCloneOutputFormat(msSelectOutputFormat(map, params->format));
36✔
2811
  msApplyOutputFormat(&(map->outputformat), format, MS_NOOVERRIDE);
36✔
2812

2813
  if (!bandlist) { /* build a bandlist (default is ALL bands) */
36✔
2814
    bufferSize = cm.bandcount * 30 + 30;
35✔
2815
    bandlist = (char *)msSmallMalloc(bufferSize);
35✔
2816
    strcpy(bandlist, "1");
2817
    for (i = 1; i < cm.bandcount; i++)
35✔
2818
      snprintf(bandlist + strlen(bandlist), bufferSize - strlen(bandlist),
×
2819
               ",%d", i + 1);
2820
  }
2821

2822
  /* apply nullvalue to the output format object if we have it */
2823
  if ((value = msOWSLookupMetadata(&(lp->metadata), "CO",
36✔
2824
                                   "rangeset_nullvalue")) != NULL) {
2825
    msSetOutputFormatOption(map->outputformat, "NULLVALUE", value);
30✔
2826
  }
2827

2828
  msLayerSetProcessingKey(lp, "BANDS", bandlist);
36✔
2829
  snprintf(numbands, sizeof(numbands), "%d", msCountChars(bandlist, ',') + 1);
36✔
2830
  msSetOutputFormatOption(map->outputformat, "BAND_COUNT", numbands);
36✔
2831

2832
  msWCSApplyLayerCreationOptions(lp, map->outputformat, bandlist);
36✔
2833
  msWCSApplyLayerMetadataItemOptions(lp, map->outputformat, bandlist);
36✔
2834

2835
  if (lp->tileindex == NULL && lp->data != NULL && strlen(lp->data) > 0 &&
36✔
2836
      lp->connectiontype != MS_KERNELDENSITY) {
6✔
2837
    if (msDrawRasterLayerLowCheckIfMustDraw(map, lp)) {
6✔
2838
      char *decrypted_path = NULL;
6✔
2839
      char szPath[MS_MAXPATHLEN];
2840
      hDS = (GDALDatasetH)msDrawRasterLayerLowOpenDataset(
6✔
2841
          map, lp, lp->data, szPath, &decrypted_path);
6✔
2842
      msFree(decrypted_path);
6✔
2843
      if (hDS) {
6✔
2844
        msWCSApplyDatasetMetadataAsCreationOptions(lp, map->outputformat,
6✔
2845
                                                   bandlist, hDS);
2846
        msWCSApplySourceDatasetMetadata(lp, map->outputformat, bandlist, hDS);
6✔
2847
      }
2848
    } else {
2849
      doDrawRasterLayerDraw = MS_FALSE;
2850
    }
2851
  }
2852

2853
  free(bandlist);
36✔
2854

2855
  if (lp->mask) {
36✔
2856
    int maskLayerIdx = msGetLayerIndex(map, lp->mask);
11✔
2857
    layerObj *maskLayer;
2858
    outputFormatObj *altFormat;
2859
    if (maskLayerIdx == -1) {
11✔
2860
      msWCSFreeCoverageMetadata(&cm);
×
2861
      msSetError(MS_MISCERR, "Layer (%s) references unknown mask layer (%s)",
×
2862
                 "msDrawLayer()", lp->name, lp->mask);
2863
      msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2864
      return msWCSException(map, NULL, NULL, params->version);
×
2865
    }
2866
    maskLayer = GET_LAYER(map, maskLayerIdx);
11✔
2867
    if (!maskLayer->maskimage) {
11✔
2868
      int i, retcode;
2869
      int origstatus, origlabelcache;
2870
      char *origImageType = msStrdup(map->imagetype);
11✔
2871
      altFormat = msSelectOutputFormat(map, "png24");
11✔
2872
      msInitializeRendererVTable(altFormat);
11✔
2873
      /* TODO: check the png24 format hasn't been tampered with, i.e. it's agg
2874
       */
2875
      maskLayer->maskimage = msImageCreate(
11✔
2876
          map->width, map->height, altFormat, map->web.imagepath,
2877
          map->web.imageurl, map->resolution, map->defresolution, NULL);
2878
      if (!maskLayer->maskimage) {
11✔
2879
        msWCSFreeCoverageMetadata(&cm);
×
2880
        msSetError(MS_MISCERR, "Unable to initialize mask image.",
×
2881
                   "msDrawLayer()");
2882
        msFree(origImageType);
×
2883
        msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2884
        return msWCSException(map, NULL, NULL, params->version);
×
2885
      }
2886

2887
      /*
2888
       * force the masked layer to status on, and turn off the labelcache so
2889
       * that eventual labels are added to the temporary image instead of being
2890
       * added to the labelcache
2891
       */
2892
      origstatus = maskLayer->status;
11✔
2893
      origlabelcache = maskLayer->labelcache;
11✔
2894
      maskLayer->status = MS_ON;
11✔
2895
      maskLayer->labelcache = MS_OFF;
11✔
2896

2897
      /* draw the mask layer in the temporary image */
2898
      retcode = msDrawLayer(map, maskLayer, maskLayer->maskimage);
11✔
2899
      maskLayer->status = origstatus;
11✔
2900
      maskLayer->labelcache = origlabelcache;
11✔
2901
      if (retcode != MS_SUCCESS) {
11✔
2902
        msWCSFreeCoverageMetadata(&cm);
×
2903
        /* set the imagetype from the original outputformat back (it was removed
2904
         * by msSelectOutputFormat() */
2905
        msFree(map->imagetype);
×
2906
        map->imagetype = origImageType;
×
2907
        msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2908
        return msWCSException(map, NULL, NULL, params->version);
×
2909
      }
2910
      /*
2911
       * hack to work around bug #3834: if we have use an alternate renderer,
2912
       * the symbolset may contain symbols that reference it. We want to remove
2913
       * those references before the altFormat is destroyed to avoid a segfault
2914
       * and/or a leak, and so the the main renderer doesn't pick the cache up
2915
       * thinking it's for him.
2916
       */
2917
      for (i = 0; i < map->symbolset.numsymbols; i++) {
22✔
2918
        if (map->symbolset.symbol[i] != NULL) {
11✔
2919
          symbolObj *s = map->symbolset.symbol[i];
2920
          if (s->renderer == MS_IMAGE_RENDERER(maskLayer->maskimage)) {
11✔
2921
            MS_IMAGE_RENDERER(maskLayer->maskimage)->freeSymbol(s);
×
2922
            s->renderer = NULL;
×
2923
          }
2924
        }
2925
      }
2926
      /* set the imagetype from the original outputformat back (it was removed
2927
       * by msSelectOutputFormat() */
2928
      msFree(map->imagetype);
11✔
2929
      map->imagetype = origImageType;
11✔
2930
    }
2931
  }
2932

2933
  /* create the image object  */
2934
  if (!map->outputformat) {
36✔
2935
    msWCSFreeCoverageMetadata(&cm);
×
2936
    msSetError(MS_WCSERR, "The map outputformat is missing!",
×
2937
               "msWCSGetCoverage()");
2938
    msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2939
    return msWCSException(map, NULL, NULL, params->version);
×
2940
  } else if (MS_RENDERER_RAWDATA(map->outputformat) ||
36✔
2941
             MS_RENDERER_PLUGIN(map->outputformat)) {
2942
    image = msImageCreate(map->width, map->height, map->outputformat,
36✔
2943
                          map->web.imagepath, map->web.imageurl,
2944
                          map->resolution, map->defresolution, NULL);
2945
  } else {
2946
    msWCSFreeCoverageMetadata(&cm);
×
2947
    msSetError(MS_WCSERR, "Map outputformat not supported for WCS!",
×
2948
               "msWCSGetCoverage()");
2949
    msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2950
    return msWCSException(map, NULL, NULL, params->version);
×
2951
  }
2952

2953
  if (image == NULL) {
36✔
2954
    msWCSFreeCoverageMetadata(&cm);
×
2955
    msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2956
    return msWCSException(map, NULL, NULL, params->version);
×
2957
  }
2958
  if (MS_RENDERER_RAWDATA(map->outputformat)) {
36✔
2959
    if (doDrawRasterLayerDraw) {
29✔
2960
      status = msDrawRasterLayerLowWithDataset(map, lp, image, NULL, hDS);
29✔
2961
    } else {
2962
      status = MS_SUCCESS;
2963
    }
2964
  } else {
2965
    status = MS_IMAGE_RENDERER(image)->getRasterBufferHandle(image, &rb);
7✔
2966
    if (MS_UNLIKELY(status == MS_FAILURE)) {
7✔
2967
      msWCSFreeCoverageMetadata(&cm);
×
2968
      msDrawRasterLayerLowCloseDataset(lp, hDS);
×
2969
      return MS_FAILURE;
×
2970
    }
2971

2972
    /* Actually produce the "grid". */
2973
    if (doDrawRasterLayerDraw) {
7✔
2974
      status = msDrawRasterLayerLowWithDataset(map, lp, image, &rb, hDS);
7✔
2975
    } else {
2976
      status = MS_SUCCESS;
2977
    }
2978
  }
2979
  msDrawRasterLayerLowCloseDataset(lp, hDS);
36✔
2980

2981
  if (status != MS_SUCCESS) {
36✔
2982
    msWCSFreeCoverageMetadata(&cm);
×
2983
    msFreeImage(image);
×
2984
    return msWCSException(map, NULL, NULL, params->version);
×
2985
  }
2986

2987
  if (strncmp(params->version, "1.1", 3) == 0) {
36✔
2988
    msWCSReturnCoverage11(params, map, image);
17✔
2989
  } else { /* WCS 1.0.0 - just return the binary data with a content type */
2990
    const char *fo_filename;
2991

2992
    /* Do we have a predefined filename? */
2993
    fo_filename = msGetOutputFormatOption(format, "FILENAME", NULL);
19✔
2994
    if (fo_filename)
19✔
2995
      msIO_setHeader("Content-Disposition", "attachment; filename=%s",
8✔
2996
                     fo_filename);
2997

2998
    /* Emit back to client. */
2999
    msOutputFormatResolveFromImage(map, image);
19✔
3000
    msIO_setHeader("Content-Type", "%s", MS_IMAGE_MIME_TYPE(map->outputformat));
38✔
3001
    msIO_sendHeaders();
19✔
3002
    status = msSaveImage(map, image, NULL);
19✔
3003

3004
    if (status != MS_SUCCESS) {
19✔
3005
      /* unfortunately, the image content type will have already been sent
3006
         but that is hard for us to avoid.  The main error that could happen
3007
         here is a misconfigured tmp directory or running out of space. */
3008
      msWCSFreeCoverageMetadata(&cm);
×
3009
      return msWCSException(map, NULL, NULL, params->version);
×
3010
    }
3011
  }
3012

3013
  /* Cleanup */
3014
  msFreeImage(image);
36✔
3015
  msApplyOutputFormat(&(map->outputformat), NULL, MS_NOOVERRIDE);
36✔
3016
  /* msFreeOutputFormat(format); */
3017

3018
  msWCSFreeCoverageMetadata(&cm);
36✔
3019
  return status;
3020
}
3021
#endif /* def USE_WCS_SVR */
3022

3023
/************************************************************************/
3024
/*                           msWCSDispatch()                            */
3025
/*                                                                      */
3026
/*      Entry point for WCS requests                                    */
3027
/************************************************************************/
3028

3029
int msWCSDispatch(mapObj *map, cgiRequestObj *request,
310✔
3030
                  owsRequestObj *ows_request) {
3031
#if defined(USE_WCS_SVR)
3032
  wcs20ParamsObj *params20 = NULL;
3033
  int status, retVal, operation;
3034

3035
  /* If SERVICE is not set or not WCS exit gracefully. */
3036
  if (ows_request->service == NULL || !EQUAL(ows_request->service, "WCS")) {
310✔
3037
    return MS_DONE;
3038
  }
3039

3040
  /* If no REQUEST is set, exit with an error */
3041
  if (ows_request->request == NULL) {
310✔
3042
    /* The request has to be set. */
3043
    msSetError(MS_WCSERR, "Missing REQUEST parameter", "msWCSDispatch()");
1✔
3044
    return msWCSException(map, "MissingParameterValue", "request",
1✔
3045
                          ows_request->version);
1✔
3046
  }
3047

3048
  if (EQUAL(ows_request->request, "GetCapabilities")) {
309✔
3049
    operation = MS_WCS_GET_CAPABILITIES;
3050
  } else if (EQUAL(ows_request->request, "DescribeCoverage")) {
234✔
3051
    operation = MS_WCS_DESCRIBE_COVERAGE;
3052
  } else if (EQUAL(ows_request->request, "GetCoverage")) {
224✔
3053
    operation = MS_WCS_GET_COVERAGE;
3054
  } else {
3055
    msSetError(MS_WCSERR, "Invalid REQUEST parameter \"%s\"", "msWCSDispatch()",
5✔
3056
               ows_request->request);
3057
    return msWCSException(map, "InvalidParameterValue", "request",
5✔
3058
                          ows_request->version);
5✔
3059
  }
3060

3061
  /* Check the number of enabled layers for the REQUEST */
3062
  msOWSRequestLayersEnabled(map, "C", ows_request->request, ows_request);
304✔
3063
  if (ows_request->numlayers == 0) {
304✔
3064
    int caps_globally_enabled = MS_FALSE, disabled = MS_FALSE;
7✔
3065
    const char *enable_request;
3066
    if (operation == MS_WCS_GET_CAPABILITIES) {
7✔
3067
      enable_request =
3068
          msOWSLookupMetadata(&map->web.metadata, "OC", "enable_request");
6✔
3069
      caps_globally_enabled = msOWSParseRequestMetadata(
6✔
3070
          enable_request, "GetCapabilities", &disabled);
3071
    }
3072

3073
    if (caps_globally_enabled == MS_FALSE) {
6✔
3074
      msSetError(MS_WCSERR,
3✔
3075
                 "WCS request not enabled. Check "
3076
                 "wcs/ows_enable_request settings.",
3077
                 "msWCSDispatch()");
3078
      return msWCSException(map, "InvalidParameterValue", "request",
3✔
3079
                            ows_request->version);
3✔
3080
    }
3081
  }
3082

3083
  /* Check the VERSION parameter */
3084
  if (ows_request->version == NULL) {
301✔
3085
    /* If the VERSION parameter is not set, it is either */
3086
    /* an error (Describe and GetCoverage), or it has to */
3087
    /* be determined (GetCapabilities). To determine the */
3088
    /* version, the request has to be fully parsed to    */
3089
    /* obtain the ACCEPTVERSIONS parameter. If this is   */
3090
    /* present also, set version to "2.0.1".             */
3091

3092
    if (operation == MS_WCS_GET_CAPABILITIES) {
2✔
3093
      /* Parse it as if it was a WCS 2.0 request */
3094
      wcs20ParamsObjPtr params_tmp = msWCSCreateParamsObj20();
2✔
3095
      status = msWCSParseRequest20(map, request, ows_request, params_tmp);
2✔
3096
      if (status == MS_FAILURE) {
2✔
3097
        msWCSFreeParamsObj20(params_tmp);
×
3098
        return msWCSException(map, "InvalidParameterValue", "request", "2.0.1");
×
3099
      }
3100

3101
      /* VERSION negotiation */
3102
      if (params_tmp->accept_versions != NULL) {
2✔
3103
        /* choose highest acceptable */
3104
        int i, highest_version = 0;
3105
        char version_string[OWS_VERSION_MAXLEN];
3106
        for (i = 0; params_tmp->accept_versions[i] != NULL; ++i) {
×
3107
          int version = msOWSParseVersionString(params_tmp->accept_versions[i]);
×
3108
          if (version == OWS_VERSION_BADFORMAT) {
×
3109
            msWCSFreeParamsObj20(params_tmp);
×
3110
            return msWCSException(map, "InvalidParameterValue", "version",
×
3111
                                  NULL);
×
3112
          }
3113
          if (version > highest_version) {
×
3114
            highest_version = version;
3115
          }
3116
        }
3117
        msOWSGetVersionString(highest_version, version_string);
×
3118
        params_tmp->version = msStrdup(version_string);
×
3119
        ows_request->version = msStrdup(version_string);
×
3120
      } else {
3121
        /* set to highest acceptable */
3122
        params_tmp->version = msStrdup("2.0.1");
2✔
3123
        ows_request->version = msStrdup("2.0.1");
2✔
3124
      }
3125

3126
      /* check if we can keep the params object */
3127
      if (EQUAL(params_tmp->version, "2.0.1")) {
2✔
3128
        params20 = params_tmp;
3129
      } else {
3130
        msWCSFreeParamsObj20(params_tmp);
×
3131
      }
3132
    } else { /* operation != GetCapabilities */
3133
      /* VERSION is mandatory in other requests */
3134
      msSetError(MS_WCSERR, "VERSION parameter not set.", "msWCSDispatch()");
×
3135
      return msWCSException(map, "InvalidParameterValue", "version", NULL);
×
3136
    }
3137
  } else {
3138
    /* Parse the VERSION parameter */
3139
    int requested_version = msOWSParseVersionString(ows_request->version);
299✔
3140
    if (requested_version == OWS_VERSION_BADFORMAT) {
299✔
3141
      /* Return an error if the VERSION is */
3142
      /* in an unsupported format.         */
3143
      return msWCSException(map, "InvalidParameterValue", "version", NULL);
2✔
3144
    }
3145

3146
    if (operation == MS_WCS_GET_CAPABILITIES) {
297✔
3147
      /* In case of GetCapabilities, make  */
3148
      char version_string[OWS_VERSION_MAXLEN];
3149
      int version, supported_versions[] = {OWS_2_0_1, OWS_2_0_0, OWS_1_1_2,
70✔
3150
                                           OWS_1_1_1, OWS_1_1_0, OWS_1_0_0};
3151
      version = msOWSNegotiateVersion(requested_version, supported_versions,
70✔
3152
                                      sizeof(supported_versions) / sizeof(int));
3153
      msOWSGetVersionString(version, version_string);
70✔
3154
      msFree(ows_request->version);
70✔
3155
      ows_request->version = msStrdup(version_string);
70✔
3156
    }
3157
  }
3158

3159
  /* VERSION specific request handler */
3160
  if (strcmp(ows_request->version, "1.0.0") == 0 ||
299✔
3161
      strcmp(ows_request->version, "1.1.0") == 0 ||
259✔
3162
      strcmp(ows_request->version, "1.1.1") == 0 ||
240✔
3163
      strcmp(ows_request->version, "1.1.2") == 0) {
218✔
3164
    auto paramsTmp = msWCSCreateParams();
81✔
3165
    status = msWCSParseRequest(request, paramsTmp, map);
81✔
3166
    if (status == MS_FAILURE) {
81✔
3167
      msWCSFreeParams(paramsTmp);
×
3168
      free(paramsTmp);
×
3169
      return msWCSException(map, "InvalidParameterValue", "request", "2.0");
×
3170
    }
3171

3172
    retVal = MS_FAILURE;
3173
    if (operation == MS_WCS_GET_CAPABILITIES) {
81✔
3174
      retVal = msWCSGetCapabilities(map, paramsTmp, request, ows_request);
31✔
3175
    } else if (operation == MS_WCS_DESCRIBE_COVERAGE) {
50✔
3176
      retVal = msWCSDescribeCoverage(map, paramsTmp, ows_request, request);
6✔
3177
    } else if (operation == MS_WCS_GET_COVERAGE) {
3178
      retVal = msWCSGetCoverage(map, request, paramsTmp, ows_request);
44✔
3179
    }
3180
    msWCSFreeParams(paramsTmp);
81✔
3181
    free(paramsTmp);
81✔
3182
    return retVal;
81✔
3183
  } else if (strcmp(ows_request->version, "2.0.0") == 0 ||
218✔
3184
             strcmp(ows_request->version, "2.0.1") == 0) {
185✔
3185
#if defined(USE_LIBXML2)
3186
    int i;
3187

3188
    if (params20 == NULL) {
212✔
3189
      params20 = msWCSCreateParamsObj20();
210✔
3190
      status = msWCSParseRequest20(map, request, ows_request, params20);
210✔
3191
      if (status == MS_FAILURE) {
210✔
3192
        msWCSFreeParamsObj20(params20);
2✔
3193
        return msWCSException(map, "InvalidParameterValue", "request", "2.0.1");
2✔
3194
      } else if (status == MS_DONE) {
208✔
3195
        /* MS_DONE means, that the exception has already been written to the IO
3196
          buffer.
3197
        */
3198
        msWCSFreeParamsObj20(params20);
2✔
3199
        return MS_FAILURE;
2✔
3200
      }
3201
    }
3202

3203
    /* check if all layer names are valid NCNames */
3204
    for (i = 0; i < map->numlayers; ++i) {
504✔
3205
      if (!msWCSIsLayerSupported(map->layers[i]))
296✔
3206
        continue;
32✔
3207

3208
      /* Check if each layers name is a valid NCName. */
3209
      if (msEvalRegex("^[a-zA-z_][a-zA-Z0-9_.-]*$", map->layers[i]->name) ==
264✔
3210
          MS_FALSE) {
3211
        msSetError(MS_WCSERR, "Layer name '%s' is not a valid NCName.",
×
3212
                   "msWCSDispatch()", map->layers[i]->name);
×
3213
        msWCSFreeParamsObj20(params20);
×
3214
        return msWCSException(map, "mapserv", "Internal", "2.0.1");
×
3215
      }
3216
    }
3217

3218
    /* Call operation specific functions */
3219
    if (operation == MS_WCS_GET_CAPABILITIES) {
208✔
3220
      retVal = msWCSGetCapabilities20(map, request, params20, ows_request);
41✔
3221
    } else if (operation == MS_WCS_DESCRIBE_COVERAGE) {
167✔
3222
      retVal = msWCSDescribeCoverage20(map, params20, ows_request);
4✔
3223
    } else if (operation == MS_WCS_GET_COVERAGE) {
3224
      retVal = msWCSGetCoverage20(map, request, params20, ows_request);
163✔
3225
    } else {
3226
      msSetError(MS_WCSERR, "Invalid request '%s'.", "msWCSDispatch20()",
3227
                 ows_request->request);
3228
      retVal =
3229
          msWCSException20(map, "InvalidParameterValue", "request", "2.0.1");
3230
    }
3231
    /* clean up */
3232
    msWCSFreeParamsObj20(params20);
208✔
3233
    return retVal;
208✔
3234
#else      /* def USE_LIBXML2 */
3235
    msSetError(MS_WCSERR,
3236
               "WCS 2.0 needs mapserver to be compiled with libxml2.",
3237
               "msWCSDispatch()");
3238
    return msWCSException(map, "mapserv", "NoApplicableCode", "2.0.1");
3239
#endif     /* def USE_LIBXML2 */
3240
  } else { /* unsupported version */
3241
    msSetError(MS_WCSERR, "WCS Server does not support VERSION %s.",
6✔
3242
               "msWCSDispatch()", ows_request->version);
3243
    return msWCSException(map, "InvalidParameterValue", "version",
6✔
3244
                          ows_request->version);
6✔
3245
  }
3246

3247
#else
3248
  msSetError(MS_WCSERR, "WCS server support is not available.",
3249
             "msWCSDispatch()");
3250
  return MS_FAILURE;
3251
#endif
3252
}
3253

3254
/************************************************************************/
3255
/*                      msWCSGetCoverageMetadata()                      */
3256
/************************************************************************/
3257

3258
#ifdef USE_WCS_SVR
3259

3260
void msWCSFreeCoverageMetadata(coverageMetadataObj *cm) {
69✔
3261
  msFree(cm->srs_epsg);
69✔
3262
}
69✔
3263

3264
int msWCSGetCoverageMetadata(layerObj *layer, coverageMetadataObj *cm) {
69✔
3265
  char *srs_urn = NULL;
3266
  int i = 0;
3267
  if (msCheckParentPointer(layer->map, "map") == MS_FAILURE)
69✔
3268
    return MS_FAILURE;
3269

3270
  /* -------------------------------------------------------------------- */
3271
  /*      Get the SRS in WCS 1.0 format (eg. EPSG:n)                      */
3272
  /* -------------------------------------------------------------------- */
3273
  msOWSGetEPSGProj(&(layer->projection), &(layer->metadata), "CO", MS_TRUE,
69✔
3274
                   &(cm->srs_epsg));
3275
  if (!cm->srs_epsg) {
69✔
3276
    msOWSGetEPSGProj(&(layer->map->projection), &(layer->map->web.metadata),
×
3277
                     "CO", MS_TRUE, &(cm->srs_epsg));
3278
    if (!cm->srs_epsg) {
×
3279
      msSetError(MS_WCSERR,
×
3280
                 "Unable to determine the SRS for this layer, no projection "
3281
                 "defined and no metadata available.",
3282
                 "msWCSGetCoverageMetadata()");
3283
      return MS_FAILURE;
×
3284
    }
3285
  }
3286

3287
  /* -------------------------------------------------------------------- */
3288
  /*      Get the SRS in urn format.                                      */
3289
  /* -------------------------------------------------------------------- */
3290
  if ((srs_urn = msOWSGetProjURN(&(layer->projection), &(layer->metadata), "CO",
69✔
3291
                                 MS_TRUE)) == NULL) {
3292
    srs_urn = msOWSGetProjURN(&(layer->map->projection),
×
3293
                              &(layer->map->web.metadata), "CO", MS_TRUE);
×
3294
  }
3295

3296
  if (srs_urn != NULL) {
×
3297
    if (strlen(srs_urn) > sizeof(cm->srs_urn) - 1) {
69✔
3298
      msSetError(MS_WCSERR, "SRS URN too long!", "msWCSGetCoverageMetadata()");
×
3299
      return MS_FAILURE;
×
3300
    }
3301

3302
    strcpy(cm->srs_urn, srs_urn);
69✔
3303
    msFree(srs_urn);
69✔
3304
  } else
3305
    cm->srs_urn[0] = '\0';
×
3306

3307
  /* -------------------------------------------------------------------- */
3308
  /*      If we have "virtual dataset" metadata on the layer, then use    */
3309
  /*      that in preference to inspecting the file(s).                   */
3310
  /*      We require extent and either size or resolution.                */
3311
  /* -------------------------------------------------------------------- */
3312
  if (msOWSLookupMetadata(&(layer->metadata), "CO", "extent") != NULL &&
136✔
3313
      (msOWSLookupMetadata(&(layer->metadata), "CO", "resolution") != NULL ||
69✔
3314
       msOWSLookupMetadata(&(layer->metadata), "CO", "size") != NULL)) {
2✔
3315
    const char *value;
3316

3317
    /* get extent */
3318
    cm->extent.minx = 0.0;
66✔
3319
    cm->extent.maxx = 0.0;
66✔
3320
    cm->extent.miny = 0.0;
66✔
3321
    cm->extent.maxy = 0.0;
66✔
3322
    if (msOWSGetLayerExtent(layer->map, layer, "CO", &cm->extent) == MS_FAILURE)
66✔
3323
      return MS_FAILURE;
3324

3325
    /* get resolution */
3326
    cm->xresolution = 0.0;
66✔
3327
    cm->yresolution = 0.0;
66✔
3328
    if ((value = msOWSLookupMetadata(&(layer->metadata), "CO", "resolution")) !=
66✔
3329
        NULL) {
3330
      char **tokens;
3331
      int n;
3332

3333
      tokens = msStringSplit(value, ' ', &n);
65✔
3334
      if (tokens == NULL || n != 2) {
65✔
3335
        msSetError(MS_WCSERR,
×
3336
                   "Wrong number of arguments for wcs|ows_resolution metadata.",
3337
                   "msWCSGetCoverageMetadata()");
3338
        msFreeCharArray(tokens, n);
×
3339
        return MS_FAILURE;
×
3340
      }
3341
      cm->xresolution = atof(tokens[0]);
65✔
3342
      cm->yresolution = atof(tokens[1]);
65✔
3343
      msFreeCharArray(tokens, n);
65✔
3344
    }
3345

3346
    /* get Size (in pixels and lines) */
3347
    cm->xsize = 0;
66✔
3348
    cm->ysize = 0;
66✔
3349
    if ((value = msOWSLookupMetadata(&(layer->metadata), "CO", "size")) !=
66✔
3350
        NULL) {
3351
      char **tokens;
3352
      int n;
3353

3354
      tokens = msStringSplit(value, ' ', &n);
9✔
3355
      if (tokens == NULL || n != 2) {
9✔
3356
        msSetError(MS_WCSERR,
×
3357
                   "Wrong number of arguments for wcs|ows_size metadata.",
3358
                   "msWCSGetCoverageDomain()");
3359
        msFreeCharArray(tokens, n);
×
3360
        return MS_FAILURE;
×
3361
      }
3362
      cm->xsize = atoi(tokens[0]);
9✔
3363
      cm->ysize = atoi(tokens[1]);
9✔
3364
      msFreeCharArray(tokens, n);
9✔
3365
    }
3366

3367
    /* try to compute raster size */
3368
    if (cm->xsize == 0 && cm->ysize == 0 && cm->xresolution != 0.0 &&
66✔
3369
        cm->yresolution != 0.0 && cm->extent.minx != cm->extent.maxx &&
57✔
3370
        cm->extent.miny != cm->extent.maxy) {
57✔
3371
      cm->xsize =
57✔
3372
          (int)((cm->extent.maxx - cm->extent.minx) / cm->xresolution + 0.5);
57✔
3373
      cm->ysize = (int)fabs(
57✔
3374
          (cm->extent.maxy - cm->extent.miny) / cm->yresolution + 0.5);
57✔
3375
    }
3376

3377
    /* try to compute raster resolution */
3378
    if ((cm->xresolution == 0.0 || cm->yresolution == 0.0) && cm->xsize != 0 &&
66✔
3379
        cm->ysize != 0) {
1✔
3380
      cm->xresolution = (cm->extent.maxx - cm->extent.minx) / cm->xsize;
1✔
3381
      cm->yresolution = (cm->extent.maxy - cm->extent.miny) / cm->ysize;
1✔
3382
    }
3383

3384
    /* do we have information to do anything */
3385
    if (cm->xresolution == 0.0 || cm->yresolution == 0.0 || cm->xsize == 0 ||
66✔
3386
        cm->ysize == 0) {
66✔
3387
      msSetError(MS_WCSERR,
×
3388
                 "Failed to collect extent and resolution for WCS coverage "
3389
                 "from metadata for layer '%s'.  Need value wcs|ows_resolution "
3390
                 "or wcs|ows_size values.",
3391
                 "msWCSGetCoverageMetadata()", layer->name);
3392
      return MS_FAILURE;
×
3393
    }
3394

3395
    /* compute geotransform */
3396
    cm->geotransform[0] = cm->extent.minx;
66✔
3397
    cm->geotransform[1] = cm->xresolution;
66✔
3398
    cm->geotransform[2] = 0.0;
66✔
3399
    cm->geotransform[3] = cm->extent.maxy;
66✔
3400
    cm->geotransform[4] = 0.0;
66✔
3401
    cm->geotransform[5] = -fabs(cm->yresolution);
66✔
3402

3403
    /* get bands count, or assume 1 if not found */
3404
    cm->bandcount = 1;
66✔
3405
    if ((value = msOWSLookupMetadata(&(layer->metadata), "CO", "bandcount")) !=
66✔
3406
        NULL) {
3407
      cm->bandcount = atoi(value);
58✔
3408
    }
3409

3410
    /* get bands type, or assume float if not found */
3411
    cm->imagemode = MS_IMAGEMODE_FLOAT32;
66✔
3412
    if ((value = msOWSLookupMetadata(&(layer->metadata), "CO", "imagemode")) !=
66✔
3413
        NULL) {
3414
      if (EQUAL(value, "INT16"))
57✔
3415
        cm->imagemode = MS_IMAGEMODE_INT16;
×
3416
      else if (EQUAL(value, "FLOAT32"))
57✔
3417
        cm->imagemode = MS_IMAGEMODE_FLOAT32;
5✔
3418
      else if (EQUAL(value, "BYTE"))
52✔
3419
        cm->imagemode = MS_IMAGEMODE_BYTE;
52✔
3420
      else {
3421
        msSetError(MS_WCSERR,
×
3422
                   "Content of wcs|ows_imagemode (%s) not recognised.  Should "
3423
                   "be one of BYTE, INT16 or FLOAT32.",
3424
                   "msWCSGetCoverageMetadata()", value);
3425
        return MS_FAILURE;
×
3426
      }
3427
    }
3428
    /* set color interpretation to undefined */
3429
    /* TODO: find better solution */
3430
    for (i = 0; i < 10; ++i) {
726✔
3431
      cm->bandinterpretation[i] = GDALGetColorInterpretationName(GCI_Undefined);
660✔
3432
    }
3433
  } else if (layer->data ==
3✔
3434
             NULL) { /* no virtual metadata, not ok unless we're talking 1
3435
                        image, hopefully we can fix that */
3436
    msSetError(
×
3437
        MS_WCSERR,
3438
        "RASTER Layer with no DATA statement and no WCS virtual dataset "
3439
        "metadata.  Tileindexed raster layers not supported for WCS without "
3440
        "virtual dataset metadata (cm->extent, wcs_res, wcs_size).",
3441
        "msWCSGetCoverageDomain()");
3442
    return MS_FAILURE;
×
3443
  } else { /* work from the file (e.g. DATA) */
3444
    GDALDatasetH hDS;
3445
    GDALRasterBandH hBand;
3446
    char szPath[MS_MAXPATHLEN];
3447
    char *decrypted_path;
3448

3449
    msGDALInitialize();
3✔
3450

3451
    msTryBuildPath3(szPath, layer->map->mappath, layer->map->shapepath,
3✔
3452
                    layer->data);
3✔
3453
    decrypted_path = msDecryptStringTokens(layer->map, szPath);
3✔
3454
    if (!decrypted_path)
3✔
3455
      return MS_FAILURE;
×
3456

3457
    msAcquireLock(TLOCK_GDAL);
3✔
3458
    {
3459
      char **connectionoptions =
3460
          msGetStringListFromHashTable(&(layer->connectionoptions));
3✔
3461
      hDS = GDALOpenEx(decrypted_path, GDAL_OF_RASTER, NULL,
3✔
3462
                       (const char *const *)connectionoptions, NULL);
3463
      CSLDestroy(connectionoptions);
3✔
3464
    }
3465
    if (hDS == NULL) {
3✔
3466
      const char *cpl_error_msg = CPLGetLastErrorMsg();
×
3467

3468
      /* we wish to avoid reporting decrypted paths */
3469
      if (cpl_error_msg != NULL &&
×
3470
          strstr(cpl_error_msg, decrypted_path) != NULL &&
×
3471
          strcmp(decrypted_path, szPath) != 0)
×
3472
        cpl_error_msg = NULL;
3473

3474
      if (cpl_error_msg == NULL)
3475
        cpl_error_msg = "";
3476

3477
      msReleaseLock(TLOCK_GDAL);
×
3478

3479
      msSetError(MS_IOERR, "%s", "msWCSGetCoverageMetadata()", cpl_error_msg);
×
3480

3481
      msFree(decrypted_path);
×
3482
      return MS_FAILURE;
×
3483
    }
3484
    msFree(decrypted_path);
3✔
3485

3486
    msGetGDALGeoTransform(hDS, layer->map, layer, cm->geotransform);
3✔
3487

3488
    cm->xsize = GDALGetRasterXSize(hDS);
3✔
3489
    cm->ysize = GDALGetRasterYSize(hDS);
3✔
3490

3491
    cm->extent.minx = cm->geotransform[0];
3✔
3492
    cm->extent.maxx = cm->geotransform[0] + cm->geotransform[1] * cm->xsize +
3✔
3493
                      cm->geotransform[2] * cm->ysize;
3✔
3494
    cm->extent.miny = cm->geotransform[3] + cm->geotransform[4] * cm->xsize +
3✔
3495
                      cm->geotransform[5] * cm->ysize;
3✔
3496
    cm->extent.maxy = cm->geotransform[3];
3✔
3497

3498
    cm->xresolution = cm->geotransform[1];
3✔
3499
    cm->yresolution = cm->geotransform[5];
3✔
3500

3501
    /* TODO: need to set resolution */
3502

3503
    cm->bandcount = GDALGetRasterCount(hDS);
3✔
3504

3505
    if (cm->bandcount == 0) {
3✔
3506
      msReleaseLock(TLOCK_GDAL);
×
3507
      msSetError(MS_WCSERR,
×
3508
                 "Raster file %s has no raster bands.  This cannot be used in "
3509
                 "a layer.",
3510
                 "msWCSGetCoverageMetadata()", layer->data);
3511
      return MS_FAILURE;
×
3512
    }
3513

3514
    hBand = GDALGetRasterBand(hDS, 1);
3✔
3515
    switch (GDALGetRasterDataType(hBand)) {
3✔
3516
    case GDT_Byte:
3✔
3517
      cm->imagemode = MS_IMAGEMODE_BYTE;
3✔
3518
      break;
3✔
3519
    case GDT_Int16:
×
3520
      cm->imagemode = MS_IMAGEMODE_INT16;
×
3521
      break;
×
3522
    default:
×
3523
      cm->imagemode = MS_IMAGEMODE_FLOAT32;
×
3524
      break;
×
3525
    }
3526

3527
    /* color interpretation */
3528
    for (i = 1; i <= 10 && i <= cm->bandcount; ++i) {
12✔
3529
      GDALColorInterp colorInterp;
3530
      hBand = GDALGetRasterBand(hDS, i);
9✔
3531
      colorInterp = GDALGetRasterColorInterpretation(hBand);
9✔
3532
      cm->bandinterpretation[i - 1] =
9✔
3533
          GDALGetColorInterpretationName(colorInterp);
9✔
3534
    }
3535

3536
    GDALClose(hDS);
3✔
3537
    msReleaseLock(TLOCK_GDAL);
3✔
3538
  }
3539

3540
  /* we must have the bounding box in lat/lon [WGS84(DD)/EPSG:4326] */
3541
  cm->llextent = cm->extent;
69✔
3542

3543
  /* Already in latlong .. use directly. */
3544
  if (layer->projection.proj != NULL &&
138✔
3545
      msProjIsGeographicCRS(&(layer->projection))) {
69✔
3546
    /* no change */
3547
  }
3548

3549
  else if (layer->projection.numargs > 0 &&
114✔
3550
           !msProjIsGeographicCRS(
57✔
3551
               &(layer->projection))) /* check the layer projection */
3552
    msProjectRect(&(layer->projection), NULL, &(cm->llextent));
57✔
3553

3554
  else if (layer->map->projection.numargs > 0 &&
×
3555
           !msProjIsGeographicCRS(
×
UNCOV
3556
               &(layer->map->projection))) /* check the map projection */
×
3557
    msProjectRect(&(layer->map->projection), NULL, &(cm->llextent));
×
3558

3559
  else { /* projection was specified in the metadata only (EPSG:... only at the
3560
            moment)  */
3561
    projectionObj proj;
3562
    char projstring[32];
3563

3564
    msInitProjection(&proj); /* or bad things happen */
×
3565
    msProjectionInheritContextFrom(&proj, &(layer->map->projection));
×
3566

3567
    snprintf(projstring, sizeof(projstring), "init=epsg:%.20s",
×
3568
             cm->srs_epsg + 5);
×
3569
    if (msLoadProjectionString(&proj, projstring) != 0)
×
3570
      return MS_FAILURE;
×
3571
    msProjectRect(&proj, NULL, &(cm->llextent));
×
3572
  }
3573

3574
  return MS_SUCCESS;
3575
}
3576
#endif /* def USE_WCS_SVR */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc