forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maplayer.c
1874 lines (1585 loc) · 58.3 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);
}
int msLayerRestoreFromScaletokens(layerObj *layer)
{
if(!layer->scaletokens) {
return MS_SUCCESS;
}
if(layer->orig_data) {
msFree(layer->data);
layer->data = layer->orig_data;
layer->orig_data = NULL;
}
if(layer->orig_tileindex) {
msFree(layer->tileindex);
layer->tileindex = layer->orig_tileindex;
layer->orig_tileindex = NULL;
}
if(layer->orig_tileitem) {
msFree(layer->tileitem);
layer->tileitem = layer->orig_tileitem;
layer->orig_tileitem = NULL;
}
if(layer->orig_filter) {
msLoadExpressionString(&(layer->filter),layer->orig_filter);
msFree(layer->orig_filter);
layer->orig_filter = NULL;
}
if(layer->orig_filteritem) {
msFree(layer->filteritem);
layer->filteritem = layer->orig_filteritem;
layer->orig_filteritem = NULL;
}
return MS_SUCCESS;
}
int msLayerApplyScaletokens(layerObj *layer, double scale)
{
int i;
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);
}
layer->orig_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);
}
layer->orig_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);
}
layer->orig_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);
}
layer->orig_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);
}
layer->orig_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); /* loadExpression() cleans up previously allocated expression */
msFree(tmpval);
}
}
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_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);
}
/*
** 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 ( ! 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;
if ( ! layer->vtable) {
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! 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 */
rv = layer->vtable->LayerNextShape(layer, shape);
/* RFC89 Apply Layer GeomTransform */
if(layer->_geomtransform.type != MS_GEOMTRANSFORM_NONE && rv == MS_SUCCESS) {
rv = msGeomTransformShape(layer->map, layer, shape);
}
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);
/* RFC89 Apply Layer GeomTransform */
if(layer->_geomtransform.type != MS_GEOMTRANSFORM_NONE && rv == MS_SUCCESS) {
rv = msGeomTransformShape(layer->map, layer, shape);
}
return rv;
}
/*
** Closes resources used by a particular layer.
*/
void msLayerClose(layerObj *layer)
{
int i,j,k;
/* 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? */
freeExpressionTokens(&(layer->filter));
freeExpressionTokens(&(layer->cluster.group));
freeExpressionTokens(&(layer->cluster.filter));
for(i=0; i<layer->numclasses; i++) {
freeExpressionTokens(&(layer->class[i]->expression));
freeExpressionTokens(&(layer->class[i]->text));
for(j=0; j<layer->class[i]->numstyles; j++)
freeExpressionTokens(&(layer->class[i]->styles[j]->_geomtransform));
for(k=0; k<layer->class[i]->numlabels; k++) {
freeExpressionTokens(&(layer->class[i]->labels[k]->expression));
freeExpressionTokens(&(layer->class[i]->labels[k]->text));
}
}
if (layer->vtable) {
layer->vtable->LayerClose(layer);
}
msLayerRestoreFromScaletokens(layer);
}
/*
** 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;
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->tailifhead = NULL;
node->next = NULL;
switch(token) {
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->token = token;
msTimeInit(&(node->tokenval.tmval));
if(msParseTime(msyystring_buffer, &(node->tokenval.tmval)) != MS_TRUE) {
msSetError(MS_PARSEERR, "Parsing time value failed.", "msTokenizeExpression()");
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()");
goto parse_error;
}
if((token = msyylex()) != MS_TOKEN_LITERAL_STRING) {
msSetError(MS_PARSEERR, "Parsing fromText function failed.", "msTokenizeExpression()");
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()");
goto parse_error;
}
/* todo: perhaps process optional args (e.g. projection) */
if((token = msyylex()) != 41) { /* ) */
msSetError(MS_PARSEERR, "Parsing fromText function failed.", "msTokenizeExpression()");
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;
}
/*
** 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, 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;
if(layer->classitem) nt++;
if(layer->filteritem) nt++;
if(layer->styleitem && strcasecmp(layer->styleitem, "AUTO") != 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->_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, '[');
}
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, '[');
}
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, '[');
}
/*
** allocate space for the item list (worse case size)
*/
/* always retrieve all items in some cases */
if(layer->connectiontype == MS_INLINE || get_all == MS_TRUE ||
(layer->map->outputformat && layer->map->outputformat->renderer == MS_RENDER_WITH_KML)) {
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;
}
/*
** build layer item list, compute item indexes for explicity item references (e.g. classitem) or item bindings
*/
if(nt > 0) {
/* 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) layer->styleitemindex = string2list(layer->items, &(layer->numitems), layer->styleitem);
if(layer->labelitem) layer->labelitemindex = string2list(layer->items, &(layer->numitems), layer->labelitem);
/* layer classes */
for(i=0; i<layer->numclasses; i++) {
/* class expression */
if(layer->class[i]->expression.type == MS_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]->_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);
/* 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));
}
if(metadata) {
char **tokens;
int n = 0;
int j;
int bFound = 0;
tokens = msStringSplit(metadata, ',', &n);
if(tokens) {
for(i=0; i<n; i++) {
bFound = 0;
for(j=0; j<layer->numitems; j++) {
if(strcmp(tokens[i], layer->items[j]) == 0) {
bFound = 1;
break;
}
}
if(!bFound) {
layer->numitems++;
layer->items = (char **)msSmallRealloc(layer->items, sizeof(char *)*(layer->numitems));
layer->items[layer->numitems-1] = msStrdup(tokens[i]);
}
}
msFreeCharArray(tokens, n);
}
}
/* populate the iteminfo array */
if(layer->numitems == 0)
return(MS_SUCCESS);
return(msLayerInitItemInfo(layer));
}
/*
** A helper function to set the items to be retrieved with a particular shape. Unused at the moment but will be used
** from within MapScript. Should not need modification.
*/
int msLayerSetItems(layerObj *layer, char **items, int numitems)
{
int i;
/* Cleanup any previous item selection */
msLayerFreeItemInfo(layer);
if(layer->items) {
msFreeCharArray(layer->items, layer->numitems);
layer->items = NULL;
layer->numitems = 0;
}
/* now allocate and set the layer item parameters */
layer->items = (char **)malloc(sizeof(char *)*numitems);
MS_CHECK_ALLOC(layer->items, sizeof(char *)*numitems, MS_FAILURE);
for(i=0; i<numitems; i++)
layer->items[i] = msStrdup(items[i]);
layer->numitems = numitems;
/* populate the iteminfo array */
return(msLayerInitItemInfo(layer));
return(MS_SUCCESS);
}
/*
** Fills a classObj with style info from the specified shape. This is used
** with STYLEITEM AUTO when rendering shapes.
** For optimal results, this should be called immediately after
** GetNextShape() or GetShape() so that the shape doesn't have to be read
** twice.
**
*/
int msLayerGetAutoStyle(mapObj *map, layerObj *layer, classObj *c, shapeObj* shape)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerGetAutoStyle(map, layer, c, shape);
}
/*
** Fills a classObj with style info from the specified attribute. This is used
** with STYLEITEM "attribute" when rendering shapes.
**
*/
int msLayerGetFeatureStyle(mapObj *map, layerObj *layer, classObj *c, shapeObj* shape)
{
char* stylestring;
if (layer->styleitem && layer->styleitemindex >=0) {
stylestring = shape->values[layer->styleitemindex];
/* try to find out the current style format */
if (strncasecmp(stylestring,"style",5) == 0) {
resetClassStyle(c);
c->layer = layer;
if (msMaybeAllocateClassStyle(c, 0))
return(MS_FAILURE);
msUpdateStyleFromString(c->styles[0], stylestring, MS_FALSE);
if(c->styles[0]->symbolname) {
if((c->styles[0]->symbol = msGetSymbolIndex(&(map->symbolset), c->styles[0]->symbolname, MS_TRUE)) == -1) {
msSetError(MS_MISCERR, "Undefined symbol \"%s\" in class of layer %s.", "msLayerGetFeatureStyle()",
c->styles[0]->symbolname, layer->name);
return MS_FAILURE;
}
}
} else if (strncasecmp(stylestring,"class",5) == 0) {
if (strcasestr(stylestring, " style ") != NULL) {
/* reset style if stylestring contains style definitions */
resetClassStyle(c);
c->layer = layer;
}
msUpdateClassFromString(c, stylestring, MS_FALSE);
} else if (strncasecmp(stylestring,"pen",3) == 0 || strncasecmp(stylestring,"brush",5) == 0 ||
strncasecmp(stylestring,"symbol",6) == 0 || strncasecmp(stylestring,"label",5) == 0) {
msOGRUpdateStyleFromString(map, layer, c, stylestring);
} else {
resetClassStyle(c);
}
return MS_SUCCESS;
}
return MS_FAILURE;
}
/*
Returns the number of inline feature of a layer
*/
int msLayerGetNumFeatures(layerObj *layer)
{
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerGetNumFeatures(layer);
}
void
msLayerSetProcessingKey( layerObj *layer, const char *key, const char *value)
{
int len = strlen(key);
int i;
char *directive = NULL;
if( value != NULL ) {
directive = (char *) msSmallMalloc(strlen(key)+strlen(value)+2);
sprintf( directive, "%s=%s", key, value );
}
for( i = 0; i < layer->numprocessing; i++ ) {
if( strncasecmp( key, layer->processing[i], len ) == 0
&& layer->processing[i][len] == '=' ) {
free( layer->processing[i] );
/*
** Either replace the existing entry with a new one or
** clear the entry.
*/
if( directive != NULL )
layer->processing[i] = directive;
else {
layer->processing[i] = layer->processing[layer->numprocessing-1];
layer->processing[layer->numprocessing-1] = NULL;
layer->numprocessing--;
}
return;
}
}
/* otherwise add the directive at the end. */
if( directive != NULL ) {
msLayerAddProcessing( layer, directive );
free( directive );
}
}
void msLayerAddProcessing( layerObj *layer, const char *directive )
{
layer->numprocessing++;
if( layer->numprocessing == 1 )
layer->processing = (char **) msSmallMalloc(2*sizeof(char *));
else
layer->processing = (char **) msSmallRealloc(layer->processing, sizeof(char*) * (layer->numprocessing+1) );
layer->processing[layer->numprocessing-1] = msStrdup(directive);
layer->processing[layer->numprocessing] = NULL;
}
char *msLayerGetProcessing( layerObj *layer, int proc_index)
{
if (proc_index < 0 || proc_index >= layer->numprocessing) {
msSetError(MS_CHILDERR, "Invalid processing index.", "msLayerGetProcessing()");
return NULL;
} else {
return layer->processing[proc_index];
}
}
char *msLayerGetProcessingKey( layerObj *layer, const char *key )
{
int i, len = strlen(key);
for( i = 0; i < layer->numprocessing; i++ ) {
if( strncasecmp(layer->processing[i],key,len) == 0
&& layer->processing[i][len] == '=' )
return layer->processing[i] + len + 1;
}
return NULL;
}
/************************************************************************/
/* msLayerGetMaxFeaturesToDraw */
/* */
/* Check to see if maxfeaturestodraw is set as a metadata or an */
/* output format option. Used for vector layers to limit the */
/* number of fatures rendered. */
/************************************************************************/
int msLayerGetMaxFeaturesToDraw(layerObj *layer, outputFormatObj *format)
{
int nMaxFeatures = -1;
const char *pszTmp = NULL;
if (layer && format) {
pszTmp = msLookupHashTable(&layer->metadata, "maxfeaturestodraw");
if (pszTmp)
nMaxFeatures = atoi(pszTmp);
else {
pszTmp = msLookupHashTable(&layer->map->web.metadata, "maxfeaturestodraw");
if (pszTmp)
nMaxFeatures = atoi(pszTmp);
}
if (nMaxFeatures < 0)
nMaxFeatures = atoi(msGetOutputFormatOption( format, "maxfeaturestodraw", "-1"));
}
return nMaxFeatures;
}
int msLayerClearProcessing( layerObj *layer )
{
if (layer->numprocessing > 0) {
msFreeCharArray( layer->processing, layer->numprocessing );