forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapogcsos.c
3090 lines (2596 loc) · 124 KB
/
mapogcsos.c
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: OGC SOS implementation
* Author: Y. Assefa, DM Solutions Group ([email protected])
*
**********************************************************************
* Copyright (c) 2006, Y. Assefa, DM Solutions Group Inc
*
* 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 _GNU_SOURCE
#include "mapserver.h"
#if defined(USE_SOS_SVR) && defined(USE_LIBXML2)
#include "maperror.h"
#include "mapthread.h"
#include "mapows.h"
#include "maptime.h"
#include "mapgml.h"
#include "mapogcfilter.h"
#include "mapowscommon.h"
#include "maplibxml2.h"
#include "libxml/parser.h"
#include "libxml/tree.h"
#include "libxml/xpath.h"
#include "libxml/xpathInternals.h"
const char *pszSOSVersion = "1.0.0";
const char *pszSOSNamespaceUri = "http://www.opengis.net/sos/1.0";
const char *pszSOSNamespacePrefix = "sos";
const char *pszOMNamespaceUri = "http://www.opengis.net/om/1.0";
const char *pszOMNamespacePrefix = "om";
const char *pszSOSDescribeSensorMimeType = "text/xml; subtype=\"sensorML/1.0.0\"";
const char *pszSOSGetObservationMimeType = "text/xml; subtype=\"om/1.0.0\"";
typedef struct {
char *pszProcedure;
xmlNodePtr psResultNode;
} SOSProcedureNode;
int msSOSParseRequest(mapObj *map, cgiRequestObj *request, sosParamsObj *sosparams);
void msSOSFreeParamsObj(sosParamsObj *sosparams);
/*
** msSOSException()
**
** Report current MapServer error in XML exception format.
** Wrapper function around msOWSCommonExceptionReport. Merely
** passes SOS specific info.
**
*/
static int msSOSException(mapObj *map, char *locator, char *exceptionCode)
{
int size = 0;
char *errorString = NULL;
char *errorMessage = NULL;
char *schemasLocation = NULL;
const char *encoding;
xmlDocPtr psDoc = NULL;
xmlNodePtr psRootNode = NULL;
xmlNsPtr psNsOws = NULL;
xmlChar *buffer = NULL;
psNsOws = xmlNewNs(NULL, BAD_CAST "http://www.opengis.net/ows/1.1", BAD_CAST "ows");
encoding = msOWSLookupMetadata(&(map->web.metadata), "SO", "encoding");
errorString = msGetErrorString("\n");
errorMessage = msEncodeHTMLEntities(errorString);
schemasLocation = msEncodeHTMLEntities(msOWSGetSchemasLocation(map));
psDoc = xmlNewDoc(BAD_CAST "1.0");
psRootNode = msOWSCommonExceptionReport(psNsOws, OWS_1_1_0, schemasLocation, pszSOSVersion, msOWSGetLanguage(map, "exception"), exceptionCode, locator, errorMessage);
xmlDocSetRootElement(psDoc, psRootNode);
xmlNewNs(psRootNode, BAD_CAST "http://www.opengis.net/ows/1.1", BAD_CAST "ows");
if (encoding)
msIO_setHeader("Content-Type","text/xml; charset=%s", encoding);
else
msIO_setHeader("Content-Type","text/xml");
msIO_sendHeaders();
xmlDocDumpFormatMemoryEnc(psDoc, &buffer, &size, (encoding ? encoding : "ISO-8859-1"), 1);
msIO_printf("%s", buffer);
/*free buffer and the document */
free(errorString);
free(errorMessage);
free(schemasLocation);
xmlFree(buffer);
xmlFreeDoc(psDoc);
xmlFreeNs(psNsOws);
/*
** The typical pattern is to call msSOSException() right after
** msSetError(). In order to prevent mapserv.c from re-reporting this
** error at a higher level, we mark it as reported here. #3571
*/
{
errorObj *err = msGetErrorObj();
if( err != NULL && err->code != MS_NOERR )
err->isreported = MS_TRUE;
}
return MS_FAILURE;
}
static int _IsInList(char **papsProcedures, int nDistinctProcedures, char *pszProcedure)
{
int i = 0;
if (papsProcedures && nDistinctProcedures > 0 && pszProcedure) {
for (i=0; i<nDistinctProcedures; i++) {
if (papsProcedures[i] && strcmp(papsProcedures[i], pszProcedure) == 0)
return 1;
}
}
return 0;
}
/************************************************************************/
/* msSOSValidateFilter */
/* */
/* Look if the filter's property names have an equivalent */
/* layre's attribute. */
/************************************************************************/
static int msSOSValidateFilter(FilterEncodingNode *psFilterNode,
layerObj *lp)
{
int i=0, bFound =0;
/* assuming here that the layer is opened*/
if (psFilterNode && lp) {
if (psFilterNode->eType == FILTER_NODE_TYPE_PROPERTYNAME) {
for (i=0; i<lp->numitems; i++) {
if (strcasecmp(lp->items[i], psFilterNode->pszValue) == 0) {
bFound = 1;
break;
}
}
if (!bFound)
return MS_FALSE;
}
if (psFilterNode->psLeftNode && psFilterNode->eType != FILTER_NODE_TYPE_SPATIAL) {
if (msSOSValidateFilter(psFilterNode->psLeftNode, lp) == MS_FALSE)
return MS_FALSE;
}
if (psFilterNode->psRightNode && psFilterNode->eType != FILTER_NODE_TYPE_SPATIAL) {
if (msSOSValidateFilter(psFilterNode->psRightNode, lp) == MS_FALSE)
return MS_FALSE;
}
}
return MS_TRUE;
}
/************************************************************************/
/* msSOSAddMetadataChildNode */
/* */
/* Utility function to add a metadata node. */
/************************************************************************/
void msSOSAddMetadataChildNode(xmlNodePtr psParent, const char *psNodeName,
xmlNsPtr psNs, hashTableObj *metadata,
const char *psNamespaces,
const char *psMetadataName,
const char *psDefaultValue)
{
xmlNodePtr psNode = NULL;
char *psValue = NULL;
if (psParent && psNodeName) {
psValue = msOWSGetEncodeMetadata(metadata, psNamespaces, psMetadataName,
psDefaultValue);
if (psValue) {
psNode = xmlNewChild(psParent, NULL, BAD_CAST psNodeName, BAD_CAST psValue);
if (psNs)
xmlSetNs(psNode, psNs);
free(psValue);
}
}
}
/************************************************************************/
/* msSOSGetFirstLayerForOffering */
/* */
/* return the first layer for the offering. */
/************************************************************************/
layerObj *msSOSGetFirstLayerForOffering(mapObj *map, const char *pszOffering,
const char *pszProperty)
{
layerObj *lp = NULL;
const char *pszTmp = NULL;
int i = 0;
if (pszOffering && map) {
for (i=0; i<map->numlayers; i++) {
pszTmp =
msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "S", "offering_id");
if (pszTmp && (strcasecmp(pszTmp, pszOffering) == 0)) {
if (pszProperty) {
pszTmp =
msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "S",
"observedproperty_id");
if (pszTmp && (strcasecmp(pszTmp, pszProperty) == 0)) {
lp = (GET_LAYER(map, i));
break;
}
} else {
lp = (GET_LAYER(map, i));
break;
}
}
}
}
return lp;
}
xmlNodePtr msSOSAddTimeNode(xmlNsPtr psNs, xmlNsPtr psNsGml, char *pszStart, char *pszEnd)
{
xmlNodePtr psNode=NULL;
char *timeel= NULL;
if (strcmp((char *)psNs->prefix,"sos") == 0)
timeel = "time";
if (strcmp((char *)psNs->prefix,"om") == 0)
timeel = "samplingTime";
else
timeel = "time";
psNode = xmlNewNode(psNs, BAD_CAST timeel);
xmlAddChild(psNode, msGML3TimePeriod(psNsGml, pszStart, pszEnd));
return psNode;
}
void msSOSAddPropertyNode(xmlNsPtr psNsSwe, xmlNsPtr psNsXLink, xmlNodePtr psParent, layerObj *lp, xmlNsPtr psNsGml, char *pszCompositePhenomenonId)
{
const char *pszValue = NULL;
char *pszTmpVal = NULL, *pszFullName = NULL;
xmlNodePtr psCompNode, psNode;
int i, j=0;
char szTmp[256];
const char *pszComponentBase = "urn:ogc:def:property:";
if (psParent && lp) {
psNode = xmlNewChild(psParent, NULL, BAD_CAST "observedProperty", NULL);
psCompNode = xmlNewChild(psNode, psNsSwe, BAD_CAST "CompositePhenomenon", NULL);
pszValue = msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id");
pszTmpVal = msStrdup(pszValue);
if (pszCompositePhenomenonId != NULL) { /* unique value needs to be constructed */
pszTmpVal = msStringConcatenate(pszTmpVal, "_");
pszTmpVal = msStringConcatenate(pszTmpVal, pszCompositePhenomenonId);
}
if (pszTmpVal) { /*should always be true */
xmlNewNsProp(psCompNode, psNsGml, BAD_CAST "id", BAD_CAST pszTmpVal);
msFree(pszTmpVal);
}
pszValue = msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_name");
if (pszValue)
psNode = xmlNewChild(psCompNode, psNsGml,
BAD_CAST "name", BAD_CAST pszValue);
/* add components */
/*assuming that the layer is opened and msLayerGetItems called*/
for(i=0; i<lp->numitems; i++) {
pszValue = msOWSLookupMetadata(&(lp->metadata), "S", "observedproperty_authority");
if (pszValue)
pszTmpVal = msStrdup(pszValue);
else
pszTmpVal = msStrdup("OGC-SWE");
pszFullName = msStrdup(pszComponentBase);
pszFullName = msStringConcatenate(pszFullName, pszTmpVal);
free(pszTmpVal);
pszFullName = msStringConcatenate(pszFullName, ":");
pszValue = msOWSLookupMetadata(&(lp->metadata), "S", "observedproperty_version");
if (pszValue)
pszTmpVal = msStrdup(pszValue);
else
pszTmpVal = msStrdup("1");
pszFullName = msStringConcatenate(pszFullName, pszTmpVal);
free(pszTmpVal);
pszFullName = msStringConcatenate(pszFullName, ":");
snprintf(szTmp, sizeof(szTmp), "%s_alias", lp->items[i]);
pszValue = msOWSLookupMetadata(&(lp->metadata), "S", szTmp);
if (pszValue)
pszTmpVal = msStrdup(pszValue);
else
pszTmpVal = msStrdup(lp->items[i]);
pszFullName = msStringConcatenate(pszFullName, pszTmpVal);
psNode = xmlNewChild(psCompNode, psNsSwe, BAD_CAST "component", NULL);
xmlNewNsProp(psNode, psNsXLink, BAD_CAST "href", BAD_CAST pszFullName);
free(pszFullName);
free(pszTmpVal);
j++;
}
pszTmpVal = msIntToString(j);
xmlNewNsProp(psCompNode, NULL, BAD_CAST "dimension", BAD_CAST pszTmpVal);
free(pszTmpVal);
}
}
/************************************************************************/
/* msSOSAddGeometryNode */
/* */
/* Outout gml 2 gemptry nodes based on a shape. All logic comes */
/* from gmlWriteGeometry_GML2. Should be merged at one point if */
/* possible. */
/************************************************************************/
void msSOSAddGeometryNode(xmlNsPtr psNsGml, xmlNsPtr psNsMs, xmlNodePtr psParent, mapObj *map, layerObj *lp, shapeObj *psShape,
const char *pszEpsg)
{
char *pszTmp = NULL;
int i,j = 0;
xmlNodePtr psPointNode, psNode, psLineNode, psPolygonNode;
int *panOuterList = NULL, *panInnerList = NULL;
if (psParent && psShape) {
if (msProjectionsDiffer(&map->projection, &lp->projection) == MS_TRUE) {
msProjectShape(&lp->projection, &map->projection, psShape);
pszEpsg = msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE);
}
switch(psShape->type) {
case(MS_SHAPE_POINT):
psNode = xmlNewChild(psParent, NULL, BAD_CAST "msGeometry", NULL);
xmlSetNs(psNode, psNsMs);
if (psShape->line[0].numpoints > 1) {
psPointNode = xmlNewChild(psNode, NULL, BAD_CAST "MultiPoint", NULL);
xmlSetNs(psPointNode, psNsGml);
if (pszEpsg)
xmlNewProp(psPointNode, BAD_CAST "srsName", BAD_CAST pszEpsg);
} else
psPointNode= psNode;
/*add all points */
for(i=0; i<psShape->line[0].numpoints; i++) {
psNode = xmlAddChild(psPointNode, msGML3Point(psNsGml, pszEpsg, NULL, psShape->line[0].point[i].x, psShape->line[0].point[i].y));
}
break;
case(MS_SHAPE_LINE):
psNode = xmlNewChild(psParent, NULL, BAD_CAST "msGeometry", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, NULL, NULL));
if (psShape->numlines > 1) {
psLineNode = xmlNewChild(psNode, NULL, BAD_CAST "MultiLineString", NULL);
xmlSetNs(psLineNode,xmlNewNs(psLineNode,
BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
if (pszEpsg)
xmlNewProp(psLineNode, BAD_CAST "srsName", BAD_CAST pszEpsg);
} else
psLineNode= psNode;
for(i=0; i<psShape->numlines; i++) {
if (psShape->numlines > 1) {
psNode = xmlNewChild(psLineNode, NULL, BAD_CAST "lineStringMember", NULL);
xmlSetNs(psNode,xmlNewNs(psNode,
BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
psNode = xmlNewChild(psNode, NULL, BAD_CAST "LineString", NULL);
xmlSetNs(psNode,xmlNewNs(psNode,
BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
} else {
psNode = xmlNewChild(psLineNode, NULL, BAD_CAST "LineString", NULL);
xmlSetNs(psNode,xmlNewNs(psNode,
BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
}
if (pszEpsg)
xmlNewProp(psNode, BAD_CAST "srsName", BAD_CAST pszEpsg);
pszTmp = NULL;
for(j=0; j<psShape->line[i].numpoints; j++) {
pszTmp = msStringConcatenate(pszTmp,
msDoubleToString(psShape->line[i].point[j].x, MS_TRUE));
pszTmp = msStringConcatenate(pszTmp, ",");
pszTmp = msStringConcatenate(pszTmp,
msDoubleToString(psShape->line[i].point[j].y, MS_TRUE));
pszTmp = msStringConcatenate(pszTmp, " ");
}
psNode = xmlNewChild(psNode, NULL, BAD_CAST "coordinates", BAD_CAST pszTmp);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml", BAD_CAST "gml"));
free(pszTmp);
}
break;
case(MS_SHAPE_POLYGON):
psNode = xmlNewChild(psParent, NULL, BAD_CAST "msGeometry", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, NULL, NULL));
if (psShape->numlines > 1) {
psPolygonNode = xmlNewChild(psNode, NULL, BAD_CAST "MultiPolygon", NULL);
xmlSetNs(psPolygonNode,
xmlNewNs(psPolygonNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
if (pszEpsg)
xmlNewProp(psPolygonNode, BAD_CAST "srsName", BAD_CAST pszEpsg);
} else
psPolygonNode= psNode;
panOuterList = msGetOuterList(psShape);
for(i=0; i<psShape->numlines; i++) {
if(panOuterList[i] != MS_TRUE)
continue;
panInnerList = msGetInnerList(psShape, i, panOuterList);
if (psShape->numlines > 1) {
psNode = xmlNewChild(psPolygonNode, NULL, BAD_CAST "polygonMember", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
psNode = xmlNewChild(psNode, NULL, BAD_CAST "Polygon", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
} else {
psNode = xmlNewChild(psPolygonNode, NULL, BAD_CAST "Polygon", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
}
if (pszEpsg)
xmlNewProp(psNode, BAD_CAST "srsName", BAD_CAST pszEpsg);
psNode = xmlNewChild(psNode, NULL, BAD_CAST "outerBoundaryIs", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
psNode = xmlNewChild(psNode, NULL, BAD_CAST "LinearRing", NULL);
xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
pszTmp = NULL;
for(j=0; j<psShape->line[i].numpoints; j++) {
pszTmp =
msStringConcatenate(pszTmp,
msDoubleToString(psShape->line[i].point[j].x, MS_TRUE));
pszTmp = msStringConcatenate(pszTmp, ",");
pszTmp =
msStringConcatenate(pszTmp,
msDoubleToString(psShape->line[i].point[j].y, MS_TRUE));
pszTmp = msStringConcatenate(pszTmp, " ");
}
psNode = xmlNewChild(psNode, NULL, BAD_CAST "coordinates", BAD_CAST pszTmp);
xmlSetNs(psNode,xmlNewNs(psNode,
BAD_CAST "http://www.opengis.net/gml",
BAD_CAST "gml"));
free(pszTmp);
if (panInnerList)
free(panInnerList);
}
if (panOuterList)
free(panOuterList);
break;
default:
break;
}
}
}
/************************************************************************/
/* void msSOSAddDataBlockDefinition(xmlNodePtr psParent, */
/* layerObj *lp) */
/* */
/* Add a databalock used for GetObservation request. */
/************************************************************************/
void msSOSAddDataBlockDefinition(xmlNsPtr psNsSwe, xmlNodePtr psParent, layerObj *lp)
{
xmlNodePtr psNode, psRecordNode, psCompNode, psSubNode, psEncNode;
const char *pszDefinition = NULL, *pszUom=NULL, *pszValue=NULL, *pszName=NULL;
char szTmp[100];
int i=0;
char *pszTokenValue = NULL;
char *pszBlockValue = NULL;
const char *pszBlockSep=NULL, *pszTokenSep=NULL;
if (psParent) {
psNode = xmlNewChild(psParent, NULL, BAD_CAST "DataBlockDefinition", NULL);
xmlSetNs(psNode, psNsSwe);
/* -------------------------------------------------------------------- */
/* Add components. */
/* -------------------------------------------------------------------- */
psCompNode = xmlNewChild(psNode, NULL, BAD_CAST "components", NULL);
psEncNode = xmlNewChild(psNode, NULL, BAD_CAST "encoding", NULL);
psRecordNode = xmlNewChild(psCompNode, NULL, BAD_CAST "DataRecord", NULL);
/*always add a time field if timeitem is defined*/
if (msOWSLookupMetadata(&(lp->metadata), "SO", "timeitem")) {
psNode = xmlNewChild(psRecordNode, NULL, BAD_CAST "field", NULL);
xmlNewNsProp(psNode, NULL, BAD_CAST "name", BAD_CAST "time");
psNode = xmlNewChild(psNode, NULL, BAD_CAST "Time", NULL);
xmlNewNsProp(psNode, NULL, BAD_CAST "definition", BAD_CAST "urn:ogc:phenomenon:time:iso8601");
}
/*add all other fields*/
/*assuming that the layer is open */
for(i=0; i<lp->numitems; i++) {
snprintf(szTmp, sizeof(szTmp), "%s_alias", lp->items[i]);
pszValue = msOWSLookupMetadata(&(lp->metadata), "S", szTmp);
if (pszValue) {
psNode = xmlNewChild(psRecordNode, NULL, BAD_CAST "field", NULL);
/* check if there is an alias/full name used */
snprintf(szTmp, sizeof(szTmp), "%s_alias", lp->items[i]);
pszName = msOWSLookupMetadata(&(lp->metadata), "S", szTmp);
if (!pszName)
pszName = lp->items[i];
xmlNewNsProp(psNode, NULL, BAD_CAST "name", BAD_CAST pszName);
psNode = xmlNewChild(psNode, NULL, BAD_CAST "Quantity", NULL);
/* get definition and uom */
snprintf(szTmp, sizeof(szTmp), "%s_definition", lp->items[i]);
pszDefinition = msOWSLookupMetadata(&(lp->metadata), "S", szTmp);
if (pszDefinition == NULL)
pszDefinition = "urn:ogc:object:definition";
xmlNewNsProp(psNode, NULL, BAD_CAST "definition", BAD_CAST pszDefinition);
snprintf(szTmp, sizeof(szTmp), "%s_uom", lp->items[i]);
pszUom = msOWSLookupMetadata(&(lp->metadata), "S", szTmp);
if (pszUom == NULL)
pszUom = "urn:ogc:object:uom";
psNode = xmlNewChild(psNode, NULL, BAD_CAST "uom", NULL);
xmlNewNsProp(psNode, NULL, BAD_CAST "code", BAD_CAST pszUom);
}
}
/* -------------------------------------------------------------------- */
/* Add encoding block. */
/* -------------------------------------------------------------------- */
pszBlockSep = msOWSLookupMetadata(&(lp->map->web.metadata), "S",
"encoding_blockSeparator");
pszTokenSep = msOWSLookupMetadata(&(lp->map->web.metadata), "S",
"encoding_tokenSeparator");
psSubNode = xmlNewChild(psEncNode, NULL, BAD_CAST "TextBlock", NULL);
if (pszTokenSep)
pszTokenValue = msStringConcatenate(pszTokenValue, (char *)pszTokenSep);
else
pszTokenValue = msStringConcatenate(pszTokenValue, ",");
xmlNewNsProp(psSubNode, NULL, BAD_CAST "tokenSeparator", BAD_CAST pszTokenValue);
if (pszBlockSep)
pszBlockValue = msStringConcatenate(pszBlockValue, (char *)pszBlockSep);
else
pszBlockValue = msStringConcatenate(pszBlockValue, "\n");
xmlNewNsProp(psSubNode, NULL, BAD_CAST "blockSeparator", BAD_CAST pszBlockValue);
xmlNewNsProp(psSubNode, NULL, BAD_CAST "decimalSeparator", BAD_CAST ".");
msFree(pszTokenValue);
msFree(pszBlockValue);
}
}
/************************************************************************/
/* msSOSAddMemberNode */
/* */
/* Add a memeber node corresponding to a feature. */
/* Assuming that the layer is opened and msLayerGetItems is */
/* called on it. */
/************************************************************************/
void msSOSAddMemberNode(xmlNsPtr psNsGml, xmlNsPtr psNsOm, xmlNsPtr psNsSwe, xmlNsPtr psNsXLink, xmlNsPtr psNsMs, xmlNodePtr psParent, mapObj *map, layerObj *lp,
int iFeatureId, const char *script_url, const char *opLayerName)
{
xmlNodePtr psObsNode, psNode, psLayerNode = NULL;
const char *pszEpsg = NULL, *pszValue = NULL;
int status,i,j;
shapeObj sShape;
char szTmp[256];
layerObj *lpfirst = NULL;
const char *pszTimeField = NULL;
char *pszTmp = NULL;
char *pszOid = NULL;
char *pszTime = NULL;
char *pszValueShape = NULL;
const char *pszFeatureId = NULL;
if (psParent) {
msInitShape(&sShape);
status = msLayerGetShape(lp, &sShape, &(lp->resultcache->results[iFeatureId]));
if(status != MS_SUCCESS) {
xmlFreeNs(psNsOm);
return;
}
psNode = xmlNewChild(psParent, NULL, BAD_CAST "member", NULL);
psObsNode = xmlNewChild(psNode, NULL, BAD_CAST "Observation", BAD_CAST pszValue);
pszFeatureId = msOWSLookupMetadata(&(lp->metadata), "OSG", "featureid");
if(pszFeatureId && msLayerGetItems(lp) == MS_SUCCESS) {
/* find the featureid amongst the items for this layer */
for(j=0; j<lp->numitems; j++) {
if(strcasecmp(lp->items[j], pszFeatureId) == 0) { /* found it */
break;
}
}
if (j<lp->numitems) {
pszOid = msStringConcatenate(pszOid, "o_");
pszOid = msStringConcatenate(pszOid, sShape.values[j]);
xmlNewNsProp(psObsNode, psNsGml, BAD_CAST "id", BAD_CAST pszOid);
}
}
/* order of elements is time, location, procedure, observedproperty
featureofinterest, result */
/* time*/
pszTimeField = msOWSLookupMetadata(&(lp->metadata), "SO",
"timeitem");
if (pszTimeField && sShape.values) {
for(i=0; i<lp->numitems; i++) {
if (strcasecmp(lp->items[i], pszTimeField) == 0) {
if (sShape.values[i] && strlen(sShape.values[i]) > 0) {
pszTime = msStringConcatenate(pszTime, sShape.values[i]);
psNode = xmlNewChild(psObsNode, psNsOm, BAD_CAST "samplingTime", NULL);
xmlAddChild(psNode, msGML3TimeInstant(psNsGml, pszTime));
msFree(pszTime);
}
break;
}
}
}
/*TODO add location*/
/*procedure*/
/* if a procedure_item is defined, we should extract the value for the
attributes. If not dump what is found in the procedure metadata bug 2054*/
if ((pszValue = msOWSLookupMetadata(&(lp->metadata), "S", "procedure_item"))) {
lpfirst = msSOSGetFirstLayerForOffering(map,
msOWSLookupMetadata(&(lp->metadata), "S",
"offering_id"),
msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id"));
if (lp != lpfirst)
status = msLayerOpen(lpfirst);
if (status == MS_SUCCESS && msLayerGetItems(lpfirst) == MS_SUCCESS) {
for(i=0; i<lpfirst->numitems; i++) {
if (strcasecmp(lpfirst->items[i], pszValue) == 0) {
break;
}
}
if (i < lpfirst->numitems) {
snprintf(szTmp, sizeof(szTmp), "%s", "urn:ogc:def:procedure:");
pszTmp = msStringConcatenate(pszTmp, szTmp);
pszValueShape = msEncodeHTMLEntities(sShape.values[i]);
pszTmp = msStringConcatenate(pszTmp, pszValueShape);
psNode = xmlNewChild(psObsNode, NULL, BAD_CAST "procedure", NULL);
xmlNewNsProp(psNode, psNsXLink, BAD_CAST "href", BAD_CAST pszTmp);
msFree(pszTmp);
pszTmp = NULL;
msFree(pszValueShape);
}
/*else should we generate a warning !*/
if (lp != lpfirst)
msLayerClose(lpfirst);
}
} else if ((pszValue = msOWSLookupMetadata(&(lp->metadata), "S", "procedure"))) {
if (! msOWSLookupMetadata(&(lp->metadata), "S", "procedure_item"))
xmlAddSibling(psNode, xmlNewComment(BAD_CAST "WARNING: Optional metadata \"sos_procedure_item\" missing for sos:procedure. If you have more than 1 procedures, sos:procedure will output them incorrectly."));
snprintf(szTmp, sizeof(szTmp), "%s", "urn:ogc:def:procedure:");
pszTmp = msStringConcatenate(pszTmp, szTmp);
pszTmp = msStringConcatenate(pszTmp, (char *)pszValue);
psNode = xmlNewChild(psObsNode, NULL, BAD_CAST "procedure", NULL);
xmlNewNsProp(psNode, psNsXLink, BAD_CAST "href", BAD_CAST pszTmp);
msFree(pszTmp);
pszTmp = NULL;
}
/*observed property*/
pszValue = msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id");
if (pszValue)
msSOSAddPropertyNode(psNsSwe, psNsXLink, psObsNode, lp, psNsGml, pszOid);
msFree(pszOid);
pszOid = NULL;
/*TODO add featureofinterest*/
pszTmp = msStringConcatenate(pszTmp, (char *) script_url);
pszTmp = msStringConcatenate(pszTmp, "service=WFS&version=1.1.0&request=DescribeFeatureType&typename=");
pszTmp = msStringConcatenate(pszTmp, (char *) opLayerName);
psNode = xmlNewChild(psObsNode, psNsOm, BAD_CAST "featureOfInterest", NULL);
xmlNewNsProp(psNode, psNsXLink, BAD_CAST "href", BAD_CAST pszTmp);
msFree(pszTmp);
pszTmp=NULL;
/* add result : gml:featureMember of all selected elements */
psNode = xmlNewChild(psObsNode, NULL, BAD_CAST "result", NULL);
/*TODO should we add soemwhere the units of the value :
<om:result uom="units.xml#cm">29.00</om:result> */
#ifdef USE_PROJ
if(msProjectionsDiffer(&(lp->projection), &(map->projection)))
msProjectShape(&lp->projection, &lp->projection, &sShape);
#endif
psNode = xmlNewChild(psNode, psNsGml, BAD_CAST "featureMember", NULL);
/* xmlSetNs(psNode,xmlNewNs(psNode, BAD_CAST "http://www.opengis.net/gml", BAD_CAST "gml")); */
/*TODO : add namespaces like wfs " ms and a url to mapserve ? */
psLayerNode = xmlNewChild(psNode, psNsMs, BAD_CAST lp->name, NULL);
/* fetch gml:id */
pszFeatureId = msOWSLookupMetadata(&(lp->metadata), "OSG", "featureid");
if(pszFeatureId && msLayerOpen(lp) == MS_SUCCESS && msLayerGetItems(lp) == MS_SUCCESS)
xmlSetNs(psLayerNode,psNsMs);
/*bbox*/
#ifdef USE_PROJ
pszEpsg = msOWSGetEPSGProj(&(map->projection), &(lp->metadata), "SO", MS_TRUE);
if (!pszEpsg)
pszEpsg = msOWSGetEPSGProj(&(lp->projection), &(lp->metadata), "SO", MS_TRUE);
if (msProjectionsDiffer(&map->projection, &lp->projection) == MS_TRUE)
msProjectRect(&lp->projection, &map->projection, &sShape.bounds);
#endif
psNode = xmlAddChild(psLayerNode, msGML3BoundedBy(psNsGml, sShape.bounds.minx, sShape.bounds.miny, sShape.bounds.maxx, sShape.bounds.maxy, pszEpsg));
/*geometry*/
msSOSAddGeometryNode(psNsGml, psNsMs, psLayerNode, map, lp, &sShape, pszEpsg);
/*attributes */
/* TODO only output attributes where there is a sos_%s_alias (to be discussed)*/
/* the first layer is the one that has to have all the metadata defined */
lpfirst = msSOSGetFirstLayerForOffering(map,
msOWSLookupMetadata(&(lp->metadata), "S",
"offering_id"),
msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id"));
if (lpfirst && msLayerOpen(lpfirst) == MS_SUCCESS &&
msLayerGetItems(lpfirst) == MS_SUCCESS) {
for(i=0; i<lpfirst->numitems; i++) {
snprintf(szTmp, sizeof(szTmp), "%s_alias", lpfirst->items[i]);
pszValue = msOWSLookupMetadata(&(lpfirst->metadata), "S", szTmp);
if (pszValue) {
for (j=0; j<lp->numitems; j++) {
if (strcasecmp(lpfirst->items[i], lpfirst->items[j]) == 0) {
/*if there is an alias used, use it to output the
parameter name : eg "sos_AMMON_DIS_alias" "Amonia" */
snprintf(szTmp, sizeof(szTmp), "%s_alias", lpfirst->items[i]);
pszValue = msOWSLookupMetadata(&(lpfirst->metadata), "S", szTmp);
pszValueShape = msEncodeHTMLEntities(sShape.values[j]);
if (pszValue) {
pszTmp = msEncodeHTMLEntities(pszValue);
psNode = xmlNewChild(psLayerNode, psNsMs, BAD_CAST pszValue,
BAD_CAST pszValueShape);
free(pszTmp);
} else {
pszTmp = msEncodeHTMLEntities(lpfirst->items[i]);
psNode = xmlNewChild(psLayerNode, psNsMs,
BAD_CAST lpfirst->items[i],
BAD_CAST pszValueShape);
free(pszTmp);
}
free(pszValueShape);
xmlSetNs(psNode,psNsMs);
break;
}
}
}
}
if (lp->index != lpfirst->index)
msLayerClose(lpfirst);
}
}
msFreeShape(&sShape);
}
/************************************************************************/
/* msSOSReturnMemberResult */
/* */
/* Add a result string to the result node (used for */
/* GetObservation using "observation" as the response output. */
/* Assuming here that the layer is already opened. */
/************************************************************************/
char* msSOSReturnMemberResult(layerObj *lp, int iFeatureId, char **ppszProcedure)
{
char *pszFinalValue = NULL;
shapeObj sShape;
int i, j, status;
layerObj *lpfirst;
const char *pszTimeField = NULL, *pszValue=NULL, *pszProcedureField=NULL;
char *pszValueShape = NULL;
char szTmp[100];
const char *pszSep=NULL;
msInitShape(&sShape);
status = msLayerGetShape(lp, &sShape, &(lp->resultcache->results[iFeatureId]));
if(status != MS_SUCCESS)
return NULL;
pszTimeField = msOWSLookupMetadata(&(lp->metadata), "SO", "timeitem");
if (pszTimeField && sShape.values) {
for(i=0; i<lp->numitems; i++) {
if (strcasecmp(lp->items[i], pszTimeField) == 0) {
pszFinalValue = msStringConcatenate(pszFinalValue, sShape.values[i]);
break;
}
}
}
if (ppszProcedure) {
pszProcedureField = msOWSLookupMetadata(&(lp->metadata), "S", "procedure_item");
for(i=0; i<lp->numitems; i++) {
if (strcasecmp(lp->items[i], pszProcedureField) == 0) {
(*ppszProcedure) = msStrdup( sShape.values[i]);
break;
}
}
}
/* the first layer is the one that has to have all the metadata defined */
lpfirst = msSOSGetFirstLayerForOffering(lp->map,
msOWSLookupMetadata(&(lp->metadata), "S",
"offering_id"),
msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id"));
if (lp == lpfirst || (lpfirst && msLayerOpen(lpfirst) == MS_SUCCESS &&
msLayerGetItems(lpfirst) == MS_SUCCESS)) {
pszSep = msOWSLookupMetadata(&(lp->map->web.metadata), "S",
"encoding_tokenSeparator");
for(i=0; i<lpfirst->numitems; i++) {
snprintf(szTmp, sizeof(szTmp), "%s_alias", lpfirst->items[i]);
pszValue = msOWSLookupMetadata(&(lpfirst->metadata), "S", szTmp);
if (pszValue) {
for (j=0; j<lp->numitems; j++) {
if (strcasecmp(lpfirst->items[i], lpfirst->items[j]) == 0) {
pszValueShape = msEncodeHTMLEntities(sShape.values[j]);
if (pszFinalValue) {
if (pszSep)
pszFinalValue = msStringConcatenate(pszFinalValue, (char *)pszSep);
else
pszFinalValue = msStringConcatenate(pszFinalValue, ",");
}
pszFinalValue = msStringConcatenate(pszFinalValue, pszValueShape);
msFree(pszValueShape);
}
}
}
}
}
msFreeShape(&sShape);
return pszFinalValue;
}
/************************************************************************/
/* msSOSAddMemberNodeObservation */
/* */
/* Add a member node used for getObservation request using */
/* Observation as the result format. */
/************************************************************************/
xmlNodePtr msSOSAddMemberNodeObservation(xmlNsPtr psNsGml, xmlNsPtr psNsSos, xmlNsPtr psNsOm, xmlNsPtr psNsSwe, xmlNsPtr psNsXLink, xmlNodePtr psParent, mapObj *map, layerObj *lp, const char *pszProcedure)
{
char *pszTmp = NULL;
xmlNodePtr psNode=NULL, psObsNode=NULL, psMemberNode=NULL;
layerObj *lpfirst;
const char *value = NULL;
/*always featch the first layer that has the same offering id and observered propery.
This allows to only define all the attributes and components on the first layer if the user
wants to present several mapserver layers as the same offering.*/
lpfirst = msSOSGetFirstLayerForOffering(map,
msOWSLookupMetadata(&(lp->metadata), "S",
"offering_id"),
msOWSLookupMetadata(&(lp->metadata), "S",
"observedproperty_id"));
if (psParent) {
psMemberNode = xmlNewChild(psParent, NULL, BAD_CAST "member", NULL);
psObsNode = xmlNewChild(psMemberNode, NULL, BAD_CAST "Observation", NULL);
/*time*/
/* ??TODO : sampling time is a manadatory element but uses a non mandatory metadata sos_offering_timeextent */
value = msOWSLookupMetadata(&(lp->metadata), "S", "offering_timeextent");
if (value) {
char **tokens;
int n;
char *pszEndTime = NULL;
tokens = msStringSplit(value, '/', &n);
if (tokens==NULL || (n != 1 && n!=2)) {
msSetError(MS_SOSERR, "Wrong number of arguments for sos_offering_timeextent.",
"msSOSGetObservation()");
msSOSException(map, "sos_offering_timeextent", "InvalidParameterValue");
return NULL;
}
if (n == 2) /* end time is empty. It is going to be set as "now*/
pszEndTime = tokens[1];
psNode = xmlAddChild(psObsNode, msSOSAddTimeNode(psNsOm, psNsGml, tokens[0], pszEndTime));
msFreeCharArray(tokens, n);
}
/* procedure */
if (pszProcedure) { /*this should always be true since procedure is a manadtory element*/
if (! msOWSLookupMetadata(&(lp->metadata), "S", "procedure_item") && msOWSLookupMetadata(&(lp->metadata), "S", "procedure"))
xmlAddSibling(psNode, xmlNewComment(BAD_CAST "WARNING: Optional metadata \"sos_procedure_item\" missing for sos:procedure. If you have more than 1 procedures, sos:procedure will output them incorrectly."));
pszTmp = msStringConcatenate(pszTmp, "urn:ogc:def:procedure:");
pszTmp = msStringConcatenate(pszTmp, (char *)pszProcedure);
psNode = xmlNewChild(psObsNode, NULL, BAD_CAST "procedure", NULL);
/* xmlNewNsProp(psNode, xmlNewNs(NULL, BAD_CAST "http://www.w3.org/1999/xlink", BAD_CAST "xlink"), BAD_CAST "href", BAD_CAST pszTmp); */
xmlNewNsProp(psNode, psNsXLink, BAD_CAST "href", BAD_CAST pszTmp);
msFree(pszTmp);
pszTmp = NULL;
}