forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmapwfs.cpp
5573 lines (4811 loc) · 201 KB
/
mapwfs.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: WFS server implementation
* Author: Daniel Morissette, DM Solutions Group (morissette@dmsolutions.ca)
*
**********************************************************************
* Copyright (c) 2002, Daniel Morissette, DM Solutions Group Inc
* Copyright (c) 2013, Even Rouault
*
* 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
****************************************************************************/
#include "mapserver.h"
#include "mapows.h"
#include <string>
#if defined(USE_WFS_SVR)
/* There is a dependency to GDAL/OGR for the GML driver and MiniXML parser */
#include "cpl_minixml.h"
#include "cpl_conv.h"
#include "cpl_string.h"
#include "mapogcfilter.h"
#include "mapowscommon.h"
#include "maptemplate.h"
#if defined(USE_LIBXML2)
#include "maplibxml2.h"
#endif
#include <string>
static int msWFSAnalyzeStoredQuery(mapObj* map,
wfsParamsObj *wfsparams,
const char* id,
const char* pszResolvedQuery);
static void msWFSSimplifyPropertyNameAndFilter(wfsParamsObj *wfsparams);
static void msWFSAnalyzeStartIndexAndFeatureCount(mapObj *map, const wfsParamsObj *paramsObj,
int bIsHits,
int *pmaxfeatures, int* pstartindex);
static int msWFSRunBasicGetFeature(mapObj* map,
layerObj* lp,
const wfsParamsObj *paramsObj,
int nWFSVersion);
static int msWFSParseRequest(mapObj *map, cgiRequestObj *request,
wfsParamsObj *wfsparams, int force_wfs_mode);
/* Must be sorted from more recent to older one */
static const int wfsSupportedVersions[] = {OWS_2_0_0, OWS_1_1_0, OWS_1_0_0};
static const char* const wfsSupportedVersionsStr[] = { "2.0.0", "1.1.0", "1.0.0" };
static const int wfsNumSupportedVersions =
(int)(sizeof(wfsSupportedVersions)/sizeof(wfsSupportedVersions[0]));
static const char* const wfsUnsupportedOperations[] = { "GetFeatureWithLock",
"LockFeature",
"Transaction",
"CreateStoredQuery",
"DropStoredQuery" };
static const int wfsNumUnsupportedOperations =
(int)(sizeof(wfsUnsupportedOperations)/sizeof(wfsUnsupportedOperations[0]));
#define WFS_LATEST_VERSION wfsSupportedVersionsStr[0]
/* Supported DescribeFeature formats */
typedef enum
{
OWS_DEFAULT_SCHEMA, /* basically a GML 2.1 schema */
OWS_SFE_SCHEMA, /* GML for simple feature exchange (formerly GML3L0) */
OWS_GML32_SFE_SCHEMA /* GML 3.2 Simple Features Level 0 */
} WFSSchemaVersion;
/*
** msWFSGetIndexUnsupportedOperation()
**
** Return index of pszOp in wfsUnsupportedOperations, or -1 otherwise
*/
static int msWFSGetIndexUnsupportedOperation(const char* pszOp)
{
int i;
for(i = 0; i < wfsNumUnsupportedOperations; i++ )
{
if( strcasecmp(wfsUnsupportedOperations[i], pszOp) == 0 )
return i;
}
return -1;
}
static
const char *msWFSGetDefaultVersion(mapObj *map);
/*
** msWFSException()
**
** Report current MapServer error in XML exception format.
*/
static
int msWFSExceptionInternal(mapObj *map, const char *locator, const char *code,
const char *version, int locatorShouldBeNull )
{
char *schemalocation = NULL;
/* In WFS, exceptions are always XML.
*/
if( version == NULL )
version = msWFSGetDefaultVersion(map);
if( msOWSParseVersionString(version) >= OWS_2_0_0 )
return msWFSException20( map, (locatorShouldBeNull) ? NULL : locator, code );
if( msOWSParseVersionString(version) >= OWS_1_1_0 )
return msWFSException11( map, (locatorShouldBeNull) ? NULL : locator, code, version );
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" ?>\n");
msIO_printf("<ServiceExceptionReport ");
msIO_printf("version=\"1.2.0\" ");
msIO_printf("xmlns=\"http://www.opengis.net/ogc\" ");
msIO_printf("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" ");
schemalocation = msEncodeHTMLEntities(msOWSGetSchemasLocation(map));
msIO_printf("xsi:schemaLocation=\"http://www.opengis.net/ogc %s/wfs/1.0.0/OGC-exception.xsd\">\n", schemalocation);
free(schemalocation);
msIO_printf(" <ServiceException code=\"%s\" locator=\"%s\">\n", code, locator);
/* Optional <Locator> element currently unused. */
/* msIO_printf(" <Message>\n"); */
msWriteErrorXML(stdout);
/* msIO_printf(" </Message>\n"); */
msIO_printf(" </ServiceException>\n");
msIO_printf("</ServiceExceptionReport>\n");
return MS_FAILURE; /* so we can call 'return msWFSException();' anywhere */
}
int msWFSException(mapObj *map, const char *locator, const char *code,
const char *version )
{
return msWFSExceptionInternal(map, locator, code, version, FALSE);
}
/*
** msWFSExceptionNoLocator()
**
** Report current MapServer error in XML exception format.
** For WFS >= 1.1.0, the locator will be ignored. It will be just used
** for legacy WFS 1.0 exceptions.
*/
static
int msWFSExceptionNoLocator(mapObj *map, const char *locator, const char *code,
const char *version )
{
return msWFSExceptionInternal(map, locator, code, version, TRUE);
}
/*
** Helper function to build a list of output formats.
**
** Given a layer it will return all formats valid for that layer, otherwise
** all formats permitted on layers in the map are returned. The string
** returned should be freed by the caller.
*/
char *msWFSGetOutputFormatList(mapObj *map, layerObj *layer, int nWFSVersion )
{
int i, got_map_list = 0;
static const int out_list_size = 20000;
char *out_list = (char*) msSmallCalloc(1,out_list_size);
if( nWFSVersion == OWS_1_0_0 )
strcpy(out_list,"GML2");
else if( nWFSVersion == OWS_1_1_0 )
strcpy(out_list,"text/xml; subtype=gml/3.1.1");
else
strcpy(out_list,"application/gml+xml; version=3.2,"
"text/xml; subtype=gml/3.2.1,"
"text/xml; subtype=gml/3.1.1,"
"text/xml; subtype=gml/2.1.2");
for( i = 0; i < map->numlayers; i++ ) {
const char *format_list;
layerObj *lp;
int j, n;
char **tokens;
lp = GET_LAYER(map, i);
if( layer != NULL && layer != lp )
continue;
format_list = msOWSLookupMetadata(&(lp->metadata),
"F","getfeature_formatlist");
if( format_list == NULL && !got_map_list ) {
format_list = msOWSLookupMetadata(&(map->web.metadata),
"F","getfeature_formatlist");
got_map_list = 1;
}
if( format_list == NULL )
continue;
n = 0;
tokens = msStringSplit(format_list, ',', &n);
for( j = 0; j < n; j++ ) {
int iformat;
const char *fname, *hit;
outputFormatObj *format_obj;
msStringTrim( tokens[j] );
iformat = msGetOutputFormatIndex(map,tokens[j]);
if( iformat < 0 )
continue;
format_obj = map->outputformatlist[iformat];
fname = format_obj->name;
if( nWFSVersion >= OWS_1_1_0
&& format_obj->mimetype != NULL )
fname = format_obj->mimetype;
hit = strstr(out_list,fname);
if( hit != NULL
&& (hit[strlen(fname)] == '\0' || hit[strlen(fname)] == ','))
continue;
if( strlen(out_list) + strlen(fname)+3 < out_list_size ) {
strcat( out_list, "," );
strcat( out_list, fname );
} else
break;
}
msFreeCharArray( tokens, n );
}
return out_list;
}
/*
**
*/
static void msWFSPrintRequestCap(const char *request,
const char *script_url,
const char *format_tag,
const char *formats_list)
{
msIO_printf(" <%s>\n", request);
/* We expect to receive a NULL-terminated args list of formats */
if (format_tag != NULL) {
int i, n;
char **tokens;
n = 0;
tokens = msStringSplit(formats_list, ',', &n);
msIO_printf(" <%s>\n", format_tag);
for( i = 0; i < n; i++ ) {
msIO_printf(" <%s/>\n", tokens[i] );
}
msFreeCharArray( tokens, n );
msIO_printf(" </%s>\n", format_tag);
}
msIO_printf(" <DCPType>\n");
msIO_printf(" <HTTP>\n");
msIO_printf(" <Get onlineResource=\"%s\" />\n", script_url);
msIO_printf(" </HTTP>\n");
msIO_printf(" </DCPType>\n");
msIO_printf(" <DCPType>\n");
msIO_printf(" <HTTP>\n");
msIO_printf(" <Post onlineResource=\"%s\" />\n", script_url);
msIO_printf(" </HTTP>\n");
msIO_printf(" </DCPType>\n");
msIO_printf(" </%s>\n", request);
}
/* msWFSLocateSRSInList()
**
** Utility function to check if a space separated list contains the one passed in argument.
** The list comes normaly from ows_srs metadata, and is expected to use the simple EPSG notation
** (EPSG:4326 ESPG:42304 ...). The srs comes from the query string and can either
** be of simple EPSG format or using gc:def:crs:EPSG:xxx format
*/
int msWFSLocateSRSInList(const char *pszList, const char *srs)
{
int nTokens,i;
char **tokens = NULL;
int bFound = MS_FALSE;
char epsg_string[100];
const char *code;
if (!pszList || !srs)
return MS_FALSE;
if (strncasecmp(srs, "EPSG:",5) == 0)
code = srs+5;
else if (strncasecmp(srs, "urn:ogc:def:crs:EPSG:",21) == 0) {
if (srs[21] == ':')
code = srs+21;
else
code = srs+20;
while( *code != ':' && *code != '\0')
code++;
if( *code == ':' )
code++;
} else if (strncasecmp(srs, "urn:EPSG:geographicCRS:",23) == 0)
code = srs + 23;
else
return MS_FALSE;
snprintf( epsg_string, sizeof(epsg_string), "EPSG:%s", code );
tokens = msStringSplit(pszList, ' ', &nTokens );
if (tokens && nTokens > 0) {
for (i=0; i<nTokens; i++) {
if (strcasecmp(tokens[i], epsg_string) == 0) {
bFound = MS_TRUE;
break;
}
}
}
msFreeCharArray(tokens, nTokens);
return bFound;
}
/* msWFSGetFeatureApplySRS()
**
** Utility function called from msWFSGetFeature. It is assumed that at this point
** all queried layers are turned ON
*/
static int msWFSGetFeatureApplySRS(mapObj *map, const char *srs, int nWFSVersion)
{
char *pszMapSRS=NULL;
char *pszOutputSRS=NULL;
layerObj *lp;
int i;
/*validation of SRS
- wfs 1.0 does not have an srsname parameter so all queried layers should be advertized using the
same srs. For wfs 1.1.0 an srsName can be passed, we should validate that It is valid for all
queries layers
*/
/* Start by applying the default service SRS to the mapObj,
* make sure we reproject the map extent if a projection was
* already set
*/
msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FO", MS_TRUE, &pszMapSRS);
if(pszMapSRS && nWFSVersion > OWS_1_0_0){
projectionObj proj;
msInitProjection(&proj);
msProjectionInheritContextFrom(&proj, &(map->projection));
if (map->projection.numargs > 0 && msLoadProjectionStringEPSG(&proj, pszMapSRS) == 0) {
msProjectRect(&(map->projection), &proj, &map->extent);
}
msLoadProjectionStringEPSG(&(map->projection), pszMapSRS);
msFreeProjection(&proj);
}
if (srs == NULL || nWFSVersion == OWS_1_0_0) {
for (i=0; i<map->numlayers; i++) {
char *pszLayerSRS;
lp = GET_LAYER(map, i);
if (lp->status != MS_ON)
continue;
if (pszMapSRS)
pszLayerSRS = pszMapSRS;
else
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "FO", MS_TRUE, &pszLayerSRS);
if (pszLayerSRS == NULL) {
msSetError(MS_WFSERR,
"Server config error: SRS must be set at least at the map or at the layer level.",
"msWFSGetFeature()");
if (pszOutputSRS)
msFree(pszOutputSRS);
/*pszMapSrs would also be NULL, no use freeing*/
return MS_FAILURE;
}
if (pszOutputSRS == NULL)
pszOutputSRS = msStrdup(pszLayerSRS);
else if (strcasecmp(pszLayerSRS,pszOutputSRS) != 0) {
msSetError(MS_WFSERR,
"Invalid GetFeature Request: All TYPENAMES in a single GetFeature request must have been advertized in the same SRS. Please check the capabilities and reformulate your request.",
"msWFSGetFeature()");
if (pszOutputSRS)
msFree(pszOutputSRS);
if(pszLayerSRS != pszMapSRS)
msFree(pszLayerSRS);
msFree(pszMapSRS);
return MS_FAILURE;
}
if(pszLayerSRS != pszMapSRS)
msFree(pszLayerSRS);
}
} else { /*srs is given so it should be valid for all layers*/
/*get all the srs defined at the map level and check them aginst the srsName passed
as argument*/
msFree(pszMapSRS);
msOWSGetEPSGProj(&(map->projection), &(map->web.metadata), "FO", MS_FALSE, &pszMapSRS);
if (pszMapSRS) {
if (!msWFSLocateSRSInList(pszMapSRS, srs)) {
msSetError(MS_WFSERR,
"Invalid GetFeature Request:Invalid SRS. Please check the capabilities and reformulate your request.",
"msWFSGetFeature()");
msFree(pszMapSRS);
return MS_FAILURE;
}
pszOutputSRS = msStrdup(srs);
} else {
for (i=0; i<map->numlayers; i++) {
char *pszLayerSRS=NULL;
lp = GET_LAYER(map, i);
if (lp->status != MS_ON)
continue;
msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "FO", MS_FALSE, &pszLayerSRS);
if (!pszLayerSRS) {
msSetError(MS_WFSERR,
"Server config error: SRS must be set at least at the map or at the layer level.",
"msWFSGetFeature()");
msFree(pszMapSRS);
return MS_FAILURE;
}
if (!msWFSLocateSRSInList(pszLayerSRS, srs)) {
msSetError(MS_WFSERR,
"Invalid GetFeature Request:Invalid SRS. Please check the capabilities and reformulate your request.",
"msWFSGetFeature()");
msFree(pszMapSRS);
msFree(pszLayerSRS);
return MS_FAILURE;
}
msFree(pszLayerSRS);
}
pszOutputSRS = msStrdup(srs);
}
}
if (pszOutputSRS) {
projectionObj sProjTmp;
int nTmp=0;
msInitProjection(&sProjTmp);
msProjectionInheritContextFrom(&sProjTmp, &(map->projection));
if( nWFSVersion >= OWS_1_1_0 ) {
nTmp = msLoadProjectionStringEPSG(&(sProjTmp), pszOutputSRS);
} else {
nTmp = msLoadProjectionString(&(sProjTmp), pszOutputSRS);
}
if (nTmp == 0) {
msProjectRect(&(map->projection), &(sProjTmp), &map->extent);
}
msFreeProjection(&(sProjTmp));
if (nTmp != 0) {
msSetError(MS_WFSERR, "msLoadProjectionString() failed", "msWFSGetFeature()");
return MS_FAILURE;
}
/*we load the projection sting in the map and possibly
set the axis order*/
if( nWFSVersion >= OWS_1_1_0 ) {
msLoadProjectionStringEPSG(&(map->projection), pszOutputSRS);
} else {
msLoadProjectionString(&(map->projection), pszOutputSRS);
}
nTmp = GetMapserverUnitUsingProj(&(map->projection));
if( nTmp != -1 ) {
map->units = static_cast<MS_UNITS>(nTmp);
}
}
msFree(pszOutputSRS);
msFree(pszMapSRS);
return MS_SUCCESS;
}
/* msWFSIsLayerSupported()
**
** Returns true (1) is this layer meets the requirements to be served as
** a WFS feature type.
*/
int msWFSIsLayerSupported(layerObj *lp)
{
/* In order to be supported, lp->type must be specified, even for
** layers that are OGR, SDE, SDO, etc connections.
*/
if ((lp->type == MS_LAYER_POINT ||
lp->type == MS_LAYER_LINE ||
lp->type == MS_LAYER_POLYGON ) &&
lp->connectiontype != MS_WMS &&
lp->connectiontype != MS_GRATICULE) {
return 1; /* true */
}
return 0; /* false */
}
static int msWFSIsLayerAllowed(layerObj *lp, owsRequestObj *ows_request)
{
return msWFSIsLayerSupported(lp) &&
(msIntegerInArray(lp->index, ows_request->enabled_layers, ows_request->numlayers));
}
static layerObj* msWFSGetLayerByName(mapObj* map, owsRequestObj *ows_request, const char* name)
{
int j;
for (j=0; j<map->numlayers; j++) {
layerObj *lp;
lp = GET_LAYER(map, j);
if (msWFSIsLayerAllowed(lp, ows_request) && lp->name && (strcasecmp(lp->name, name) == 0)) {
return lp;
}
}
return NULL;
}
/*
** msWFSDumpLayer()
*/
int msWFSDumpLayer(mapObj *map, layerObj *lp, const char *script_url_encoded)
{
rectObj ext;
char *pszWfsSrs = NULL;
projectionObj poWfs;
msIO_printf(" <FeatureType>\n");
if (lp->name && strlen(lp->name) > 0 &&
(msIsXMLTagValid(lp->name) == MS_FALSE || isdigit(lp->name[0])))
msIO_fprintf(stdout, "<!-- WARNING: The layer name '%s' might contain spaces or "
"invalid characters or may start with a number. This could lead to potential problems. -->\n",lp->name);
msOWSPrintEncodeParam(stdout, "LAYER.NAME", lp->name, OWS_WARN,
" <Name>%s</Name>\n", NULL);
msOWSPrintEncodeMetadata(stdout, &(lp->metadata), "FO", "title",
OWS_WARN, " <Title>%s</Title>\n", lp->name);
msOWSPrintEncodeMetadata(stdout, &(lp->metadata), "FO", "abstract",
OWS_NOERR, " <Abstract>%s</Abstract>\n", NULL);
msOWSPrintEncodeMetadataList(stdout, &(lp->metadata), "FO",
"keywordlist",
" <Keywords>\n",
" </Keywords>\n",
" %s\n", NULL);
/* In WFS, every layer must have exactly one SRS and there is none at */
/* the top level contrary to WMS */
/* */
/* So here is the way we'll deal with SRS: */
/* 1- If a top-level map projection (or wfs_srs metadata) is set then */
/* all layers are advertized in the map's projection and they will */
/* be reprojected on the fly in the GetFeature request. */
/* 2- If there is no top-level map projection (or wfs_srs metadata) then */
/* each layer is advertized in its own projection as defined in the */
/* layer's projection object or wfs_srs metadata. */
/* */
/* if Map has a SRS, Use it for all layers. */
msOWSGetEPSGProj(&(map->projection),&(map->web.metadata),"FO",MS_TRUE, &pszWfsSrs);
if(!pszWfsSrs) {
/* Map has no SRS. Use layer SRS or produce a warning. */
msOWSGetEPSGProj(&(lp->projection),&(lp->metadata), "FO", MS_TRUE, &pszWfsSrs);
}
msOWSPrintEncodeParam(stdout, "(at least one of) MAP.PROJECTION, LAYER.PROJECTION or wfs_srs metadata",
pszWfsSrs, OWS_WARN, " <SRS>%s</SRS>\n", NULL);
/* If layer has no proj set then use map->proj for bounding box. */
if (msOWSGetLayerExtent(map, lp, "FO", &ext) == MS_SUCCESS) {
msInitProjection(&poWfs);
msProjectionInheritContextFrom(&poWfs, &(map->projection));
if (pszWfsSrs != NULL)
msLoadProjectionString(&(poWfs), pszWfsSrs);
if(lp->projection.numargs > 0) {
msOWSPrintLatLonBoundingBox(stdout, " ", &(ext),
&(lp->projection), &(poWfs), OWS_WFS);
} else {
msOWSPrintLatLonBoundingBox(stdout, " ", &(ext),
&(map->projection), &(poWfs), OWS_WFS);
}
msFreeProjection(&poWfs);
} else {
msIO_printf("<!-- WARNING: Optional LatLongBoundingBox could not be established for this layer. Consider setting the EXTENT in the LAYER object, or wfs_extent metadata. Also check that your data exists in the DATA statement -->\n");
}
const char* metadataurl_list = msOWSLookupMetadata(&(lp->metadata), "FO", "metadataurl_list");
if( metadataurl_list ) {
int ntokens = 0;
char** tokens = msStringSplit(metadataurl_list, ' ', &ntokens);
for( int i = 0; i < ntokens; i++ )
{
std::string key("metadataurl_");
key += tokens[i];
msOWSPrintURLType(stdout, &(lp->metadata), "FO", key.c_str(),
OWS_WARN, NULL, "MetadataURL", " type=\"%s\"",
NULL, NULL, " format=\"%s\"", "%s",
MS_TRUE, MS_FALSE, MS_FALSE, MS_TRUE, MS_TRUE,
NULL, NULL, NULL, NULL, NULL, " ");
}
msFreeCharArray(tokens, ntokens);
}
else {
if (! msOWSLookupMetadata(&(lp->metadata), "FO", "metadataurl_href"))
msMetadataSetGetMetadataURL(lp, script_url_encoded);
msOWSPrintURLType(stdout, &(lp->metadata), "FO", "metadataurl",
OWS_WARN, NULL, "MetadataURL", " type=\"%s\"",
NULL, NULL, " format=\"%s\"", "%s",
MS_TRUE, MS_FALSE, MS_FALSE, MS_TRUE, MS_TRUE,
NULL, NULL, NULL, NULL, NULL, " ");
}
if (msOWSLookupMetadata(&(lp->metadata), "OFG", "featureid") == NULL) {
msIO_fprintf(stdout, "<!-- WARNING: Required Feature Id attribute (fid) not specified for this feature type. Make sure you set one of wfs_featureid, ows_featureid or gml_featureid metadata. -->\n");
}
msIO_printf(" </FeatureType>\n");
msFree(pszWfsSrs);
return MS_SUCCESS;
}
/*
** msWFSHandleUpdateSequence()
*/
int msWFSHandleUpdateSequence(mapObj *map, wfsParamsObj *params, const char* pszFunction)
{
/* -------------------------------------------------------------------- */
/* Handle updatesequence */
/* -------------------------------------------------------------------- */
const char* updatesequence = msOWSLookupMetadata(&(map->web.metadata), "FO", "updatesequence");
if (params->pszUpdateSequence != NULL) {
int i = msOWSNegotiateUpdateSequence(params->pszUpdateSequence, updatesequence);
if (i == 0) { /* current */
msSetError(MS_WFSERR, "UPDATESEQUENCE parameter (%s) is equal to server (%s)", pszFunction, params->pszUpdateSequence, updatesequence);
/* FIXME? : according to table 7 of OWS 1.1, we should return a service */
/* metadata document with only “version” and “updateSequence” parameters */
return msWFSException(map, "updatesequence", "CurrentUpdateSequence", params->pszVersion);
}
if (i > 0) { /* invalid */
msSetError(MS_WFSERR, "UPDATESEQUENCE parameter (%s) is higher than server (%s)", pszFunction, params->pszUpdateSequence, updatesequence);
/* locator must be NULL. See Table 25 of OWS 1.1 */
return msWFSExceptionNoLocator(map, "updatesequence", MS_OWS_ERROR_INVALID_UPDATE_SEQUENCE, params->pszVersion);
}
}
return MS_SUCCESS;
}
/*
** msWFSGetCapabilitiesNegotiateVersion()
*/
static int msWFSGetCapabilitiesNegotiateVersion(mapObj *map, wfsParamsObj *wfsparams)
{
int iVersion = -1;
char tmpString[OWS_VERSION_MAXLEN];
/* acceptversions: do OWS Common style of version negotiation */
if (wfsparams->pszAcceptVersions && strlen(wfsparams->pszAcceptVersions) > 0) {
char **tokens;
int i, j;
tokens = msStringSplit(wfsparams->pszAcceptVersions, ',', &j);
for (i=0; i<j; i++) {
iVersion = msOWSParseVersionString(tokens[i]);
if (iVersion < 0) {
msSetError(MS_WFSERR, "Invalid version format : %s.", "msWFSGetCapabilities()", tokens[i]);
msFreeCharArray(tokens, j);
return msWFSException(map, "acceptversions", MS_OWS_ERROR_INVALID_PARAMETER_VALUE,NULL);
}
/* negotiate version */
iVersion = msOWSCommonNegotiateVersion(iVersion, wfsSupportedVersions, wfsNumSupportedVersions);
if (iVersion != -1)
break;
}
msFreeCharArray(tokens, j);
if(iVersion == -1) {
msSetError(MS_WFSERR, "ACCEPTVERSIONS list (%s) does not match supported versions", "msWFSGetCapabilities()", wfsparams->pszAcceptVersions);
/* locator must be NULL. See Table 25 of OWS 1.1 */
return msWFSExceptionNoLocator(map, "acceptversions", MS_OWS_ERROR_VERSION_NEGOTIATION_FAILED,NULL);
}
} else {
/* negotiate version */
int tmpInt;
iVersion = msOWSParseVersionString(wfsparams->pszVersion);
if( iVersion < 0 )
{
return msWFSException(map, "version", MS_OWS_ERROR_INVALID_PARAMETER_VALUE, NULL);
}
tmpInt = msOWSCommonNegotiateVersion(iVersion, wfsSupportedVersions, wfsNumSupportedVersions);
/* Old style negociation : paragraph D.11 of OWS 1.1.0 spec */
if( tmpInt < 0 )
{
int i;
for( i = 0 ; i < wfsNumSupportedVersions; i++ )
{
if( iVersion >= wfsSupportedVersions[i] )
{
iVersion = wfsSupportedVersions[i];
break;
}
}
if( i == wfsNumSupportedVersions )
iVersion = wfsSupportedVersions[wfsNumSupportedVersions - 1];
}
}
/* set result as string and carry on */
if (wfsparams->pszVersion)
msFree(wfsparams->pszVersion);
wfsparams->pszVersion = msStrdup(msOWSGetVersionString(iVersion, tmpString));
return MS_SUCCESS;
}
/*
** msWFSGetCapabilities()
*/
int msWFSGetCapabilities(mapObj *map, wfsParamsObj *wfsparams, cgiRequestObj *req, owsRequestObj *ows_request)
{
char *script_url=NULL, *script_url_encoded;
const char *updatesequence=NULL;
const char *wmtver=NULL;
char *formats_list;
int ret;
int iVersion;
int i=0;
ret = msWFSGetCapabilitiesNegotiateVersion(map, wfsparams);
if( ret != MS_SUCCESS )
return ret;
iVersion = msOWSParseVersionString(wfsparams->pszVersion);
if( iVersion == OWS_2_0_0 )
return msWFSGetCapabilities20( map, wfsparams, req, ows_request);
if( iVersion == OWS_1_1_0 )
return msWFSGetCapabilities11( map, wfsparams, req, ows_request);
/* Decide which version we're going to return... only 1.0.0 for now */
wmtver = "1.0.0";
/* We need this server's onlineresource. */
if ((script_url=msOWSGetOnlineResource(map, "FO", "onlineresource", req)) == NULL ||
(script_url_encoded = msEncodeHTMLEntities(script_url)) == NULL) {
msSetError(MS_WFSERR, "Server URL not found", "msWFSGetCapabilities()");
return msWFSException(map, "mapserv", MS_OWS_ERROR_NO_APPLICABLE_CODE, wmtver);
}
free(script_url);
script_url = NULL;
ret = msWFSHandleUpdateSequence(map, wfsparams, "msWFSGetCapabilities()");
if( ret != MS_SUCCESS )
{
free(script_url_encoded);
return ret;
}
msIO_setHeader("Content-Type","text/xml; charset=UTF-8");
msIO_sendHeaders();
msIO_printf("<?xml version='1.0' encoding=\"UTF-8\" ?>\n");
updatesequence = msOWSLookupMetadata(&(map->web.metadata), "FO", "updatesequence");
msIO_printf("<WFS_Capabilities \n"
" version=\"%s\" \n"
" updateSequence=\"%s\" \n"
" xmlns=\"http://www.opengis.net/wfs\" \n"
" xmlns:ogc=\"http://www.opengis.net/ogc\" \n"
" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n"
" xsi:schemaLocation=\"http://www.opengis.net/wfs %s/wfs/%s/WFS-capabilities.xsd\">\n",
wmtver, updatesequence ? updatesequence : "0",
msOWSGetSchemasLocation(map), wmtver);
/* Report MapServer Version Information */
msIO_printf("\n<!-- %s -->\n\n", msGetVersion());
/*
** SERVICE definition
*/
msIO_printf("<Service>\n");
msIO_printf(" <Name>MapServer WFS</Name>\n");
/* the majority of this section is dependent on appropriately named metadata in the WEB object */
msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "FO", "title",
OWS_WARN, " <Title>%s</Title>\n", map->name);
msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "FO", "abstract",
OWS_NOERR, " <Abstract>%s</Abstract>\n", NULL);
msOWSPrintEncodeMetadataList(stdout, &(map->web.metadata), "FO",
"keywordlist",
" <Keywords>\n", " </Keywords>\n",
" %s\n", NULL);
/* Service/onlineresource */
/* Defaults to same as request onlineresource if wfs_service_onlineresource */
/* is not set. */
msOWSPrintEncodeMetadata(stdout, &(map->web.metadata),
"FO", "service_onlineresource", OWS_NOERR,
" <OnlineResource>%s</OnlineResource>\n",
script_url_encoded);
msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "FO", "fees",
OWS_NOERR, " <Fees>%s</Fees>\n", NULL);
msOWSPrintEncodeMetadata(stdout, &(map->web.metadata), "FO",
"accessconstraints", OWS_NOERR,
" <AccessConstraints>%s</AccessConstraints>\n",
NULL);
msIO_printf("</Service>\n\n");
/*
** CAPABILITY definitions: list of supported requests
*/
msIO_printf("<Capability>\n");
msIO_printf(" <Request>\n");
msWFSPrintRequestCap("GetCapabilities", script_url_encoded,
NULL, NULL);
/* msWFSPrintRequestCap("DescribeFeatureType", script_url_encoded, "SchemaDescriptionLanguage", "XMLSCHEMA", "SFE_XMLSCHEMA", NULL); */
/* msWFSPrintRequestCap("GetFeature", script_url_encoded, "ResultFormat", "GML2", "GML3", NULL); */
/* don't advertise the GML3 or GML for SFE support */
if (msOWSRequestIsEnabled(map, NULL, "F", "DescribeFeatureType", MS_TRUE))
msWFSPrintRequestCap("DescribeFeatureType", script_url_encoded, "SchemaDescriptionLanguage", "XMLSCHEMA" );
if (msOWSRequestIsEnabled(map, NULL, "F", "GetFeature", MS_TRUE)) {
formats_list = msWFSGetOutputFormatList( map, NULL, OWS_1_0_0 );
msWFSPrintRequestCap("GetFeature", script_url_encoded, "ResultFormat", formats_list );
msFree( formats_list );
}
msIO_printf(" </Request>\n");
msIO_printf("</Capability>\n\n");
/*
** FeatureTypeList: layers
*/
msIO_printf("<FeatureTypeList>\n");
/* Operations supported... set default at top-level, and more operations */
/* can be added inside each layer... for MapServer only query is supported */
msIO_printf(" <Operations>\n");
msIO_printf(" <Query/>\n");
msIO_printf(" </Operations>\n");
for(i=0; i<map->numlayers; i++) {
layerObj *lp;
lp = GET_LAYER(map, i);
if (lp->status == MS_DELETE)
continue;
if (msWFSIsLayerAllowed(lp, ows_request)) {
msWFSDumpLayer(map, lp, script_url_encoded);
}
}
msIO_printf("</FeatureTypeList>\n\n");
/*
** OGC Filter Capabilities ... for now we support only BBOX
*/
msIO_printf("<ogc:Filter_Capabilities>\n");
msIO_printf(" <ogc:Spatial_Capabilities>\n");
msIO_printf(" <ogc:Spatial_Operators>\n");
#ifdef USE_GEOS
msIO_printf(" <ogc:Equals/>\n");
msIO_printf(" <ogc:Disjoint/>\n");
msIO_printf(" <ogc:Touches/>\n");
msIO_printf(" <ogc:Within/>\n");
msIO_printf(" <ogc:Overlaps/>\n");
msIO_printf(" <ogc:Crosses/>\n");
msIO_printf(" <ogc:Intersect/>\n");
msIO_printf(" <ogc:Contains/>\n");
msIO_printf(" <ogc:DWithin/>\n");
#endif
msIO_printf(" <ogc:BBOX/>\n");
msIO_printf(" </ogc:Spatial_Operators>\n");
msIO_printf(" </ogc:Spatial_Capabilities>\n");
msIO_printf(" <ogc:Scalar_Capabilities>\n");
msIO_printf(" <ogc:Logical_Operators />\n");
msIO_printf(" <ogc:Comparison_Operators>\n");
msIO_printf(" <ogc:Simple_Comparisons />\n");
msIO_printf(" <ogc:Like />\n");
msIO_printf(" <ogc:Between />\n");
msIO_printf(" </ogc:Comparison_Operators>\n");
msIO_printf(" </ogc:Scalar_Capabilities>\n");
msIO_printf("</ogc:Filter_Capabilities>\n\n");
/*
** Done!
*/
msIO_printf("</WFS_Capabilities>\n");
free(script_url_encoded);
return MS_SUCCESS;
}
/*
** Helper functions for producing XML schema.
*/
static const char *msWFSGetGeometryType(const char *type, OWSGMLVersion outputformat)
{
if(!type) return "GeometryPropertyType";
if(strcasecmp(type, "point") == 0) {
switch(outputformat) {
case OWS_GML2:
case OWS_GML3:
case OWS_GML32:
return "PointPropertyType";
}
} else if(strcasecmp(type, "multipoint") == 0) {
switch(outputformat) {
case OWS_GML2:
case OWS_GML3:
case OWS_GML32:
return "MultiPointPropertyType";
}
} else if(strcasecmp(type, "line") == 0) {
switch(outputformat) {
case OWS_GML2:
return "LineStringPropertyType";
case OWS_GML3:
case OWS_GML32:
return "CurvePropertyType";
}
} else if(strcasecmp(type, "multiline") == 0) {
switch(outputformat) {
case OWS_GML2:
return "MultiLineStringPropertyType";
case OWS_GML3:
case OWS_GML32:
return "MultiCurvePropertyType";
}
} else if(strcasecmp(type, "polygon") == 0) {
switch(outputformat) {
case OWS_GML2:
return "PolygonPropertyType";
case OWS_GML3:
case OWS_GML32:
return "SurfacePropertyType";