forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapogcsld.c
5124 lines (4442 loc) · 204 KB
/
mapogcsld.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 SLD implementation
* Author: Y. Assefa, DM Solutions Group ([email protected])
*
**********************************************************************
* Copyright (c) 2003, 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
****************************************************************************/
#include "mapogcsld.h"
#include "mapogcfilter.h"
#include "mapserver.h"
#ifdef USE_OGR
#include "cpl_string.h"
#endif
#define SLD_LINE_SYMBOL_NAME "sld_line_symbol"
#define SLD_LINE_SYMBOL_DASH_NAME "sld_line_symbol_dash"
#define SLD_MARK_SYMBOL_SQUARE "sld_mark_symbol_square"
#define SLD_MARK_SYMBOL_SQUARE_FILLED "sld_mark_symbol_square_filled"
#define SLD_MARK_SYMBOL_CIRCLE "sld_mark_symbol_circle"
#define SLD_MARK_SYMBOL_CIRCLE_FILLED "sld_mark_symbol_circle_filled"
#define SLD_MARK_SYMBOL_TRIANGLE "sld_mark_symbol_triangle"
#define SLD_MARK_SYMBOL_TRIANGLE_FILLED "sld_mark_symbol_triangle_filled"
#define SLD_MARK_SYMBOL_STAR "sld_mark_symbol_star"
#define SLD_MARK_SYMBOL_STAR_FILLED "sld_mark_symbol_star_filled"
#define SLD_MARK_SYMBOL_CROSS "sld_mark_symbol_cross"
#define SLD_MARK_SYMBOL_CROSS_FILLED "sld_mark_symbol_cross_filled"
#define SLD_MARK_SYMBOL_X "sld_mark_symbol_x"
#define SLD_MARK_SYMBOL_X_FILLED "sld_mark_symbol_x_filled"
/************************************************************************/
/* msSLDApplySLDURL */
/* */
/* Use the SLD document given through a URL and apply the SLD */
/* on the map. Layer name and Named Layer's name parameter are */
/* used to do the match. */
/************************************************************************/
int msSLDApplySLDURL(mapObj *map, char *szURL, int iLayer,
char *pszStyleLayerName, char **ppszLayerNames)
{
#ifdef USE_OGR
/* needed for libcurl function msHTTPGetFile in maphttp.c */
#if defined(USE_CURL)
char *pszSLDTmpFile = NULL;
int status = 0;
char *pszSLDbuf=NULL;
FILE *fp = NULL;
int nStatus = MS_FAILURE;
if (map && szURL) {
pszSLDTmpFile = msTmpFile(map, map->mappath, NULL, "sld.xml");
if (pszSLDTmpFile == NULL) {
pszSLDTmpFile = msTmpFile(map, NULL, NULL, "sld.xml" );
}
if (msHTTPGetFile(szURL, pszSLDTmpFile, &status,-1, 0, 0) == MS_SUCCESS) {
if ((fp = fopen(pszSLDTmpFile, "rb")) != NULL) {
int nBufsize=0;
fseek(fp, 0, SEEK_END);
nBufsize = ftell(fp);
rewind(fp);
pszSLDbuf = (char*)malloc((nBufsize+1)*sizeof(char));
fread(pszSLDbuf, 1, nBufsize, fp);
fclose(fp);
pszSLDbuf[nBufsize] = '\0';
unlink(pszSLDTmpFile);
}
} else {
msSetError(MS_WMSERR, "Could not open SLD %s and save it in temporary file %s. Please make sure that the sld url is valid and that the temporary path is set. The temporary path can be defined for example by setting TMPPATH in the map file. Please check the MapServer documentation on temporary path settings.", "msSLDApplySLDURL", szURL, pszSLDTmpFile);
}
if (pszSLDbuf)
nStatus = msSLDApplySLD(map, pszSLDbuf, iLayer, pszStyleLayerName, ppszLayerNames);
}
return nStatus;
#else
msSetError(MS_MISCERR, "WMS/WFS client support is not enabled .", "msSLDApplySLDURL()");
return(MS_FAILURE);
#endif
#else
/* ------------------------------------------------------------------
* OGR Support not included...
* ------------------------------------------------------------------ */
msSetError(MS_MISCERR, "OGR support is not available.", "msSLDApplySLDURL()");
return(MS_FAILURE);
#endif /* USE_OGR */
}
/************************************************************************/
/* msSLDApplySLD */
/* */
/* Parses the SLD into array of layers. Go through the map and */
/* compare the SLD layers and the map layers using the name. If */
/* they have the same name, copy the classes asscoaited with */
/* the SLD layers onto the map layers. */
/************************************************************************/
int msSLDApplySLD(mapObj *map, char *psSLDXML, int iLayer,
char *pszStyleLayerName, char **ppszLayerNames)
{
#if defined(USE_WMS_SVR) || defined (USE_WFS_SVR) || defined (USE_WCS_SVR) || defined(USE_SOS_SVR)
#ifdef USE_OGR
int nLayers = 0;
layerObj *pasLayers = NULL;
int i, j, k, z, iClass;
int bUseSpecificLayer = 0;
const char *pszTmp = NULL;
int bFreeTemplate = 0;
int nLayerStatus = 0;
int nStatus = MS_SUCCESS;
/*const char *pszSLDNotSupported = NULL;*/
char *tmpfilename = NULL;
const char *pszFullName = NULL;
char szTmp[512];
char *pszTmp1=NULL;
char *pszTmp2 = NULL;
char *pszBuffer = NULL;
layerObj *lp = NULL;
char *pszSqlExpression=NULL;
FilterEncodingNode *psExpressionNode =NULL;
int bFailedExpression=0;
pasLayers = msSLDParseSLD(map, psSLDXML, &nLayers);
/* -------------------------------------------------------------------- */
/* If the same layer is given more that once, we need to */
/* duplicate it. */
/* -------------------------------------------------------------------- */
if (pasLayers && nLayers>0) {
int l,m;
for (m=0; m<nLayers; m++) {
layerObj *psTmpLayer=NULL;
int nIndex;
char tmpId[128];
for (l=0; l<nLayers; l++) {
if(pasLayers[m].name == NULL || pasLayers[l].name == NULL)
continue;
nIndex = msGetLayerIndex(map, pasLayers[m].name);
if (m !=l && strcasecmp(pasLayers[m].name, pasLayers[l].name)== 0 &&
nIndex != -1) {
psTmpLayer = (layerObj *) malloc(sizeof(layerObj));
initLayer(psTmpLayer, map);
msCopyLayer(psTmpLayer, GET_LAYER(map,nIndex));
/* open the source layer */
if ( !psTmpLayer->vtable)
msInitializeVirtualTable(psTmpLayer);
/*make the name unique*/
snprintf(tmpId, sizeof(tmpId), "%lx_%x_%d",(long)time(NULL),(int)getpid(),
map->numlayers);
if (psTmpLayer->name)
msFree(psTmpLayer->name);
psTmpLayer->name = strdup(tmpId);
msFree(pasLayers[l].name);
pasLayers[l].name = strdup(tmpId);
msInsertLayer(map, psTmpLayer, -1);
MS_REFCNT_DECR(psTmpLayer);
}
}
}
}
if (pasLayers && nLayers > 0) {
for (i=0; i<map->numlayers; i++) {
if (iLayer >=0 && iLayer< map->numlayers) {
i = iLayer;
bUseSpecificLayer = 1;
}
/* compare layer name to wms_name as well */
pszTmp = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "MO", "name");
for (j=0; j<nLayers; j++) {
/* -------------------------------------------------------------------- */
/* copy : - class */
/* - layer's labelitem */
/* -------------------------------------------------------------------- */
if ((pasLayers[j].name && pszStyleLayerName == NULL &&
((strcasecmp(GET_LAYER(map, i)->name, pasLayers[j].name) == 0 ||
(pszTmp && strcasecmp(pszTmp, pasLayers[j].name) == 0))||
(GET_LAYER(map, i)->group &&
strcasecmp(GET_LAYER(map, i)->group, pasLayers[j].name) == 0))) ||
(bUseSpecificLayer && pszStyleLayerName && pasLayers[j].name &&
strcasecmp(pasLayers[j].name, pszStyleLayerName) == 0)) {
#ifdef notdef
/*this is a test code if we decide to flag some layers as not supporting SLD*/
pszSLDNotSupported = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "M", "SLD_NOT_SUPPORTED");
if (pszSLDNotSupported) {
msSetError(MS_WMSERR, "Layer %s does not support SLD", "msSLDApplySLD", pasLayers[j].name);
return MS_FAILURE;
}
#endif
if ( pasLayers[j].numclasses > 0) {
GET_LAYER(map, i)->type = pasLayers[j].type;
for(k=0; k<GET_LAYER(map, i)->numclasses; k++) {
if (GET_LAYER(map, i)->class[k] != NULL) {
GET_LAYER(map, i)->class[k]->layer=NULL;
if (freeClass(GET_LAYER(map, i)->class[k]) == MS_SUCCESS ) {
msFree(GET_LAYER(map, i)->class[k]);
GET_LAYER(map, i)->class[k] = NULL;
}
}
}
GET_LAYER(map, i)->numclasses = 0;
/*unset the classgroup on the layer if it was set. This allows the layer to render
with all the classes defined in the SLD*/
msFree(GET_LAYER(map, i)->classgroup);
GET_LAYER(map, i)->classgroup = NULL;
iClass = 0;
for (k=0; k < pasLayers[j].numclasses; k++) {
if (msGrowLayerClasses(GET_LAYER(map, i)) == NULL)
return MS_FAILURE;
initClass(GET_LAYER(map, i)->class[iClass]);
msCopyClass(GET_LAYER(map, i)->class[iClass],
pasLayers[j].class[k], NULL);
GET_LAYER(map, i)->class[iClass]->layer = GET_LAYER(map, i);
GET_LAYER(map, i)->class[iClass]->type = GET_LAYER(map, i)->type;
GET_LAYER(map, i)->numclasses++;
/*aliases may have been used as part of the sld text symbolizer for
label element. Try to process it if that is the case #3114*/
if (msLayerOpen(GET_LAYER(map, i)) == MS_SUCCESS &&
msLayerGetItems(GET_LAYER(map, i)) == MS_SUCCESS) {
if (GET_LAYER(map, i)->class[iClass]->text.string) {
for(z=0; z<GET_LAYER(map, i)->numitems; z++) {
if (!GET_LAYER(map, i)->items[z] || strlen(GET_LAYER(map, i)->items[z]) <= 0)
continue;
snprintf(szTmp, sizeof(szTmp), "%s_alias", GET_LAYER(map, i)->items[z]);
pszFullName = msOWSLookupMetadata(&(GET_LAYER(map, i)->metadata), "G", szTmp);
pszTmp1 = msStrdup( GET_LAYER(map, i)->class[iClass]->text.string);
if (pszFullName != NULL && (strstr(pszTmp1, pszFullName) != NULL)) {
char *tmpstr1= NULL;
tmpstr1 = msReplaceSubstring(pszTmp1, pszFullName, GET_LAYER(map, i)->items[z]);
pszTmp2 = (char *)malloc(sizeof(char)*(strlen(tmpstr1)+3));
sprintf(pszTmp2,"(%s)",tmpstr1);
msLoadExpressionString(&(GET_LAYER(map, i)->class[iClass]->text), pszTmp2);
msFree(pszTmp2);
}
msFree(pszTmp1);
}
}
}
iClass++;
}
} else {
/*this is probably an SLD that uses Named styles*/
if (pasLayers[j].classgroup) {
for (k=0; k<GET_LAYER(map, i)->numclasses; k++) {
if (GET_LAYER(map, i)->class[k]->group &&
strcasecmp(GET_LAYER(map, i)->class[k]->group,
pasLayers[j].classgroup) == 0)
break;
}
if (k < GET_LAYER(map, i)->numclasses) {
msFree( GET_LAYER(map, i)->classgroup);
GET_LAYER(map, i)->classgroup = msStrdup(pasLayers[j].classgroup);
} else {
/* TODO we throw an exception ?*/
}
}
}
if (pasLayers[j].labelitem) {
if (GET_LAYER(map, i)->labelitem)
free(GET_LAYER(map, i)->labelitem);
GET_LAYER(map, i)->labelitem = msStrdup(pasLayers[j].labelitem);
}
if (pasLayers[j].classitem) {
if (GET_LAYER(map, i)->classitem)
free(GET_LAYER(map, i)->classitem);
GET_LAYER(map, i)->classitem = msStrdup(pasLayers[j].classitem);
}
/* opacity for sld raster */
if (GET_LAYER(map, i)->type == MS_LAYER_RASTER &&
pasLayers[j].opacity != -1)
GET_LAYER(map, i)->opacity = pasLayers[j].opacity;
/* mark as auto-generate SLD */
if (GET_LAYER(map, i)->connectiontype == MS_WMS)
msInsertHashTable(&(GET_LAYER(map, i)->metadata),
"wms_sld_body", "auto" );
/* ==================================================================== */
/* if the SLD contained a spatial feature, the layerinfo */
/* parameter contains the node. Extract it and do a query on */
/* the layer. Insert also a metadata that will be used when */
/* rendering the final image. */
/* ==================================================================== */
if (pasLayers[j].layerinfo &&
(GET_LAYER(map, i)->type == MS_LAYER_POINT ||
GET_LAYER(map, i)->type == MS_LAYER_LINE ||
GET_LAYER(map, i)->type == MS_LAYER_POLYGON ||
GET_LAYER(map, i)->type == MS_LAYER_ANNOTATION ||
GET_LAYER(map, i)->type == MS_LAYER_TILEINDEX)) {
FilterEncodingNode *psNode = NULL;
msInsertHashTable(&(GET_LAYER(map, i)->metadata),
"tmp_wms_sld_query", "true" );
psNode = (FilterEncodingNode *)pasLayers[j].layerinfo;
/* -------------------------------------------------------------------- */
/* set the template on the classes so that the query works */
/* using classes. If there are no classes, set it at the layer level.*/
/* -------------------------------------------------------------------- */
if (GET_LAYER(map, i)->numclasses > 0) {
for (k=0; k<GET_LAYER(map, i)->numclasses; k++) {
if (!GET_LAYER(map, i)->class[k]->template)
GET_LAYER(map, i)->class[k]->template = msStrdup("ttt.html");
}
} else if (!GET_LAYER(map, i)->template) {
bFreeTemplate = 1;
GET_LAYER(map, i)->template = msStrdup("ttt.html");
}
nLayerStatus = GET_LAYER(map, i)->status;
GET_LAYER(map, i)->status = MS_ON;
nStatus =
FLTApplyFilterToLayer(psNode, map,
GET_LAYER(map, i)->index);
/* -------------------------------------------------------------------- */
/* nothing found is a valid, do not exit. */
/* -------------------------------------------------------------------- */
if (nStatus != MS_SUCCESS) {
errorObj *ms_error;
ms_error = msGetErrorObj();
if(ms_error->code == MS_NOTFOUND)
nStatus = MS_SUCCESS;
}
GET_LAYER(map, i)->status = nLayerStatus;
FLTFreeFilterEncodingNode(psNode);
if ( bFreeTemplate) {
free(GET_LAYER(map, i)->template);
GET_LAYER(map, i)->template = NULL;
}
pasLayers[j].layerinfo=NULL;
if( nStatus != MS_SUCCESS )
return nStatus;
} else {
/*in some cases it would make sense to concatenate all the class
expressions and use it to set the filter on the layer. This
could increase performace. Will do it for db types layers #2840*/
lp = GET_LAYER(map, i);
if (lp->filter.string == NULL ||
(lp->filter.string && lp->filter.type == MS_EXPRESSION)) {
if (lp->connectiontype == MS_POSTGIS || lp->connectiontype == MS_ORACLESPATIAL ||
lp->connectiontype == MS_SDE || lp->connectiontype == MS_PLUGIN) {
if (lp->numclasses > 0) {
/*check first that all classes have an expression type. That is
the only way we can concatenate them and set the filter
expression*/
for (k=0; k<lp->numclasses; k++) {
if (lp->class[k]->expression.type != MS_EXPRESSION)
break;
}
if (k == lp->numclasses) {
bFailedExpression = 0;
for (k=0; k<lp->numclasses; k++) {
if (pszBuffer == NULL)
snprintf(szTmp, sizeof(szTmp), "%s", "((");
else
snprintf(szTmp, sizeof(szTmp), "%s", " OR ");
pszBuffer =msStringConcatenate(pszBuffer, szTmp);
psExpressionNode = BuildExpressionTree(lp->class[k]->expression.string,NULL);
if (psExpressionNode) {
pszSqlExpression = FLTGetSQLExpression(psExpressionNode,lp);
if (pszSqlExpression) {
pszBuffer =
msStringConcatenate(pszBuffer, pszSqlExpression);
msFree(pszSqlExpression);
} else {
bFailedExpression =1;
break;
}
FLTFreeFilterEncodingNode(psExpressionNode);
} else {
bFailedExpression =1;
break;
}
}
if (!bFailedExpression) {
snprintf(szTmp, sizeof(szTmp), "%s", "))");
pszBuffer =msStringConcatenate(pszBuffer, szTmp);
msLoadExpressionString(&lp->filter, pszBuffer);
}
msFree(pszBuffer);
pszBuffer = NULL;
}
}
}
}
}
break;
}
}
if (bUseSpecificLayer)
break;
}
/* -------------------------------------------------------------------- */
/* if needed return a comma separated list of the layers found */
/* in the sld. */
/* -------------------------------------------------------------------- */
if (ppszLayerNames) {
char *pszTmp = NULL;
for (i=0; i<nLayers; i++) {
if (pasLayers[i].name) {
if (pszTmp !=NULL)
pszTmp = msStringConcatenate(pszTmp, ",");
pszTmp = msStringConcatenate(pszTmp, pasLayers[i].name);
}
}
*ppszLayerNames = pszTmp;
}
for (i=0; i<nLayers; i++)
freeLayer(&pasLayers[i]);
msFree(pasLayers);
}
if(map->debug == MS_DEBUGLEVEL_VVV) {
tmpfilename = msTmpFile(map, map->mappath, NULL, "_sld.map");
if (tmpfilename == NULL) {
tmpfilename = msTmpFile(map, NULL, NULL, "_sld.map" );
}
if (tmpfilename) {
msSaveMap(map,tmpfilename);
msDebug("msApplySLD(): Map file after SLD was applied %s", tmpfilename);
msFree(tmpfilename);
}
}
return MS_SUCCESS;
#else
/* ------------------------------------------------------------------
* OGR Support not included...
* ------------------------------------------------------------------ */
msSetError(MS_MISCERR, "OGR support is not available.", "msSLDApplySLD()");
return(MS_FAILURE);
#endif /* USE_OGR */
#else
msSetError(MS_MISCERR, "OWS support is not available.",
"msSLDApplySLD()");
return(MS_FAILURE);
#endif
}
#ifdef USE_OGR
/************************************************************************/
/* msSLDParseSLD */
/* */
/* Parse the sld document into layers : for each named layer */
/* there is one mapserver layer created with approproate */
/* classes and styles. */
/* Returns an array of mapserver layers. The pnLayres if */
/* provided will indicate the size of the returned array. */
/************************************************************************/
layerObj *msSLDParseSLD(mapObj *map, char *psSLDXML, int *pnLayers)
{
CPLXMLNode *psRoot = NULL;
CPLXMLNode *psSLD, *psNamedLayer, *psChild, *psName;
layerObj *pasLayers = NULL;
int iLayer = 0;
int nLayers = 0;
if (map == NULL || psSLDXML == NULL || strlen(psSLDXML) <= 0 ||
(strstr(psSLDXML, "StyledLayerDescriptor") == NULL)) {
msSetError(MS_WMSERR, "Invalid SLD document", "");
return NULL;
}
psRoot = CPLParseXMLString(psSLDXML);
if( psRoot == NULL) {
msSetError(MS_WMSERR, "Invalid SLD document : %s", "", psSLDXML);
return NULL;
}
/* strip namespaces ogc and sld and gml */
CPLStripXMLNamespace(psRoot, "ogc", 1);
CPLStripXMLNamespace(psRoot, "sld", 1);
CPLStripXMLNamespace(psRoot, "gml", 1);
CPLStripXMLNamespace(psRoot, "se", 1);
/* -------------------------------------------------------------------- */
/* get the root element (Filter). */
/* -------------------------------------------------------------------- */
psChild = psRoot;
psSLD = NULL;
while( psChild != NULL ) {
if (psChild->eType == CXT_Element &&
EQUAL(psChild->pszValue,"StyledLayerDescriptor")) {
psSLD = psChild;
break;
} else
psChild = psChild->psNext;
}
if (!psSLD) {
msSetError(MS_WMSERR, "Invalid SLD document : %s", "", psSLDXML);
return NULL;
}
/* -------------------------------------------------------------------- */
/* Parse the named layers. */
/* -------------------------------------------------------------------- */
psNamedLayer = CPLGetXMLNode(psSLD, "NamedLayer");
while (psNamedLayer) {
if (!psNamedLayer->pszValue ||
strcasecmp(psNamedLayer->pszValue, "NamedLayer") != 0) {
psNamedLayer = psNamedLayer->psNext;
continue;
}
psNamedLayer = psNamedLayer->psNext;
nLayers++;
}
if (nLayers > 0)
pasLayers = (layerObj *)malloc(sizeof(layerObj)*nLayers);
else
return NULL;
psNamedLayer = CPLGetXMLNode(psSLD, "NamedLayer");
if (psNamedLayer) {
iLayer = 0;
while (psNamedLayer)
{
if (!psNamedLayer->pszValue ||
strcasecmp(psNamedLayer->pszValue, "NamedLayer") != 0) {
psNamedLayer = psNamedLayer->psNext;
continue;
}
psName = CPLGetXMLNode(psNamedLayer, "Name");
initLayer(&pasLayers[iLayer], map);
if (psName && psName->psChild && psName->psChild->pszValue)
pasLayers[iLayer].name = msStrdup(psName->psChild->pszValue);
msSLDParseNamedLayer(psNamedLayer, &pasLayers[iLayer]);
psNamedLayer = psNamedLayer->psNext;
iLayer++;
}
}
if (pnLayers)
*pnLayers = nLayers;
if (psRoot)
CPLDestroyXMLNode(psRoot);
return pasLayers;
}
int _msSLDParseSizeParameter(CPLXMLNode *psSize)
{
int nSize = 0;
CPLXMLNode *psLiteral = NULL;
if (psSize) {
psLiteral = CPLGetXMLNode(psSize, "Literal");
if (psLiteral && psLiteral->psChild && psLiteral->psChild->pszValue)
nSize = atof(psLiteral->psChild->pszValue);
else if (psSize->psChild && psSize->psChild->pszValue)
nSize = atof(psSize->psChild->pszValue);
}
return nSize;
}
/************************************************************************/
/* _SLDApplyRuleValues */
/* */
/* Utility function to set the scale, title/name for the */
/* classes created by a Rule. */
/************************************************************************/
void _SLDApplyRuleValues(CPLXMLNode *psRule, layerObj *psLayer,
int nNewClasses)
{
int i=0;
CPLXMLNode *psMinScale=NULL, *psMaxScale=NULL;
CPLXMLNode *psName=NULL, *psTitle=NULL;
double dfMinScale=0, dfMaxScale=0;
char *pszName=NULL, *pszTitle=NULL;
if (psRule && psLayer && nNewClasses > 0) {
/* -------------------------------------------------------------------- */
/* parse minscale and maxscale. */
/* -------------------------------------------------------------------- */
psMinScale = CPLGetXMLNode(psRule,
"MinScaleDenominator");
if (psMinScale && psMinScale->psChild &&
psMinScale->psChild->pszValue)
dfMinScale = atof(psMinScale->psChild->pszValue);
psMaxScale = CPLGetXMLNode(psRule,
"MaxScaleDenominator");
if (psMaxScale && psMaxScale->psChild &&
psMaxScale->psChild->pszValue)
dfMaxScale = atof(psMaxScale->psChild->pszValue);
/* -------------------------------------------------------------------- */
/* parse name and title. */
/* -------------------------------------------------------------------- */
psName = CPLGetXMLNode(psRule, "Name");
if (psName && psName->psChild &&
psName->psChild->pszValue)
pszName = psName->psChild->pszValue;
psTitle = CPLGetXMLNode(psRule, "Title");
if (psTitle && psTitle->psChild &&
psTitle->psChild->pszValue)
pszTitle = psTitle->psChild->pszValue;
/* -------------------------------------------------------------------- */
/* set the scale to all the classes created by the rule. */
/* -------------------------------------------------------------------- */
if (dfMinScale > 0 || dfMaxScale > 0) {
for (i=0; i<nNewClasses; i++) {
if (dfMinScale > 0)
psLayer->class[psLayer->numclasses-1-i]->minscaledenom = dfMinScale;
if (dfMaxScale)
psLayer->class[psLayer->numclasses-1-i]->maxscaledenom = dfMaxScale;
}
}
/* -------------------------------------------------------------------- */
/* set name and title to the classes created by the rule. */
/* -------------------------------------------------------------------- */
for (i=0; i<nNewClasses; i++) {
if (!psLayer->class[psLayer->numclasses-1-i]->name) {
if (pszName)
psLayer->class[psLayer->numclasses-1-i]->name = msStrdup(pszName);
else if (pszTitle)
psLayer->class[psLayer->numclasses-1-i]->name = msStrdup(pszTitle);
else
psLayer->class[psLayer->numclasses-1-i]->name = msStrdup("Unknown");
}
}
if (pszTitle) {
for (i=0; i<nNewClasses; i++) {
psLayer->class[psLayer->numclasses-1-i]->title =
msStrdup(pszTitle);
}
}
}
}
/************************************************************************/
/* msSLDParseNamedLayer */
/* */
/* Parse NamedLayer root. */
/************************************************************************/
int msSLDParseNamedLayer(CPLXMLNode *psRoot, layerObj *psLayer)
{
CPLXMLNode *psFeatureTypeStyle, *psRule, *psUserStyle;
CPLXMLNode *psSLDName = NULL, *psNamedStyle=NULL;
CPLXMLNode *psElseFilter = NULL, *psFilter=NULL;
CPLXMLNode *psTmpNode = NULL;
FilterEncodingNode *psNode = NULL;
int nNewClasses=0, nClassBeforeFilter=0, nClassAfterFilter=0;
int nClassAfterRule=0, nClassBeforeRule=0;
char *pszTmpFilter = NULL;
layerObj *psCurrentLayer = NULL;
const char *pszWmsName=NULL;
int j=0;
const char *key=NULL;
if (!psRoot || !psLayer)
return MS_FAILURE;
psUserStyle = CPLGetXMLNode(psRoot, "UserStyle");
if (psUserStyle) {
psFeatureTypeStyle = CPLGetXMLNode(psUserStyle, "FeatureTypeStyle");
if (psFeatureTypeStyle) {
while (psFeatureTypeStyle && psFeatureTypeStyle->pszValue &&
strcasecmp(psFeatureTypeStyle->pszValue,
"FeatureTypeStyle") == 0) {
if (!psFeatureTypeStyle->pszValue ||
strcasecmp(psFeatureTypeStyle->pszValue,
"FeatureTypeStyle") != 0) {
psFeatureTypeStyle = psFeatureTypeStyle->psNext;
continue;
}
/* -------------------------------------------------------------------- */
/* Parse rules with no Else filter. */
/* -------------------------------------------------------------------- */
psRule = CPLGetXMLNode(psFeatureTypeStyle, "Rule");
while (psRule) {
if (!psRule->pszValue ||
strcasecmp(psRule->pszValue, "Rule") != 0) {
psRule = psRule->psNext;
continue;
}
/* used for scale setting */
nClassBeforeRule = psLayer->numclasses;
psElseFilter = CPLGetXMLNode(psRule, "ElseFilter");
nClassBeforeFilter = psLayer->numclasses;
if (psElseFilter == NULL)
msSLDParseRule(psRule, psLayer);
nClassAfterFilter = psLayer->numclasses;
/* -------------------------------------------------------------------- */
/* Parse the filter and apply it to the latest class created by */
/* the rule. */
/* NOTE : Spatial Filter is not supported. */
/* -------------------------------------------------------------------- */
psFilter = CPLGetXMLNode(psRule, "Filter");
if (psFilter && psFilter->psChild &&
psFilter->psChild->pszValue) {
CPLXMLNode *psTmpNextNode = NULL;
/* clone the tree and set the next node to null */
/* so we only have the Filter node */
psTmpNode = CPLCloneXMLTree(psFilter);
psTmpNextNode = psTmpNode->psNext;
psTmpNode->psNext = NULL;
pszTmpFilter = CPLSerializeXMLTree(psTmpNode);
psTmpNode->psNext = psTmpNextNode;
CPLDestroyXMLNode(psTmpNode);
if (pszTmpFilter) {
/* nTmp = strlen(psFilter->psChild->pszValue)+17; */
/* pszTmpFilter = malloc(sizeof(char)*nTmp); */
/* sprintf(pszTmpFilter,"<Filter>%s</Filter>", */
/* psFilter->psChild->pszValue); */
/* pszTmpFilter[nTmp-1]='\0'; */
psNode = FLTParseFilterEncoding(pszTmpFilter);
CPLFree(pszTmpFilter);
}
if (psNode) {
char *pszExpression = NULL;
int i;
/*preparse the filter for possible gml aliases set on the layer's metada:
"gml_NA3DESC_alias" "alias_name" and filter could be
<ogc:PropertyName>alias_name</ogc:PropertyName> #3079*/
for (j=0; j<psLayer->map->numlayers; j++) {
psCurrentLayer = GET_LAYER(psLayer->map, j);
pszWmsName = msOWSLookupMetadata(&(psCurrentLayer->metadata), "MO", "name");
if ((psCurrentLayer->name && psLayer->name &&
strcasecmp(psCurrentLayer->name, psLayer->name) == 0) ||
(psCurrentLayer->group && psLayer->name &&
strcasecmp(psCurrentLayer->group, psLayer->name) == 0) ||
(psLayer->name && pszWmsName &&
strcasecmp(pszWmsName, psLayer->name) == 0))
break;
}
if (j < psLayer->map->numlayers) {
/*make sure that the tmp layer has all the metadata that
the orinal layer has, allowing to do parsing for
such things as gml_attribute_type #3052*/
while (1) {
key = msNextKeyFromHashTable(&psCurrentLayer->metadata, key);
if (!key)
break;
else
msInsertHashTable(&psLayer->metadata, key,
msLookupHashTable(&psCurrentLayer->metadata, key));
}
FLTPreParseFilterForAlias(psNode, psLayer->map, j, "G");
}
pszExpression = FLTGetCommonExpression(psNode, psLayer);
FLTFreeFilterEncodingNode(psNode);
psNode = NULL;
if (pszExpression) {
nNewClasses =
nClassAfterFilter - nClassBeforeFilter;
for (i=0; i<nNewClasses; i++) {
msLoadExpressionString(&psLayer->
class[psLayer->numclasses-1-i]->
expression, pszExpression);
}
msFree(pszExpression);
pszExpression = NULL;
}
}
}
nClassAfterRule = psLayer->numclasses;
nNewClasses = nClassAfterRule - nClassBeforeRule;
/* apply scale and title to newly created classes */
_SLDApplyRuleValues(psRule, psLayer, nNewClasses);
/* TODO : parse legendgraphic */
psRule = psRule->psNext;
}
/* -------------------------------------------------------------------- */
/* First parse rules with the else filter. These rules will */
/* create the classes that are placed at the end of class */
/* list. (See how classes are applied to layers in function */
/* msSLDApplySLD). */
/* -------------------------------------------------------------------- */
psRule = CPLGetXMLNode(psFeatureTypeStyle, "Rule");
while (psRule) {
if (!psRule->pszValue ||
strcasecmp(psRule->pszValue, "Rule") != 0) {
psRule = psRule->psNext;
continue;
}
psElseFilter = CPLGetXMLNode(psRule, "ElseFilter");
if (psElseFilter) {
msSLDParseRule(psRule, psLayer);
_SLDApplyRuleValues(psRule, psLayer, 1);
}
psRule = psRule->psNext;
}
psFeatureTypeStyle = psFeatureTypeStyle->psNext;
}
}
}
/* check for Named styles*/
else {
psNamedStyle = CPLGetXMLNode(psRoot, "NamedStyle");
if (psNamedStyle) {
psSLDName = CPLGetXMLNode(psNamedStyle, "Name");
if (psSLDName && psSLDName->psChild && psSLDName->psChild->pszValue) {
msFree(psLayer->classgroup);
psLayer->classgroup = msStrdup(psSLDName->psChild->pszValue);
}
}
}
return MS_SUCCESS;
}
/************************************************************************/
/* void msSLDParseRule(CPLXMLNode *psRoot, layerObj *psLayer) */
/* */
/* Parse a Rule node into classes for a spcific layer. */
/************************************************************************/
int msSLDParseRule(CPLXMLNode *psRoot, layerObj *psLayer)
{
CPLXMLNode *psLineSymbolizer = NULL;
CPLXMLNode *psPolygonSymbolizer = NULL;
CPLXMLNode *psPointSymbolizer = NULL;
CPLXMLNode *psTextSymbolizer = NULL;
CPLXMLNode *psRasterSymbolizer = NULL;
int bSymbolizer = 0;
int bNewClass=0, nSymbolizer=0;
if (!psRoot || !psLayer)
return MS_FAILURE;
/* TODO : parse name of the rule */
/* -------------------------------------------------------------------- */
/* The SLD specs assumes here that a certain FeatureType can only have*/
/* rules for only one type of symbolizer. */
/* -------------------------------------------------------------------- */
/* ==================================================================== */
/* For each rule a new class is created. If there are more than */
/* one symbolizer of the same type, a style is added in the */
/* same class. */
/* ==================================================================== */
nSymbolizer =0;
/* line symbolizer */
psLineSymbolizer = CPLGetXMLNode(psRoot, "LineSymbolizer");
while (psLineSymbolizer) {
if (!psLineSymbolizer->pszValue ||
strcasecmp(psLineSymbolizer->pszValue,
"LineSymbolizer") != 0) {
psLineSymbolizer = psLineSymbolizer->psNext;
continue;
}
bSymbolizer = 1;
if (nSymbolizer == 0)
bNewClass = 1;
else
bNewClass = 0;
msSLDParseLineSymbolizer(psLineSymbolizer, psLayer, bNewClass);
psLineSymbolizer = psLineSymbolizer->psNext;
psLayer->type = MS_LAYER_LINE;
nSymbolizer++;
}
/* Polygon symbolizer */
psPolygonSymbolizer = CPLGetXMLNode(psRoot, "PolygonSymbolizer");
while (psPolygonSymbolizer) {
if (!psPolygonSymbolizer->pszValue ||
strcasecmp(psPolygonSymbolizer->pszValue,
"PolygonSymbolizer") != 0) {
psPolygonSymbolizer = psPolygonSymbolizer->psNext;
continue;
}
bSymbolizer = 1;
if (nSymbolizer == 0)
bNewClass = 1;
else
bNewClass = 0;
msSLDParsePolygonSymbolizer(psPolygonSymbolizer, psLayer,
bNewClass);
psPolygonSymbolizer = psPolygonSymbolizer->psNext;
psLayer->type = MS_LAYER_POLYGON;
nSymbolizer++;
}
/* Point Symbolizer */
psPointSymbolizer = CPLGetXMLNode(psRoot, "PointSymbolizer");
while (psPointSymbolizer) {
if (!psPointSymbolizer->pszValue ||
strcasecmp(psPointSymbolizer->pszValue,
"PointSymbolizer") != 0) {
psPointSymbolizer = psPointSymbolizer->psNext;
continue;
}
bSymbolizer = 1;
if (nSymbolizer == 0)
bNewClass = 1;
else
bNewClass = 0;
msSLDParsePointSymbolizer(psPointSymbolizer, psLayer, bNewClass);
psPointSymbolizer = psPointSymbolizer->psNext;
psLayer->type = MS_LAYER_POINT;
nSymbolizer++;
}
/* Text symbolizer */
/* ==================================================================== */
/* For text symbolizer, here is how it is translated into */
/* mapserver classes : */
/* - If there are other symbolizers(line, polygon, symbol), */
/* the label object created will be created in the same class */