forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 4
/
maplayer.c
2411 lines (2057 loc) · 75.5 KB
/
maplayer.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: Implementation of most layerObj functions.
* Author: Steve Lime and the MapServer team.
*
******************************************************************************
* Copyright (c) 1996-2005 Regents of the University of Minnesota.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
****************************************************************************/
#include "mapserver.h"
#include "maptime.h"
#include "mapogcfilter.h"
#include "mapthread.h"
#include "mapfile.h"
#include "mapparser.h"
#include <assert.h>
static int populateVirtualTable(layerVTableObj *vtable);
/*
** Iteminfo is a layer parameter that holds information necessary to retrieve an individual item for
** a particular source. It is an array built from a list of items. The type of iteminfo will vary by
** source. For shapefiles and OGR it is simply an array of integers where each value is an index for
** the item. For SDE it's a ESRI specific type that contains index and column type information. Two
** helper functions below initialize and free that structure member which is used locally by layer
** specific functions.
*/
int msLayerInitItemInfo(layerObj *layer)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerInitItemInfo(layer);
}
void msLayerFreeItemInfo(layerObj *layer)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return;
}
layer->vtable->LayerFreeItemInfo(layer);
/*
* Layer expressions with attribute binding hold a numeric index pointing
* to an iteminfo (node->tokenval.bindval.index). If iteminfo changes,
* an expression may be no longer valid. (#5161)
*/
msLayerFreeExpressions(layer);
}
int msLayerRestoreFromScaletokens(layerObj *layer)
{
if(!layer->scaletokens || !layer->orig_st) {
return MS_SUCCESS;
}
if(layer->orig_st->data) {
msFree(layer->data);
layer->data = layer->orig_st->data;
}
if(layer->orig_st->tileindex) {
msFree(layer->tileindex);
layer->tileindex = layer->orig_st->tileindex;
}
if(layer->orig_st->tileitem) {
msFree(layer->tileitem);
layer->tileitem = layer->orig_st->tileitem;
}
if(layer->orig_st->filter) {
msLoadExpressionString(&(layer->filter),layer->orig_st->filter);
msFree(layer->orig_st->filter);
}
if(layer->orig_st->filteritem) {
msFree(layer->filteritem);
layer->filteritem = layer->orig_st->filteritem;
}
if(layer->orig_st->n_processing) {
int i;
for(i=0;i<layer->orig_st->n_processing;i++) {
msFree(layer->processing[layer->orig_st->processing_idx[i]]);
layer->processing[layer->orig_st->processing_idx[i]] = layer->orig_st->processing[i];
}
msFree(layer->orig_st->processing);
msFree(layer->orig_st->processing_idx);
}
msFree(layer->orig_st);
layer->orig_st = NULL;
return MS_SUCCESS;
}
#define check_st_alloc(l) if(!l->orig_st) l->orig_st=msSmallCalloc(1,sizeof(originalScaleTokenStrings));
int msLayerApplyScaletokens(layerObj *layer, double scale)
{
int i,p;
if(!layer->scaletokens) {
return MS_SUCCESS;
}
msLayerRestoreFromScaletokens(layer);
for(i=0;i<layer->numscaletokens;i++) {
scaleTokenObj *st = &layer->scaletokens[i];
scaleTokenEntryObj *ste = NULL;
if(scale<=0) {
ste = &(st->tokens[0]);
/* no scale defined, use first entry */
} else {
int tokenindex=0;
while(tokenindex<st->n_entries) {
ste = &(st->tokens[tokenindex]);
if(scale < ste->maxscale && scale >= ste->minscale) break; /* current token is the correct one */
tokenindex++;
ste = NULL;
}
}
assert(ste);
if(layer->data && strstr(layer->data,st->name)) {
if(layer->debug >= MS_DEBUGLEVEL_DEBUG) {
msDebug("replacing scaletoken (%s) with (%s) in layer->data (%s) for scale=%f\n",
st->name,ste->value,layer->name,scale);
}
check_st_alloc(layer);
layer->orig_st->data = layer->data;
layer->data = msStrdup(layer->data);
layer->data = msReplaceSubstring(layer->data,st->name,ste->value);
}
if(layer->tileindex && strstr(layer->tileindex,st->name)) {
if(layer->debug >= MS_DEBUGLEVEL_DEBUG) {
msDebug("replacing scaletoken (%s) with (%s) in layer->tileindex (%s) for scale=%f\n",
st->name,ste->value,layer->name,scale);
}
check_st_alloc(layer);
layer->orig_st->tileindex = layer->tileindex;
layer->tileindex = msStrdup(layer->tileindex);
layer->tileindex = msReplaceSubstring(layer->tileindex,st->name,ste->value);
}
if(layer->tileitem && strstr(layer->tileitem,st->name)) {
if(layer->debug >= MS_DEBUGLEVEL_DEBUG) {
msDebug("replacing scaletoken (%s) with (%s) in layer->tileitem (%s) for scale=%f\n",
st->name,ste->value,layer->name,scale);
}
check_st_alloc(layer);
layer->orig_st->tileitem = layer->tileitem;
layer->tileitem = msStrdup(layer->tileitem);
layer->tileitem = msReplaceSubstring(layer->tileitem,st->name,ste->value);
}
if(layer->filteritem && strstr(layer->filteritem,st->name)) {
if(layer->debug >= MS_DEBUGLEVEL_DEBUG) {
msDebug("replacing scaletoken (%s) with (%s) in layer->filteritem (%s) for scale=%f\n",
st->name,ste->value,layer->name,scale);
}
check_st_alloc(layer);
layer->orig_st->filteritem = layer->filteritem;
layer->filteritem = msStrdup(layer->filteritem);
layer->filteritem = msReplaceSubstring(layer->filteritem,st->name,ste->value);
}
if(layer->filter.string && strstr(layer->filter.string,st->name)) {
char *tmpval;
if(layer->debug >= MS_DEBUGLEVEL_DEBUG) {
msDebug("replacing scaletoken (%s) with (%s) in layer->filter (%s) for scale=%f\n",
st->name,ste->value,layer->name,scale);
}
check_st_alloc(layer);
layer->orig_st->filter = msStrdup(layer->filter.string);
tmpval = msStrdup(layer->filter.string);
tmpval = msReplaceSubstring(tmpval,st->name,ste->value);
if(msLoadExpressionString(&(layer->filter),tmpval) == -1) return(MS_FAILURE); /* msLoadExpressionString() cleans up previously allocated expression */
msFree(tmpval);
}
for(p=0;p<layer->numprocessing;p++) {
if(strstr(layer->processing[p],st->name)) {
check_st_alloc(layer);
layer->orig_st->n_processing++;
layer->orig_st->processing = msSmallRealloc(layer->orig_st->processing, layer->orig_st->n_processing * sizeof(char*));
layer->orig_st->processing_idx = msSmallRealloc(layer->orig_st->processing_idx, layer->orig_st->n_processing * sizeof(int));
layer->orig_st->processing[layer->orig_st->n_processing-1] = layer->processing[p];
layer->orig_st->processing_idx[layer->orig_st->n_processing-1] = p;
layer->processing[p] = msStrdup(layer->processing[p]);
layer->processing[p] = msReplaceSubstring(layer->processing[p],st->name,ste->value);
}
}
}
return MS_SUCCESS;
}
/*
** Does exactly what it implies, readies a layer for processing.
*/
int msLayerOpen(layerObj *layer)
{
int rv;
/* RFC-86 Scale dependant token replacements*/
rv = msLayerApplyScaletokens(layer,(layer->map)?layer->map->scaledenom:-1);
if (rv != MS_SUCCESS) return rv;
/* RFC-69 clustering support */
if (layer->cluster.region)
return msClusterLayerOpen(layer);
if(layer->features && layer->connectiontype != MS_GRATICULE )
layer->connectiontype = MS_INLINE;
if(layer->tileindex && layer->connectiontype == MS_SHAPEFILE)
layer->connectiontype = MS_TILED_SHAPEFILE;
if(layer->type == MS_LAYER_RASTER && layer->connectiontype != MS_WMS
&& layer->connectiontype != MS_KERNELDENSITY)
layer->connectiontype = MS_RASTER;
if ( ! layer->vtable) {
rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerOpen(layer);
}
/*
** Returns MS_TRUE if layer has been opened using msLayerOpen(), MS_FALSE otherwise
*/
int msLayerIsOpen(layerObj *layer)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerIsOpen(layer);
}
/*
** Returns MS_TRUE is a layer supports the common expression/filter syntax (RFC 64) and MS_FALSE otherwise.
*/
int msLayerSupportsCommonFilters(layerObj *layer)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerSupportsCommonFilters(layer);
}
int msLayerTranslateFilter(layerObj *layer, expressionObj *filter, char *filteritem)
{
if (!layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerTranslateFilter(layer, filter, filteritem);
}
/*
** Performs a spatial, and optionally an attribute based feature search. The function basically
** prepares things so that candidate features can be accessed by query or drawing functions. For
** OGR and shapefiles this sets an internal bit vector that indicates whether a particular feature
** is to processed. For SDE it executes an SQL statement on the SDE server. Once run the msLayerNextShape
** function should be called to actually access the shapes.
**
** Note that for shapefiles we apply any maxfeatures constraint at this point. That may be the only
** connection type where this is feasible.
*/
int msLayerWhichShapes(layerObj *layer, rectObj rect, int isQuery)
{
if(!msLayerSupportsCommonFilters(layer))
msLayerTranslateFilter(layer, &layer->filter, layer->filteritem);
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerWhichShapes(layer, rect, isQuery);
}
/*
** Called after msWhichShapes has been called to actually retrieve shapes within a given area
** and matching a vendor specific filter (i.e. layer FILTER attribute).
**
** Shapefiles: NULL shapes (shapes with attributes but NO vertices are skipped)
*/
int msLayerNextShape(layerObj *layer, shapeObj *shape)
{
int rv, filter_passed;
if ( ! layer->vtable) {
rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
#ifdef USE_V8_MAPSCRIPT
/* we need to force the GetItems for the geomtransform attributes */
if(!layer->items &&
layer->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION &&
strstr(layer->_geomtransform.string, "javascript"))
msLayerGetItems(layer);
#endif
/* At the end of switch case (default -> break; -> return MS_FAILURE),
* was following TODO ITEM:
*
* TO DO! This is where dynamic joins will happen. Joined attributes will be
* tagged on to the main attributes with the naming scheme [join name].[item name].
* We need to leverage the iteminfo (I think) at this point
*/
/* RFC 91: MapServer-based filtering is done at a more general level. */
do {
rv = layer->vtable->LayerNextShape(layer, shape);
if(rv != MS_SUCCESS) return rv;
filter_passed = MS_TRUE; /* By default accept ANY shape */
/* attributes need to be iconv'd to UTF-8 before any filter logic is applied */
if(layer->encoding) {
rv = msLayerEncodeShapeAttributes(layer,shape);
if(rv != MS_SUCCESS)
return rv;
}
// if(layer->numitems > 0 && layer->iteminfo) {
filter_passed = msEvalExpression(layer, shape, &(layer->filter), layer->filteritemindex);
// }
if(!filter_passed) msFreeShape(shape);
} while(!filter_passed);
/* RFC89 Apply Layer GeomTransform */
if(layer->_geomtransform.type != MS_GEOMTRANSFORM_NONE && rv == MS_SUCCESS) {
rv = msGeomTransformShape(layer->map, layer, shape);
if(rv != MS_SUCCESS)
return rv;
}
return rv;
}
/*
** Used to retrieve a shape from a result set by index. Result sets are created by the various
** msQueryBy...() functions. The index is assigned by the data source.
*/
/* int msLayerResultsGetShape(layerObj *layer, shapeObj *shape, int tile, long record)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerResultsGetShape(layer, shape, tile, record);
} */
/*
** Used to retrieve a shape by index. All data sources must be capable of random access using
** a record number(s) of some sort.
*/
int msLayerGetShape(layerObj *layer, shapeObj *shape, resultObj *record)
{
int rv;
if( ! layer->vtable) {
rv = msInitializeVirtualTable(layer);
if(rv != MS_SUCCESS)
return rv;
}
/*
** TODO: This is where dynamic joins could happen. Joined attributes would be
** tagged on to the main attributes with the naming scheme [join name].[item name].
*/
rv = layer->vtable->LayerGetShape(layer, shape, record);
if(rv != MS_SUCCESS)
return rv;
/* RFC89 Apply Layer GeomTransform */
if(layer->_geomtransform.type != MS_GEOMTRANSFORM_NONE && rv == MS_SUCCESS) {
rv = msGeomTransformShape(layer->map, layer, shape);
if(rv != MS_SUCCESS)
return rv;
}
if(layer->encoding) {
rv = msLayerEncodeShapeAttributes(layer,shape);
if(rv != MS_SUCCESS)
return rv;
}
return rv;
}
/*
** Returns the number of shapes that match the potential filter and extent.
* rectProjection is the projection in which rect is expressed, or can be NULL if
* rect should be considered in the layer projection.
* This should be equivalent to calling msLayerWhichShapes() and counting the
* number of shapes returned by msLayerNextShape(), honouring layer->maxfeatures
* limitation if layer->maxfeatures>=0, and honouring layer->startindex if
* layer->startindex >= 1 and paging is enabled.
* Returns -1 in case of failure.
*/
int msLayerGetShapeCount(layerObj *layer, rectObj rect, projectionObj *rectProjection)
{
int rv;
if( ! layer->vtable) {
rv = msInitializeVirtualTable(layer);
if(rv != MS_SUCCESS)
return -1;
}
return layer->vtable->LayerGetShapeCount(layer, rect, rectProjection);
}
/*
** Closes resources used by a particular layer.
*/
void msLayerClose(layerObj *layer)
{
/* no need for items once the layer is closed */
msLayerFreeItemInfo(layer);
if(layer->items) {
msFreeCharArray(layer->items, layer->numitems);
layer->items = NULL;
layer->numitems = 0;
}
/* clear out items used as part of expressions (bug #2702) -- what about the layer filter? */
msLayerFreeExpressions(layer);
if (layer->vtable) {
layer->vtable->LayerClose(layer);
}
msLayerRestoreFromScaletokens(layer);
}
/*
** Clear out items used as part of expressions.
*/
void msLayerFreeExpressions(layerObj *layer)
{
int i,j,k;
msFreeExpressionTokens(&(layer->filter));
msFreeExpressionTokens(&(layer->cluster.group));
msFreeExpressionTokens(&(layer->cluster.filter));
for(i=0; i<layer->numclasses; i++) {
msFreeExpressionTokens(&(layer->class[i]->expression));
msFreeExpressionTokens(&(layer->class[i]->text));
for(j=0; j<layer->class[i]->numstyles; j++)
msFreeExpressionTokens(&(layer->class[i]->styles[j]->_geomtransform));
for(k=0; k<layer->class[i]->numlabels; k++) {
msFreeExpressionTokens(&(layer->class[i]->labels[k]->expression));
msFreeExpressionTokens(&(layer->class[i]->labels[k]->text));
}
}
}
/*
** Retrieves a list of attributes available for this layer. Most sources also set the iteminfo array
** at this point. This function is used when processing query results to expose attributes to query
** templates. At that point all attributes are fair game.
*/
int msLayerGetItems(layerObj *layer)
{
const char *itemNames;
/* clean up any previously allocated instances */
msLayerFreeItemInfo(layer);
if(layer->items) {
msFreeCharArray(layer->items, layer->numitems);
layer->items = NULL;
layer->numitems = 0;
}
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
/* At the end of switch case (default -> break; -> return MS_FAILURE),
* was following TODO ITEM:
*/
/* TO DO! Need to add any joined itemd on to the core layer items, one long list! */
itemNames = msLayerGetProcessingKey( layer, "ITEMS" );
if (itemNames) {
layer->items = msStringSplit(itemNames, ',', &layer->numitems);
/* populate the iteminfo array */
return (msLayerInitItemInfo(layer));
} else
return layer->vtable->LayerGetItems(layer);
}
/*
** Returns extent of spatial coverage for a layer.
**
** If layer->extent is set then this value is used, otherwise the
** driver-specific implementation is called (this can be expensive).
**
** If layer is not already opened then it is opened and closed (so this
** function can be called on both opened or closed layers).
**
** Returns MS_SUCCESS/MS_FAILURE.
*/
int msLayerGetExtent(layerObj *layer, rectObj *extent)
{
int need_to_close = MS_FALSE, status = MS_SUCCESS;
if (MS_VALID_EXTENT(layer->extent)) {
*extent = layer->extent;
return MS_SUCCESS;
}
if (!msLayerIsOpen(layer)) {
if (msLayerOpen(layer) != MS_SUCCESS)
return MS_FAILURE;
need_to_close = MS_TRUE;
}
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS) {
if (need_to_close)
msLayerClose(layer);
return rv;
}
}
status = layer->vtable->LayerGetExtent(layer, extent);
if (need_to_close)
msLayerClose(layer);
return(status);
}
int msLayerGetItemIndex(layerObj *layer, char *item)
{
int i;
for(i=0; i<layer->numitems; i++) {
if(strcasecmp(layer->items[i], item) == 0) return(i);
}
return -1; /* item not found */
}
static int string2list(char **list, int *listsize, char *string)
{
int i;
for(i=0; i<(*listsize); i++) {
if(strcasecmp(list[i], string) == 0) {
/* printf("string2list (duplicate): %s %d\n", string, i); */
return(i);
}
}
list[i] = msStrdup(string);
(*listsize)++;
/* printf("string2list: %s %d\n", string, i); */
return(i);
}
extern int msyylex(void);
extern int msyylex_destroy(void);
extern int msyystate;
extern char *msyystring; /* string to tokenize */
extern double msyynumber; /* token containers */
extern char *msyystring_buffer;
const char *msExpressionTokenToString(int token) {
switch(token) {
case '(': return "(";
case ')': return ")";
case ',': return ",";
case '+': return "+";
case '-': return "-";
case '/': return "/";
case '*': return "*";
case '%': return "%";
case MS_TOKEN_LOGICAL_AND: return " and ";
case MS_TOKEN_LOGICAL_OR: return " or ";
case MS_TOKEN_LOGICAL_NOT: return " not ";
case MS_TOKEN_COMPARISON_EQ: return " = ";
case MS_TOKEN_COMPARISON_NE: return " != ";
case MS_TOKEN_COMPARISON_GT: return " > ";
case MS_TOKEN_COMPARISON_GE: return " >= ";
case MS_TOKEN_COMPARISON_LT: return " < ";
case MS_TOKEN_COMPARISON_LE: return " <= ";
case MS_TOKEN_COMPARISON_IEQ: return "";
case MS_TOKEN_COMPARISON_RE: return " ~ ";
case MS_TOKEN_COMPARISON_IRE: return " ~* ";
case MS_TOKEN_COMPARISON_IN: return " in ";
case MS_TOKEN_COMPARISON_LIKE: return " like ";
case MS_TOKEN_COMPARISON_INTERSECTS: return "intersects";
case MS_TOKEN_COMPARISON_DISJOINT: return "disjoint";
case MS_TOKEN_COMPARISON_TOUCHES: return "touches";
case MS_TOKEN_COMPARISON_OVERLAPS: return "overlaps";
case MS_TOKEN_COMPARISON_CROSSES: return "crosses";
case MS_TOKEN_COMPARISON_WITHIN: return "within";
case MS_TOKEN_COMPARISON_CONTAINS: return "contains";
case MS_TOKEN_COMPARISON_EQUALS: return "equals";
case MS_TOKEN_COMPARISON_BEYOND: return "beyond";
case MS_TOKEN_COMPARISON_DWITHIN: return "dwithin";
case MS_TOKEN_FUNCTION_LENGTH: return "length";
case MS_TOKEN_FUNCTION_TOSTRING: return "tostring";
case MS_TOKEN_FUNCTION_COMMIFY: return "commify";
case MS_TOKEN_FUNCTION_AREA: return "area";
case MS_TOKEN_FUNCTION_ROUND: return "round";
case MS_TOKEN_FUNCTION_BUFFER: return "buffer";
case MS_TOKEN_FUNCTION_DIFFERENCE: return "difference";
case MS_TOKEN_FUNCTION_SIMPLIFY: return "simplify";
// case MS_TOKEN_FUNCTION_SIMPLIFYPT:
case MS_TOKEN_FUNCTION_GENERALIZE: return "generalize";
default: return NULL;
}
}
int msTokenizeExpression(expressionObj *expression, char **list, int *listsize)
{
tokenListNodeObjPtr node;
int token;
/* TODO: make sure the constants can't somehow reference invalid expression types */
/* if(expression->type != MS_EXPRESSION && expression->type != MS_GEOMTRANSFORM_EXPRESSION) return MS_SUCCESS; */
msAcquireLock(TLOCK_PARSER);
msyystate = MS_TOKENIZE_EXPRESSION;
msyystring = expression->string; /* the thing we're tokenizing */
while((token = msyylex()) != 0) { /* keep processing tokens until the end of the string (\0) */
if((node = (tokenListNodeObjPtr) malloc(sizeof(tokenListNodeObj))) == NULL) {
msSetError(MS_MEMERR, NULL, "msTokenizeExpression()");
goto parse_error;
}
node->tokensrc = NULL;
node->tailifhead = NULL;
node->next = NULL;
switch(token) {
case MS_TOKEN_LITERAL_BOOLEAN:
case MS_TOKEN_LITERAL_NUMBER:
node->token = token;
node->tokenval.dblval = msyynumber;
break;
case MS_TOKEN_LITERAL_STRING:
node->token = token;
node->tokenval.strval = msStrdup(msyystring_buffer);
break;
case MS_TOKEN_LITERAL_TIME:
node->tokensrc = msStrdup(msyystring_buffer);
node->token = token;
msTimeInit(&(node->tokenval.tmval));
if(msParseTime(msyystring_buffer, &(node->tokenval.tmval)) != MS_TRUE) {
msSetError(MS_PARSEERR, "Parsing time value failed.", "msTokenizeExpression()");
free(node);
goto parse_error;
}
break;
case MS_TOKEN_BINDING_DOUBLE: /* we've encountered an attribute (binding) reference */
case MS_TOKEN_BINDING_INTEGER:
case MS_TOKEN_BINDING_STRING:
case MS_TOKEN_BINDING_TIME:
node->token = token; /* binding type */
node->tokenval.bindval.item = msStrdup(msyystring_buffer);
if(list) node->tokenval.bindval.index = string2list(list, listsize, msyystring_buffer);
break;
case MS_TOKEN_BINDING_SHAPE:
node->token = token;
break;
case MS_TOKEN_BINDING_MAP_CELLSIZE:
node->token = token;
break;
case MS_TOKEN_BINDING_DATA_CELLSIZE:
node->token = token;
break;
case MS_TOKEN_FUNCTION_FROMTEXT: /* we want to process a shape from WKT once and not for every feature being evaluated */
if((token = msyylex()) != 40) { /* ( */
msSetError(MS_PARSEERR, "Parsing fromText function failed.", "msTokenizeExpression()");
free(node);
goto parse_error;
}
if((token = msyylex()) != MS_TOKEN_LITERAL_STRING) {
msSetError(MS_PARSEERR, "Parsing fromText function failed.", "msTokenizeExpression()");
free(node);
goto parse_error;
}
node->token = MS_TOKEN_LITERAL_SHAPE;
node->tokenval.shpval = msShapeFromWKT(msyystring_buffer);
if(!node->tokenval.shpval) {
msSetError(MS_PARSEERR, "Parsing fromText function failed, WKT processing failed.", "msTokenizeExpression()");
free(node);
goto parse_error;
}
/* todo: perhaps process optional args (e.g. projection) */
if((token = msyylex()) != 41) { /* ) */
msSetError(MS_PARSEERR, "Parsing fromText function failed.", "msTokenizeExpression()");
msFreeShape(node->tokenval.shpval);
free(node->tokenval.shpval);
free(node);
goto parse_error;
}
break;
default:
node->token = token; /* for everything else */
break;
}
/* add node to token list */
if(expression->tokens == NULL) {
expression->tokens = node;
} else {
if(expression->tokens->tailifhead != NULL) /* this should never be NULL, but just in case */
expression->tokens->tailifhead->next = node; /* put the node at the end of the list */
}
/* repoint the head of the list to the end - our new element
this causes a loop if we are at the head, be careful not to
walk in a loop */
expression->tokens->tailifhead = node;
}
expression->curtoken = expression->tokens; /* point at the first token */
msReleaseLock(TLOCK_PARSER);
return MS_SUCCESS;
parse_error:
msReleaseLock(TLOCK_PARSER);
return MS_FAILURE;
}
static void buildLayerItemList(layerObj *layer)
{
int i, j, k, l;
/*
** build layer item list, compute item indexes for explicity item references (e.g. classitem) or item bindings
*/
/* layer items */
if(layer->classitem) layer->classitemindex = string2list(layer->items, &(layer->numitems), layer->classitem);
if(layer->filteritem) layer->filteritemindex = string2list(layer->items, &(layer->numitems), layer->filteritem);
if(layer->styleitem && (strcasecmp(layer->styleitem, "AUTO") != 0) && (strncasecmp(layer->styleitem, "javascript://",13) != 0))
layer->styleitemindex = string2list(layer->items, &(layer->numitems), layer->styleitem);
if(layer->labelitem) layer->labelitemindex = string2list(layer->items, &(layer->numitems), layer->labelitem);
if(layer->utfitem) layer->utfitemindex = string2list(layer->items, &(layer->numitems), layer->utfitem);
/* layer classes */
for(i=0; i<layer->numclasses; i++) {
if(layer->class[i]->expression.type == MS_EXPRESSION) /* class expression */
msTokenizeExpression(&(layer->class[i]->expression), layer->items, &(layer->numitems));
/* class styles (items, bindings, geomtransform) */
for(j=0; j<layer->class[i]->numstyles; j++) {
if(layer->class[i]->styles[j]->rangeitem)
layer->class[i]->styles[j]->rangeitemindex = string2list(layer->items, &(layer->numitems), layer->class[i]->styles[j]->rangeitem);
for(k=0; k<MS_STYLE_BINDING_LENGTH; k++) {
if(layer->class[i]->styles[j]->bindings[k].item)
layer->class[i]->styles[j]->bindings[k].index = string2list(layer->items, &(layer->numitems), layer->class[i]->styles[j]->bindings[k].item);
if (layer->class[i]->styles[j]->exprBindings[k].type == MS_EXPRESSION)
{
msTokenizeExpression(
&(layer->class[i]->styles[j]->exprBindings[k]),
layer->items, &(layer->numitems));
}
}
if(layer->class[i]->styles[j]->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION)
msTokenizeExpression(&(layer->class[i]->styles[j]->_geomtransform), layer->items, &(layer->numitems));
}
/* class labels and label styles (items, bindings, geomtransform) */
for(l=0; l<layer->class[i]->numlabels; l++) {
for(j=0; j<layer->class[i]->labels[l]->numstyles; j++) {
if(layer->class[i]->labels[l]->styles[j]->rangeitem)
layer->class[i]->labels[l]->styles[j]->rangeitemindex = string2list(layer->items, &(layer->numitems), layer->class[i]->labels[l]->styles[j]->rangeitem);
for(k=0; k<MS_STYLE_BINDING_LENGTH; k++) {
if(layer->class[i]->labels[l]->styles[j]->bindings[k].item)
layer->class[i]->labels[l]->styles[j]->bindings[k].index = string2list(layer->items, &(layer->numitems), layer->class[i]->labels[l]->styles[j]->bindings[k].item);
if(layer->class[i]->labels[l]->styles[j]->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION)
msTokenizeExpression(&(layer->class[i]->labels[l]->styles[j]->_geomtransform), layer->items, &(layer->numitems));
}
}
for(k=0; k<MS_LABEL_BINDING_LENGTH; k++) {
if(layer->class[i]->labels[l]->bindings[k].item)
layer->class[i]->labels[l]->bindings[k].index = string2list(layer->items, &(layer->numitems), layer->class[i]->labels[l]->bindings[k].item);
if (layer->class[i]->labels[l]->exprBindings[k].type == MS_EXPRESSION)
{
msTokenizeExpression(
&(layer->class[i]->labels[l]->exprBindings[k]),
layer->items, &(layer->numitems));
}
}
/* label expression */
if(layer->class[i]->labels[l]->expression.type == MS_EXPRESSION) msTokenizeExpression(&(layer->class[i]->labels[l]->expression), layer->items, &(layer->numitems));
/* label text */
if(layer->class[i]->labels[l]->text.type == MS_EXPRESSION || (layer->class[i]->labels[l]->text.string && strchr(layer->class[i]->labels[l]->text.string,'[') != NULL && strchr(layer->class[i]->labels[l]->text.string,']') != NULL))
msTokenizeExpression(&(layer->class[i]->labels[l]->text), layer->items, &(layer->numitems));
}
/* class text */
if(layer->class[i]->text.type == MS_EXPRESSION || (layer->class[i]->text.string && strchr(layer->class[i]->text.string,'[') != NULL && strchr(layer->class[i]->text.string,']') != NULL))
msTokenizeExpression(&(layer->class[i]->text), layer->items, &(layer->numitems));
}
/* layer filter */
if(layer->filter.type == MS_EXPRESSION) msTokenizeExpression(&(layer->filter), layer->items, &(layer->numitems));
/* cluster expressions */
if(layer->cluster.group.type == MS_EXPRESSION) msTokenizeExpression(&(layer->cluster.group), layer->items, &(layer->numitems));
if(layer->cluster.filter.type == MS_EXPRESSION) msTokenizeExpression(&(layer->cluster.filter), layer->items, &(layer->numitems));
/* utfdata */
if(layer->utfdata.type == MS_EXPRESSION || (layer->utfdata.string && strchr(layer->utfdata.string,'[') != NULL && strchr(layer->utfdata.string,']') != NULL)) {
msTokenizeExpression(&(layer->utfdata), layer->items, &(layer->numitems));
}
}
/*
** This function builds a list of items necessary to draw or query a particular layer by
** examining the contents of the various xxxxitem parameters and expressions. That list is
** then used to set the iteminfo variable.
*/
int msLayerWhichItems(layerObj *layer, int get_all, const char *metadata)
{
int i, j, k, l, rv;
int nt=0;
if (!layer->vtable) {
rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS) return rv;
}
/* Cleanup any previous item selection */
msLayerFreeItemInfo(layer);
if(layer->items) {
msFreeCharArray(layer->items, layer->numitems);
layer->items = NULL;
layer->numitems = 0;
}
/*
** need a count of potential items/attributes needed
*/
/* layer level counts */
layer->classitemindex = -1;
layer->filteritemindex = -1;
layer->styleitemindex = -1;
layer->labelitemindex = -1;
layer->utfitemindex = -1;
if(layer->classitem) nt++;
if(layer->filteritem) nt++;
if(layer->styleitem &&
(strcasecmp(layer->styleitem, "AUTO") != 0) &&
(strncasecmp(layer->styleitem, "javascript://", 13) != 0)) nt++;
if(layer->filter.type == MS_EXPRESSION)
nt += msCountChars(layer->filter.string, '[');
if(layer->cluster.group.type == MS_EXPRESSION)
nt += msCountChars(layer->cluster.group.string, '[');
if(layer->cluster.filter.type == MS_EXPRESSION)
nt += msCountChars(layer->cluster.filter.string, '[');
if(layer->labelitem) nt++;
if(layer->utfitem) nt++;
if(layer->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION)
msTokenizeExpression(&layer->_geomtransform, layer->items, &(layer->numitems));
/* class level counts */
for(i=0; i<layer->numclasses; i++) {
for(j=0; j<layer->class[i]->numstyles; j++) {
if(layer->class[i]->styles[j]->rangeitem) nt++;
nt += layer->class[i]->styles[j]->numbindings;
if(layer->class[i]->styles[j]->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION)
nt += msCountChars(layer->class[i]->styles[j]->_geomtransform.string, '[');
for(k=0; k<MS_STYLE_BINDING_LENGTH; k++) {
if (layer->class[i]->styles[j]->exprBindings[k].type == MS_EXPRESSION)
{
nt += msCountChars(layer->class[i]->styles[j]->exprBindings[k].string, '[');
}
}
}
if(layer->class[i]->expression.type == MS_EXPRESSION)
nt += msCountChars(layer->class[i]->expression.string, '[');
for(l=0; l<layer->class[i]->numlabels; l++) {
nt += layer->class[i]->labels[l]->numbindings;
for(j=0; j<layer->class[i]->labels[l]->numstyles; j++) {
if(layer->class[i]->labels[l]->styles[j]->rangeitem) nt++;
nt += layer->class[i]->labels[l]->styles[j]->numbindings;
if(layer->class[i]->labels[l]->styles[j]->_geomtransform.type == MS_GEOMTRANSFORM_EXPRESSION)
nt += msCountChars(layer->class[i]->labels[l]->styles[j]->_geomtransform.string, '[');
}
for(k=0; k<MS_LABEL_BINDING_LENGTH; k++) {
if (layer->class[i]->labels[l]->exprBindings[k].type == MS_EXPRESSION)
{
nt += msCountChars(layer->class[i]->labels[l]->exprBindings[k].string, '[');
}
}
if(layer->class[i]->labels[l]->expression.type == MS_EXPRESSION)
nt += msCountChars(layer->class[i]->labels[l]->expression.string, '[');
if(layer->class[i]->labels[l]->text.type == MS_EXPRESSION || (layer->class[i]->labels[l]->text.string && strchr(layer->class[i]->labels[l]->text.string,'[') != NULL && strchr(layer->class[i]->labels[l]->text.string,']') != NULL))
nt += msCountChars(layer->class[i]->labels[l]->text.string, '[');
}
if(layer->class[i]->text.type == MS_EXPRESSION || (layer->class[i]->text.string && strchr(layer->class[i]->text.string,'[') != NULL && strchr(layer->class[i]->text.string,']') != NULL))
nt += msCountChars(layer->class[i]->text.string, '[');
}
/* utfgrid count */
if(layer->utfdata.type == MS_EXPRESSION || (layer->utfdata.string && strchr(layer->utfdata.string,'[') != NULL && strchr(layer->utfdata.string,']') != NULL))
nt += msCountChars(layer->utfdata.string, '[');
/*
** allocate space for the item list (worse case size)
*/
/* always retrieve all items in some cases */
if(layer->connectiontype == MS_INLINE ||
(layer->map->outputformat && layer->map->outputformat->renderer == MS_RENDER_WITH_KML)) {
get_all = MS_TRUE;
}
if( get_all ) {
rv = msLayerGetItems(layer);
if(nt > 0) /* need to realloc the array to accept the possible new items*/
layer->items = (char **)msSmallRealloc(layer->items, sizeof(char *)*(layer->numitems + nt));
} else {
rv = layer->vtable->LayerCreateItems(layer, nt);
}
if(rv != MS_SUCCESS)
return rv;
buildLayerItemList(layer);
if(metadata) {
char **tokens;
int n = 0;
int j;
int bFound = 0;
tokens = msStringSplit(metadata, ',', &n);
if(tokens) {
for(i=0; i<n; i++) {