forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapwms.cpp
5168 lines (4554 loc) · 221 KB
/
mapwms.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* $Id$
*
* Project: MapServer
* Purpose: OpenGIS Web Mapping Service support implementation.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#define NEED_IGNORE_RET_VAL
#include "mapserver.h"
#include "maperror.h"
#include "mapthread.h"
#include "mapgml.h"
#include <ctype.h>
#include "maptemplate.h"
#include "mapows.h"
#include "mapogcsld.h"
#include "mapogcfilter.h"
#include "mapowscommon.h"
#include "maptime.h"
#include "mapproject.h"
#include <cassert>
#include <stdarg.h>
#include <time.h>
#include <string.h>
#include <set>
#include <string>
#include <vector>
#ifdef WIN32
#include <process.h>
#endif
/* ==================================================================
* WMS Server stuff.
* ================================================================== */
#ifdef USE_WMS_SVR
/*
** msWMSException()
**
** Report current MapServer error in requested format.
*/
static
int msWMSException(mapObj *map, int nVersion, const char *exception_code,
const char *wms_exception_format)
{
char *schemalocation = NULL;
/* Default to WMS 1.3.0 exceptions if version not set yet */
if (nVersion <= 0)
nVersion = OWS_1_3_0;
/* get scheam location */
schemalocation = msEncodeHTMLEntities( msOWSGetSchemasLocation(map) );
/* Establish default exception format depending on VERSION */
if (wms_exception_format == NULL) {
if (nVersion <= OWS_1_0_0)
wms_exception_format = "INIMAGE"; /* WMS 1.0.0 */
else if (nVersion <= OWS_1_0_7)
wms_exception_format = "SE_XML"; /* WMS 1.0.1 to 1.0.7 */
else if (nVersion <= OWS_1_1_1)
wms_exception_format = "application/vnd.ogc.se_xml"; /* WMS 1.1.0 and later */
else
wms_exception_format = "text/xml";
}
if (strcasecmp(wms_exception_format, "INIMAGE") == 0 ||
strcasecmp(wms_exception_format, "BLANK") == 0 ||
strcasecmp(wms_exception_format, "application/vnd.ogc.se_inimage")== 0 ||
strcasecmp(wms_exception_format, "application/vnd.ogc.se_blank") == 0) {
int blank = 0;
if (strcasecmp(wms_exception_format, "BLANK") == 0 ||
strcasecmp(wms_exception_format, "application/vnd.ogc.se_blank") == 0) {
blank = 1;
}
msWriteErrorImage(map, NULL, blank);
} else if (strcasecmp(wms_exception_format, "WMS_XML") == 0) { /* Only in V1.0.0 */
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<WMTException version=\"1.0.0\">\n");
msWriteErrorXML(stdout);
msIO_printf("</WMTException>\n");
} else /* XML error, the default: SE_XML (1.0.1 to 1.0.7) */
/* or application/vnd.ogc.se_xml (1.1.0 and later) */
{
if (nVersion <= OWS_1_0_7) {
/* In V1.0.1 to 1.0.7, the MIME type was text/xml */
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
msIO_printf("<!DOCTYPE ServiceExceptionReport SYSTEM \"http://www.digitalearth.gov/wmt/xml/exception_1_0_1.dtd\">\n");
msIO_printf("<ServiceExceptionReport version=\"1.0.1\">\n");
} else if (nVersion <= OWS_1_1_0) {
/* In V1.1.0 and later, we have OGC-specific MIME types */
/* we cannot return anything else than application/vnd.ogc.se_xml here. */
msIO_setHeader("Content-Type","application/vnd.ogc.se_xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
msIO_printf("<!DOCTYPE ServiceExceptionReport SYSTEM \"%s/wms/1.1.0/exception_1_1_0.dtd\">\n",schemalocation);
msIO_printf("<ServiceExceptionReport version=\"1.1.0\">\n");
} else if (nVersion <= OWS_1_1_1) { /* 1.1.1 */
msIO_setHeader("Content-Type","application/vnd.ogc.se_xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
msIO_printf("<!DOCTYPE ServiceExceptionReport SYSTEM \"%s/wms/1.1.1/exception_1_1_1.dtd\">\n", schemalocation);
msIO_printf("<ServiceExceptionReport version=\"1.1.1\">\n");
} else { /*1.3.0*/
if (strcasecmp(wms_exception_format, "application/vnd.ogc.se_xml") == 0) {
msIO_setHeader("Content-Type","application/vnd.ogc.se_xml; charset=UTF-8");
} else {
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
}
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" standalone=\"no\" ?>\n");
msIO_printf("<ServiceExceptionReport version=\"1.3.0\" xmlns=\"http://www.opengis.net/ogc\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.opengis.net/ogc %s/wms/1.3.0/exceptions_1_3_0.xsd\">\n",
schemalocation);
}
if (exception_code)
msIO_printf("<ServiceException code=\"%s\">\n", exception_code);
else
msIO_printf("<ServiceException>\n");
msWriteErrorXML(stdout);
msIO_printf("</ServiceException>\n");
msIO_printf("</ServiceExceptionReport>\n");
}
free(schemalocation);
return MS_FAILURE; /* so that we can call 'return msWMSException();' anywhere */
}
static bool msWMSSetTimePattern(const char *timepatternstring, const char *timestring, bool checkonly)
{
if (timepatternstring && timestring) {
/* parse the time parameter to extract a distinct time. */
/* time value can be dicrete times (eg 2004-09-21), */
/* multiple times (2004-09-21, 2004-09-22, ...) */
/* and range(s) (2004-09-21/2004-09-25, 2004-09-27/2004-09-29) */
const auto atimes = msStringSplit(timestring, ',');
/* get the pattern to use */
if (!atimes.empty()) {
auto patterns = msStringSplit(timepatternstring, ',');
for (auto& pattern: patterns) {
msStringTrimBlanks(pattern);
msStringTrimLeft(pattern);
}
for (const auto& atime: atimes) {
const auto ranges = msStringSplit(atime.c_str(), '/');
for( const auto& range: ranges) {
bool match = false;
for (const auto& pattern: patterns) {
if (!pattern.empty()) {
if (msTimeMatchPattern(range.c_str(), pattern.c_str()) == MS_TRUE) {
if (!checkonly) msSetLimitedPatternsToUse(pattern.c_str());
match = true;
break;
}
}
}
if (!match) {
msSetError(MS_WMSERR, "Time value %s given does not match the time format pattern.", "msWMSSetTimePattern", range.c_str());
return false;
}
}
}
}
}
return true;
}
/*
** Apply the TIME parameter to layers that are time aware
*/
static
int msWMSApplyTime(mapObj *map, int version, const char *time, const char *wms_exception_format)
{
if (map) {
const char* timepattern = msOWSLookupMetadata(&(map->web.metadata), "MO", "timeformat");
for (int i=0; i<map->numlayers; i++) {
layerObj* lp = (GET_LAYER(map, i));
if (lp->status != MS_ON && lp->status != MS_DEFAULT)
continue;
/* check if the layer is time aware */
const char* timeextent = msOWSLookupMetadata(&(lp->metadata), "MO", "timeextent");
const char* timefield = msOWSLookupMetadata(&(lp->metadata), "MO", "timeitem");
const char* timedefault = msOWSLookupMetadata(&(lp->metadata), "MO", "timedefault");
if (timeextent && timefield) {
/* check to see if the time value is given. If not */
/* use default time. If default time is not available */
/* send an exception */
if (time == NULL || strlen(time) <=0) {
if (timedefault == NULL) {
msSetError(MS_WMSERR, "No Time value was given, and no default time value defined.", "msWMSApplyTime");
return msWMSException(map, version, "MissingDimensionValue", wms_exception_format);
} else {
if (msValidateTimeValue((char *)timedefault, timeextent) == MS_FALSE) {
msSetError(MS_WMSERR, "No Time value was given, and the default time value %s is invalid or outside the time extent defined %s", "msWMSApplyTime", timedefault, timeextent);
/* return MS_FALSE; */
return msWMSException(map, version, "InvalidDimensionValue", wms_exception_format);
}
msLayerSetTimeFilter(lp, timedefault, timefield);
}
} else {
/*
** Check to see if there is a list of possible patterns defined. If it is the case, use
** it to set the time pattern to use for the request.
**
** Last argument is set to TRUE (checkonly) to not trigger the patterns info setting, rather
** to only apply the wms_timeformats on the user request values, not the mapfile values.
*/
if (timepattern && time && strlen(time) > 0) {
if (!msWMSSetTimePattern(timepattern, time, true))
return msWMSException(map, version,"InvalidDimensionValue", wms_exception_format);
}
/* check if given time is in the range */
if (msValidateTimeValue(time, timeextent) == MS_FALSE) {
if (timedefault == NULL) {
msSetError(MS_WMSERR, "Time value(s) %s given is invalid or outside the time extent defined (%s).", "msWMSApplyTime", time, timeextent);
/* return MS_FALSE; */
return msWMSException(map, version,
"InvalidDimensionValue", wms_exception_format);
} else {
if (msValidateTimeValue((char *)timedefault, timeextent) == MS_FALSE) {
msSetError(MS_WMSERR, "Time value(s) %s given is invalid or outside the time extent defined (%s), and default time set is invalid (%s)", "msWMSApplyTime", time, timeextent, timedefault);
/* return MS_FALSE; */
return msWMSException(map, version,
"InvalidDimensionValue", wms_exception_format);
} else
msLayerSetTimeFilter(lp, timedefault, timefield);
}
} else {
/* build the time string */
msLayerSetTimeFilter(lp, time, timefield);
timeextent= NULL;
}
}
}
}
/* last argument is MS_FALSE to trigger a method call that set the patterns
info. some drivers use it */
if (timepattern && time && strlen(time) > 0) {
if (!msWMSSetTimePattern(timepattern, time, false))
return msWMSException(map, version, "InvalidDimensionValue", wms_exception_format);
}
}
return MS_SUCCESS;
}
/*
** Apply the FILTER parameter to layers (RFC118)
*/
static
int msWMSApplyFilter(mapObj *map, int version, const char *filter,
int def_srs_needs_axis_swap, const char *wms_exception_format, owsRequestObj *ows_request)
{
// Empty filter should be ignored
if (!filter || strlen(filter) == 0)
return MS_SUCCESS;
if (!map)
return MS_FAILURE;
/* Count number of requested layers / groups / etc.
* Only layers with STATUS ON were in the LAYERS request param.
* Layers with STATUS DEFAULT were set in the mapfile and are
* not expected to have a corresponding filter in the request
*/
int numlayers = 0;
for(int i=0; i<map->numlayers; i++) {
layerObj *lp=NULL;
if(map->layerorder[i] != -1) {
lp = (GET_LAYER(map, map->layerorder[i]));
if (lp->status == MS_ON)
numlayers++;
}
}
/* -------------------------------------------------------------------- */
/* Parse the Filter parameter. If there are several Filter */
/* parameters, each Filter is inside parentheses. */
/* -------------------------------------------------------------------- */
int numfilters = 0;
char **paszFilters = NULL;
if (filter[0] == '(') {
paszFilters = FLTSplitFilters(filter, &numfilters);
} else if (numlayers == 1) {
numfilters=1;
paszFilters = (char **)msSmallMalloc(sizeof(char *)*numfilters);
paszFilters[0] = msStrdup(filter);
}
if (numfilters != ows_request->numwmslayerargs) {
msSetError(MS_WMSERR, "Wrong number of filter elements, one filter must be specified for each requested layer or groups.",
"msWMSApplyFilter" );
msFreeCharArray(paszFilters, numfilters);
return msWMSException(map, version, "InvalidParameterValue", wms_exception_format);
}
/* We're good to go. Apply each filter to the corresponding layer */
for(int i=0; i<map->numlayers; i++) {
layerObj *lp=NULL;
if(map->layerorder[i] != -1)
lp = (GET_LAYER(map, map->layerorder[i]));
/* Only layers with STATUS ON were in the LAYERS request param.*/
if (lp == NULL || lp->status != MS_ON)
continue;
const int curfilter = ows_request->layerwmsfilterindex[lp->index];
/* Skip empty filters */
assert(paszFilters);
assert(curfilter >= 0 && curfilter < numfilters);
if (paszFilters[curfilter][0] == '\0') {
continue;
}
/* Force setting a template to enable query. */
if (lp->_template == NULL)
lp->_template = msStrdup("ttt.html");
/* Parse filter */
FilterEncodingNode* psNode = FLTParseFilterEncoding(paszFilters[curfilter]);
if (!psNode) {
msSetError(MS_WMSERR,
"Invalid or Unsupported FILTER : %s",
"msWMSApplyFilter()", paszFilters[curfilter]);
msFreeCharArray(paszFilters, numfilters);
return msWMSException(map, version, "InvalidParameterValue", wms_exception_format);
}
/* For WMS 1.3 and up, we may need to swap the axis of bbox and geometry
* elements inside the filter(s)
*/
if (version >= OWS_1_3_0)
FLTDoAxisSwappingIfNecessary(map, psNode, def_srs_needs_axis_swap);
#ifdef do_we_need_this
FLTProcessPropertyIsNull(psNode, map, lp->index);
/*preparse the filter for gml aliases*/
FLTPreParseFilterForAliasAndGroup(psNode, map, lp->index, "G");
/* Check that FeatureId filters are consistent with the active layer */
if( FLTCheckFeatureIdFilters(psNode, map, lp->index) == MS_FAILURE)
{
FLTFreeFilterEncodingNode( psNode );
return msWFSException(map, "mapserv", MS_OWS_ERROR_NO_APPLICABLE_CODE, paramsObj->pszVersion);
}
/* FIXME?: could probably apply to WFS 1.1 too */
if( nWFSVersion >= OWS_2_0_0 )
{
int nEvaluation;
if( FLTCheckInvalidOperand(psNode) == MS_FAILURE)
{
FLTFreeFilterEncodingNode( psNode );
msFreeCharArray(paszFilters, numfilters);
return msWFSException(map, "filter", MS_WFS_ERROR_OPERATION_PROCESSING_FAILED, paramsObj->pszVersion);
}
if( FLTCheckInvalidProperty(psNode, map, lp->index) == MS_FAILURE)
{
FLTFreeFilterEncodingNode( psNode );
msFreeCharArray(paszFilters, numfilters);
return msWFSException(map, "filter", MS_OWS_ERROR_INVALID_PARAMETER_VALUE, paramsObj->pszVersion);
}
psNode = FLTSimplify(psNode, &nEvaluation);
if( psNode == NULL )
{
FLTFreeFilterEncodingNode( psNode );
msFreeCharArray(paszFilters, numfilters);
if( nEvaluation == 1 ) {
/* return full layer */
return msWFSRunBasicGetFeature(map, lp, paramsObj, nWFSVersion);
}
else {
/* return empty result set */
return MS_SUCCESS;
}
}
}
#endif
/* Apply filter to this layer */
/* But first, start by removing any wfs_use_default_extent_for_getfeature metadata item */
/* that could result in the BBOX to be removed */
std::string old_value_wfs_use_default_extent_for_getfeature;
{
const char* old_value_tmp = msLookupHashTable(
&(lp->metadata), "wfs_use_default_extent_for_getfeature");
if( old_value_tmp )
{
old_value_wfs_use_default_extent_for_getfeature = old_value_tmp;
msRemoveHashTable(&(lp->metadata), "wfs_use_default_extent_for_getfeature");
}
}
msInsertHashTable(&(lp->metadata), "gml_wmsfilter_flag", "true");
int ret = FLTApplyFilterToLayer(psNode, map, lp->index);
msRemoveHashTable(&(lp->metadata), "gml_wmsfilter_flag");
if( !old_value_wfs_use_default_extent_for_getfeature.empty() )
{
msInsertHashTable(&(lp->metadata), "wfs_use_default_extent_for_getfeature",
old_value_wfs_use_default_extent_for_getfeature.c_str());
}
if( ret != MS_SUCCESS ) {
errorObj* ms_error = msGetErrorObj();
if(ms_error->code != MS_NOTFOUND) {
msSetError(MS_WMSERR, "FLTApplyFilterToLayer() failed", "msWMSApplyFilter()");
FLTFreeFilterEncodingNode( psNode );
msFreeCharArray(paszFilters, numfilters);
return msWMSException(map, version, "InvalidParameterValue", wms_exception_format);
}
}
FLTFreeFilterEncodingNode( psNode );
}/* for */
msFreeCharArray(paszFilters, numfilters);
return MS_SUCCESS;
}
/*
** msWMSPrepareNestedGroups()
**
** purpose: Parse WMS_LAYER_GROUP settings into arrays
**
** params:
** - nestedGroups: This array holds the arrays of groups that have been set
** through the WMS_LAYER_GROUP metadata
** - numNestedGroups: This array holds the number of groups set in
** WMS_LAYER_GROUP for each layer
** - isUsedInNestedGroup: This array indicates if the layer is used as group
** as set through the WMS_LAYER_GROUP metadata
*/
static
void msWMSPrepareNestedGroups(mapObj* map, int /* nVersion */, char*** nestedGroups, int* numNestedGroups, int* isUsedInNestedGroup)
{
//Create set to hold unique groups
std::set<std::string> uniqgroups;
for (int i = 0; i < map->numlayers; i++) {
nestedGroups[i] = NULL; /* default */
numNestedGroups[i] = 0; /* default */
isUsedInNestedGroup[i] = 0; /* default */
const char* groups = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "MO", "layer_group");
if ((groups != NULL) && (strlen(groups) != 0)) {
if (GET_LAYER(map, i)->group != NULL && strlen(GET_LAYER(map, i)->group) != 0) {
const char* errorMsg = "It is not allowed to set both the GROUP and WMS_LAYER_GROUP for a layer";
msSetError(MS_WMSERR, errorMsg, "msWMSPrepareNestedGroups()", NULL);
msIO_fprintf(stdout, "<!-- ERROR: %s -->\n", errorMsg);
/* cannot return exception at this point because we are already writing to stdout */
} else {
if (groups[0] != '/') {
const char* errorMsg = "The WMS_LAYER_GROUP metadata does not start with a '/'";
msSetError(MS_WMSERR, errorMsg, "msWMSPrepareNestedGroups()", NULL);
msIO_fprintf(stdout, "<!-- ERROR: %s -->\n", errorMsg);
/* cannot return exception at this point because we are already writing to stdout */
} else {
/* split into subgroups. Start at address + 1 because the first '/' would cause an extra empty group */
nestedGroups[i] = msStringSplit(groups + 1, '/', &numNestedGroups[i]);
/* Iterate through the groups and add them to the unique groups array */
for (int k=0; k<numNestedGroups[i]; k++) {
uniqgroups.insert(msStringToLower(std::string(nestedGroups[i][k])));
}
}
}
}
}
/* Iterate through layers to find out whether they are in any of the nested groups */
for (int i = 0; i < map->numlayers; i++) {
if( uniqgroups.find(msStringToLower(std::string(GET_LAYER(map, i)->name))) != uniqgroups.end() ) {
isUsedInNestedGroup[i] = 1;
}
}
}
/*
** Validate that a given dimension is inside the extents defined
*/
static
bool msWMSValidateDimensionValue(const char *value, const char *dimensionextent, bool forcecharacter)
{
std::vector<pointObj> aextentranges;
bool isextentavalue = false;
bool isextentarange = false;
bool ischaracter = false;
if (forcecharacter)
ischaracter = true;
if (!value || !dimensionextent)
return false;
/*for the value, we support descrete values (2005) */
/* multiple values (abc, def, ...) */
/* and range(s) (1000/2000, 3000/5000) */
/** we do not support resolution*/
/* -------------------------------------------------------------------- */
/* parse the extent first. */
/* -------------------------------------------------------------------- */
auto extents = msStringSplit (dimensionextent, ',');
for( auto& extent: extents ) // Make sure to get by reference so that it is updated in place
msStringTrim(extent);
std::vector<std::string> aextentvalues;
if (extents.size() == 1) {
if (strstr(dimensionextent, "/") == NULL) {
/*single value*/
isextentavalue = true;
aextentvalues.push_back(dimensionextent);
if (!forcecharacter)
ischaracter= FLTIsNumeric(dimensionextent) == MS_FALSE;
} else {
const auto ranges = msStringSplit (dimensionextent, '/');
if(ranges.size() == 2 || ranges.size() == 3) {
/*single range*/
isextentarange = true;
aextentranges.resize(1);
aextentranges[0].x = atof(ranges[0].c_str());
aextentranges[0].y = atof(ranges[1].c_str());
/*ranges should be numeric*/
ischaracter = false;
}
}
} else if (extents.size() > 1) { /*check if it is muliple values or mutliple ranges*/
if (strstr(dimensionextent, "/") == NULL) {
/*multiple values*/
isextentavalue = true;
aextentvalues = extents;
if (!forcecharacter)
ischaracter= FLTIsNumeric(aextentvalues[0].c_str()) == MS_FALSE;
} else { /*multiple range extent*/
int isvalidextent = MS_TRUE;
/*ranges should be numeric*/
ischaracter = false;
isextentarange = true;
aextentranges.resize(extents.size());
size_t nextentranges=0;
for(const auto& extent: extents) {
const auto onerange = msStringSplit(extent.c_str(), '/');
if (onerange.size() != 2 && onerange.size() != 3) {
isvalidextent = MS_FALSE;
break;
}
if (isvalidextent) {
aextentranges[nextentranges].x = atof(onerange[0].c_str());
aextentranges[nextentranges++].y = atof(onerange[1].c_str());
}
}
if (!isvalidextent) {
nextentranges = 0;
isextentarange = false;
}
aextentranges.resize(nextentranges);
}
}
/* make sure that we got a valid extent*/
if (!isextentavalue && !isextentarange) {
return false;
}
/*for the extent of the dimesion, we support
single value, or list of mulitiple values comma separated,
a single range or multipe ranges */
const auto uservalues = msStringSplit (value, ',');
bool uservaluevalid = false;
if (uservalues.size() == 1) {
/*user iput=single*/
/*is it descret or range*/
const auto ranges = msStringSplit(uservalues[0].c_str(), '/');
if (ranges.size() == 1) { /*discrete*/
if (isextentavalue) {
/*single user value, single/multiple values extent*/
for (const auto& extentvalue: aextentvalues) {
if (ischaracter)
uservaluevalid = (uservalues[0] == extentvalue);
else {
if (atof(uservalues[0].c_str()) == atof(extentvalue.c_str()))
uservaluevalid = true;
}
if(uservaluevalid)
break;
}
} else if (isextentarange) {
/*single user value, single/multiple range extent*/
const float currentval = atof(uservalues[0].c_str());
for (const auto& extentrange: aextentranges) {
const float minval = extentrange.x;
const float maxval = extentrange.y;
if (currentval >= minval && currentval <= maxval) {
uservaluevalid= true;
break;
}
}
}
} else if (ranges.size() == 2 || ranges.size() == 3) { /*range*/
/*user input=single range. In this case the extents must
be of a range type.*/
const float mincurrentval = atof(ranges[0].c_str());
const float maxcurrentval = atof(ranges[1].c_str());
if (isextentarange) {
for (const auto& extentrange: aextentranges) {
const float minval = extentrange.x;
const float maxval = extentrange.y;
if(minval <= mincurrentval && maxval >= maxcurrentval &&
minval <= maxval) {
uservaluevalid= true;
break;
}
}
}
}
} else if (uservalues.size() > 1) { /*user input=multiple*/
if (strstr(value, "/") == NULL) {
/*user input=multiple value*/
bool valueisvalid = false;
for (const auto& uservalue: uservalues) {
valueisvalid = false;
if (isextentavalue) {
/*user input is multiple values, extent is defned as one or multiple values*/
for (const auto& extentvalue: aextentvalues) {
if (ischaracter) {
if (uservalue == extentvalue) {
valueisvalid = true;
break;
}
} else {
if (atof(uservalue.c_str()) == atof(extentvalue.c_str())) {
valueisvalid = true;
break;
}
}
}
/*every value shoule be valid*/
if (!valueisvalid)
break;
} else if (isextentarange) {
/*user input is multiple values, extent is defned as one or multiple ranges*/
for (const auto& extentrange: aextentranges) {
const float minval = extentrange.x;
const float maxval = extentrange.y;
const float currentval = atof(uservalue.c_str());
if(minval <= currentval && maxval >= currentval &&
minval <= maxval) {
valueisvalid = true;
break;
}
}
if (!valueisvalid)
break;
}
}
uservaluevalid = valueisvalid;
} else { /*user input multiple ranges*/
bool valueisvalid = true;
for (const auto& uservalue: uservalues) {
/*each ranges should be valid*/
const auto onerange = msStringSplit(uservalue.c_str(), '/');
if (onerange.size() == 2 || onerange.size() == 3) {
const float mincurrentval = atof(onerange[0].c_str());
const float maxcurrentval = atof(onerange[1].c_str());
/*extent must be defined also as a rangle*/
if (isextentarange) {
bool found = false;
for (const auto& extentrange: aextentranges) {
const float mincurrentrange = extentrange.x;
const float maxcurrentrange = extentrange.y;
if(mincurrentval >=mincurrentrange && maxcurrentval <= maxcurrentrange &&
mincurrentval <= maxcurrentval) {
found = true;
break;
}
}
if (!found) {
valueisvalid = false;
break;
}
}
} else {
valueisvalid = false;
}
}
uservaluevalid = valueisvalid;
}
}
return uservaluevalid;
}
static
bool msWMSApplyDimensionLayer(layerObj *lp, const char *item, const char *value, bool forcecharacter)
{
bool result = false;
if (lp && item && value) {
/*for the value, we support descrete values (2005) */
/* multiple values (abc, def, ...) */
/* and range(s) (1000/2000, 3000/5000) */
char* pszExpression = FLTGetExpressionForValuesRanges(lp, item, value, forcecharacter ? MS_TRUE : MS_FALSE);
if (pszExpression) {
// If tileindex is set, the filter is applied to tileindex too.
int tlpindex = -1;
if (lp->tileindex && (tlpindex = msGetLayerIndex(lp->map, lp->tileindex)) != -1) {
result = FLTApplyExpressionToLayer((GET_LAYER(lp->map, tlpindex)), pszExpression) != MS_FALSE;
} else {
result = true;
}
result &= FLTApplyExpressionToLayer(lp, pszExpression) != MS_FALSE;
msFree(pszExpression);
}
}
return result;
}
static
bool msWMSApplyDimension(layerObj *lp, int /* version */, const char *dimensionname, const char *value,
const char * /* wms_exception_format */)
{
bool forcecharacter = false;
bool result = false;
if (lp && dimensionname && value) {
/*check if the dimension name passes starts with dim_. All dimensions should start with dim_, except elevation*/
std::string dimension;
if (strncasecmp(dimensionname, "dim_", 4) == 0)
dimension = dimensionname+4;
else
dimension = dimensionname;
/*if value is empty and a default is defined, use it*/
std::string currentvalue;
if (strlen(value) > 0)
currentvalue = value;
else {
const char* dimensiondefault = msOWSLookupMetadata(&(lp->metadata), "M", (dimension + "_default").c_str());
if (dimensiondefault)
currentvalue = dimensiondefault;
}
/*check if the manadatory metada related to the dimension are set*/
const char* dimensionitem = msOWSLookupMetadata(&(lp->metadata), "M", (dimension + "_item").c_str());
const char* dimensionextent = msOWSLookupMetadata(&(lp->metadata), "M", (dimension + "_extent").c_str());
const char* dimensionunit = msOWSLookupMetadata(&(lp->metadata), "M", (dimension + "_units").c_str());
/*if the server want to force the type to character*/
const char* dimensiontype = msOWSLookupMetadata(&(lp->metadata), "M", (dimension + "_type").c_str());
if (dimensiontype && strcasecmp(dimensiontype, "Character") == 0)
forcecharacter = true;
if (dimensionitem && dimensionextent && dimensionunit && !currentvalue.empty()) {
if(msWMSValidateDimensionValue(currentvalue.c_str(), dimensionextent, forcecharacter)) {
result = msWMSApplyDimensionLayer(lp, dimensionitem, currentvalue.c_str(), forcecharacter);
} else {
msSetError(MS_WMSERR, "Dimension %s with a value of %s is invalid or outside the extents defined", "msWMSApplyDimension",
dimension.c_str(), currentvalue.c_str());
result = false;
}
} else
msSetError(MS_WMSERR, "Dimension %s : invalid settings. Make sure that item, units and extent are set.", "msWMSApplyDimension",
dimension.c_str());
}
return result;
}
/*
**
*/
int msWMSLoadGetMapParams(mapObj *map, int nVersion,
char **names, char **values, int numentries, const char *wms_exception_format,
const char * /*wms_request*/, owsRequestObj *ows_request)
{
bool adjust_extent = false;
bool nonsquare_enabled = false;
int transparent = MS_NOOVERRIDE;
bool bbox_pixel_is_point = false;
outputFormatObj *format = NULL;
int validlayers = 0;
const char *styles = NULL;
int invalidlayers = 0;
std::string epsgbuf;
std::string srsbuffer;
bool epsgvalid = false;
bool timerequest = false;
const char *stime = NULL;
bool srsfound = false;
bool bboxfound = false;
bool formatfound = false;
bool widthfound = false;
bool heightfound = false;
const char *request = NULL;
int status = 0;
const char *layerlimit = NULL;
bool tiled = false;
const char *sldenabled=NULL;
const char *sld_url=NULL;
const char *sld_body=NULL;
const char *filter = NULL;
/* Some of the getMap parameters are actually required depending on the */
/* request, but for now we assume all are optional and the map file */
/* defaults will apply. */
msAdjustExtent(&(map->extent), map->width, map->height);
/*
Check for SLDs first. If SLD is available LAYERS and STYLES parameters are non mandatory
*/
for(int i=0; i<numentries; i++) {
/* check if SLD is passed. If yes, check for OGR support */
if (strcasecmp(names[i], "SLD") == 0 || strcasecmp(names[i], "SLD_BODY") == 0) {
sldenabled = msOWSLookupMetadata(&(map->web.metadata), "MO", "sld_enabled");
if (sldenabled == NULL) {
sldenabled = "true";
}
if (strcasecmp(sldenabled, "true") == 0) {
if (strcasecmp(names[i], "SLD") == 0) {
sld_url = values[i];
}
if (strcasecmp(names[i], "SLD_BODY") == 0) {
sld_body = values[i];
}
}
}
}
std::vector<std::string> wmslayers;
for(int i=0; i<numentries; i++) {
/* getMap parameters */
if (strcasecmp(names[i], "REQUEST") == 0) {
request = values[i];
}
if (strcasecmp(names[i], "LAYERS") == 0) {
std::vector<int> layerOrder(map->numlayers);
wmslayers = msStringSplit(values[i], ',');
if (wmslayers.empty()) {
if (sld_url == NULL && sld_body == NULL) {
msSetError(MS_WMSERR, "At least one layer name required in LAYERS.",
"msWMSLoadGetMapParams()");
return msWMSException(map, nVersion, NULL, wms_exception_format);
}
}
if (nVersion >= OWS_1_3_0) {
layerlimit = msOWSLookupMetadata(&(map->web.metadata), "MO", "layerlimit");
if(layerlimit) {
if (static_cast<int>(wmslayers.size()) > atoi(layerlimit)) {
msSetError(MS_WMSERR, "Number of layers requested exceeds LayerLimit.",
"msWMSLoadGetMapParams()");
return msWMSException(map, nVersion, NULL, wms_exception_format);
}
}
}
for (int iLayer=0; iLayer < map->numlayers; iLayer++) {
map->layerorder[iLayer] = iLayer;
}
int nLayerOrder = 0;
for(int j=0; j<map->numlayers; j++) {
/* Keep only layers with status=DEFAULT by default */
/* Layer with status DEFAULT is drawn first. */
if (GET_LAYER(map, j)->status != MS_DEFAULT)
GET_LAYER(map, j)->status = MS_OFF;
else {
map->layerorder[nLayerOrder++] = j;
layerOrder[j] = 1;
}
}
char*** nestedGroups = (char***)msSmallCalloc(map->numlayers, sizeof(char**));
int* numNestedGroups = (int*)msSmallCalloc(map->numlayers, sizeof(int));
int* isUsedInNestedGroup = (int*)msSmallCalloc(map->numlayers, sizeof(int));
msWMSPrepareNestedGroups(map, nVersion, nestedGroups, numNestedGroups, isUsedInNestedGroup);
if (ows_request->layerwmsfilterindex != NULL)
msFree(ows_request->layerwmsfilterindex);
ows_request->layerwmsfilterindex = (int*)msSmallMalloc(map->numlayers * sizeof(int));
for(int j=0; j<map->numlayers; j++) {
ows_request->layerwmsfilterindex[j] = -1;
}
ows_request->numwmslayerargs = static_cast<int>(wmslayers.size());
for (int k = 0; k < static_cast<int>(wmslayers.size()); k++) {
const auto& wmslayer = wmslayers[k];
bool layerfound = false;
for (int j=0; j<map->numlayers; j++) {
/* Turn on selected layers only. */
if ( ((GET_LAYER(map, j)->name &&
strcasecmp(GET_LAYER(map, j)->name, wmslayer.c_str()) == 0) ||
(map->name && strcasecmp(map->name, wmslayer.c_str()) == 0) ||
(GET_LAYER(map, j)->group && strcasecmp(GET_LAYER(map, j)->group, wmslayer.c_str()) == 0) ||
((numNestedGroups[j] >0) && msStringInArray(wmslayer.c_str(), nestedGroups[j], numNestedGroups[j]))) &&
((msIntegerInArray(GET_LAYER(map, j)->index, ows_request->enabled_layers, ows_request->numlayers))) ) {
if (GET_LAYER(map, j)->status != MS_DEFAULT) {
if (layerOrder[j] == 0) {
map->layerorder[nLayerOrder++] = j;
layerOrder[j] = 1;
GET_LAYER(map, j)->status = MS_ON;
}
}
/* if a layer name is repeated assign the first matching filter */
/* duplicate names will be assigned filters later when layer copies are created */
if (ows_request->layerwmsfilterindex[j] == -1) {
ows_request->layerwmsfilterindex[j] = k;
}
validlayers++;