-
-
Notifications
You must be signed in to change notification settings - Fork 369
/
maputil.c
2763 lines (2389 loc) · 90.9 KB
/
maputil.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: Various utility functions ... a real hodgepodge.
* Author: Steve Lime and the MapServer team.
*
* Notes: Some code (notably msAlphaBlend()) are directly derived from GD. See
* the mapserver/GD-COPYING file for the GD license. Use of this code in this
* manner is compatible with the MapServer license.
*
******************************************************************************
* 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 <time.h>
#include "mapserver.h"
#include "maptime.h"
#include "mapthread.h"
#include "mapcopy.h"
#include "mapows.h"
#include "gdal.h"
#if defined(_WIN32) && !defined(__CYGWIN__)
# include <windows.h>
# include <tchar.h>
# include <fcntl.h>
# include <io.h>
#include <process.h>
#endif
#ifdef USE_RSVG
#include <glib-object.h>
#endif
#ifdef USE_GEOS
#include <geos_c.h>
#endif
extern char *msyystring_buffer;
extern int msyylex_destroy(void);
extern int yyparse(parseObj *);
int msScaleInBounds(double scale, double minscale, double maxscale)
{
if(scale > 0) {
if(maxscale != -1 && scale >= maxscale) return MS_FALSE;
if(minscale != -1 && scale < minscale) return MS_FALSE;
}
return MS_TRUE;
}
/*
** Helper functions to convert from strings to other types or objects.
*/
static int bindIntegerAttribute(int *attribute, const char *value)
{
if(!value || strlen(value) == 0) return MS_FAILURE;
*attribute = MS_NINT(atof(value)); /*use atof instead of atoi as a fix for bug 2394*/
return MS_SUCCESS;
}
static int bindDoubleAttribute(double *attribute, const char *value)
{
if(!value || strlen(value) == 0) return MS_FAILURE;
*attribute = atof(value);
return MS_SUCCESS;
}
static int bindColorAttribute(colorObj *attribute, const char *value)
{
int len;
if(!value || ((len = strlen(value)) == 0)) return MS_FAILURE;
if(value[0] == '#' && (len == 7 || len == 9)) { /* got a hex color */
char hex[2];
hex[0] = value[1];
hex[1] = value[2];
attribute->red = msHexToInt(hex);
hex[0] = value[3];
hex[1] = value[4];
attribute->green = msHexToInt(hex);
hex[0] = value[5];
hex[1] = value[6];
attribute->blue = msHexToInt(hex);
if(len == 9) {
hex[0] = value[7];
hex[1] = value[8];
attribute->alpha = msHexToInt(hex);
}
return MS_SUCCESS;
} else { /* try a space delimited string */
char **tokens=NULL;
int numtokens=0;
tokens = msStringSplit(value, ' ', &numtokens);
if(tokens==NULL || numtokens != 3) {
msFreeCharArray(tokens, numtokens);
return MS_FAILURE; /* punt */
}
attribute->red = atoi(tokens[0]);
attribute->green = atoi(tokens[1]);
attribute->blue = atoi(tokens[2]);
msFreeCharArray(tokens, numtokens);
return MS_SUCCESS;
}
return MS_FAILURE; /* shouldn't get here */
}
static void bindStyle(layerObj *layer, shapeObj *shape, styleObj *style, int drawmode)
{
assert(MS_DRAW_FEATURES(drawmode));
if(style->numbindings > 0) {
if(style->bindings[MS_STYLE_BINDING_SYMBOL].index != -1) {
style->symbol = msGetSymbolIndex(&(layer->map->symbolset), shape->values[style->bindings[MS_STYLE_BINDING_SYMBOL].index], MS_TRUE);
if(style->symbol == -1) style->symbol = 0; /* a reasonable default (perhaps should throw an error?) */
}
if(style->bindings[MS_STYLE_BINDING_ANGLE].index != -1) {
style->angle = 360.0;
bindDoubleAttribute(&style->angle, shape->values[style->bindings[MS_STYLE_BINDING_ANGLE].index]);
}
if(style->bindings[MS_STYLE_BINDING_SIZE].index != -1) {
style->size = 1;
bindDoubleAttribute(&style->size, shape->values[style->bindings[MS_STYLE_BINDING_SIZE].index]);
}
if(style->bindings[MS_STYLE_BINDING_WIDTH].index != -1) {
style->width = 1;
bindDoubleAttribute(&style->width, shape->values[style->bindings[MS_STYLE_BINDING_WIDTH].index]);
}
if(style->bindings[MS_STYLE_BINDING_COLOR].index != -1 && !MS_DRAW_QUERY(drawmode)) {
MS_INIT_COLOR(style->color, -1,-1,-1,255);
bindColorAttribute(&style->color, shape->values[style->bindings[MS_STYLE_BINDING_COLOR].index]);
}
if(style->bindings[MS_STYLE_BINDING_OUTLINECOLOR].index != -1 && !MS_DRAW_QUERY(drawmode)) {
MS_INIT_COLOR(style->outlinecolor, -1,-1,-1,255);
bindColorAttribute(&style->outlinecolor, shape->values[style->bindings[MS_STYLE_BINDING_OUTLINECOLOR].index]);
}
if(style->bindings[MS_STYLE_BINDING_OUTLINEWIDTH].index != -1) {
style->outlinewidth = 1;
bindDoubleAttribute(&style->outlinewidth, shape->values[style->bindings[MS_STYLE_BINDING_OUTLINEWIDTH].index]);
}
if(style->bindings[MS_STYLE_BINDING_OPACITY].index != -1) {
style->opacity = 100;
bindIntegerAttribute(&style->opacity, shape->values[style->bindings[MS_STYLE_BINDING_OPACITY].index]);
}
if(style->bindings[MS_STYLE_BINDING_OFFSET_X].index != -1) {
style->offsetx = 0;
bindDoubleAttribute(&style->offsetx, shape->values[style->bindings[MS_STYLE_BINDING_OFFSET_X].index]);
}
if(style->bindings[MS_STYLE_BINDING_OFFSET_Y].index != -1) {
style->offsety = 0;
bindDoubleAttribute(&style->offsety, shape->values[style->bindings[MS_STYLE_BINDING_OFFSET_Y].index]);
}
if(style->bindings[MS_STYLE_BINDING_POLAROFFSET_PIXEL].index != -1) {
style->polaroffsetpixel = 0;
bindDoubleAttribute(&style->polaroffsetpixel, shape->values[style->bindings[MS_STYLE_BINDING_POLAROFFSET_PIXEL].index]);
}
if(style->bindings[MS_STYLE_BINDING_POLAROFFSET_ANGLE].index != -1) {
style->polaroffsetangle = 0;
bindDoubleAttribute(&style->polaroffsetangle, shape->values[style->bindings[MS_STYLE_BINDING_POLAROFFSET_ANGLE].index]);
}
}
if (style->nexprbindings > 0)
{
if (style->exprBindings[MS_STYLE_BINDING_OFFSET_X].type == MS_EXPRESSION)
{
style->offsetx = msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_OFFSET_X]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_OFFSET_Y].type == MS_EXPRESSION)
{
style->offsety = msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_OFFSET_Y]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_ANGLE].type == MS_EXPRESSION)
{
style->angle = msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_ANGLE]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_SIZE].type == MS_EXPRESSION)
{
style->size = msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_SIZE]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_WIDTH].type == MS_EXPRESSION)
{
style->width = msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_WIDTH]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_OPACITY].type == MS_EXPRESSION)
{
style->opacity = 100 * msEvalDoubleExpression(
&(style->exprBindings[MS_STYLE_BINDING_OPACITY]),
shape);
}
if (style->exprBindings[MS_STYLE_BINDING_OUTLINECOLOR].type == MS_EXPRESSION)
{
char* txt = msEvalTextExpression(
&(style->exprBindings[MS_STYLE_BINDING_OUTLINECOLOR]), shape);
bindColorAttribute(&style->outlinecolor, txt);
msFree(txt);
}
if (style->exprBindings[MS_STYLE_BINDING_COLOR].type == MS_EXPRESSION)
{
char* txt = msEvalTextExpression(
&(style->exprBindings[MS_STYLE_BINDING_COLOR]), shape);
bindColorAttribute(&style->color, txt);
msFree(txt);
}
}
if(style->opacity < 100 || style->color.alpha != 255 ) {
int alpha;
alpha = MS_NINT(style->opacity*2.55);
style->color.alpha = alpha;
style->outlinecolor.alpha = alpha;
style->backgroundcolor.alpha = alpha;
style->mincolor.alpha = alpha;
style->maxcolor.alpha = alpha;
}
}
static void bindLabel(layerObj *layer, shapeObj *shape, labelObj *label, int drawmode)
{
int i;
assert(MS_DRAW_LABELS(drawmode));
/* check the label styleObj's (TODO: do we need to use querymapMode here? */
for(i=0; i<label->numstyles; i++) {
/* force MS_DRAWMODE_FEATURES for label styles */
bindStyle(layer, shape, label->styles[i], drawmode|MS_DRAWMODE_FEATURES);
}
if(label->numbindings > 0) {
if(label->bindings[MS_LABEL_BINDING_ANGLE].index != -1) {
label->angle = 0.0;
bindDoubleAttribute(&label->angle, shape->values[label->bindings[MS_LABEL_BINDING_ANGLE].index]);
}
if(label->bindings[MS_LABEL_BINDING_SIZE].index != -1) {
label->size = 1;
bindIntegerAttribute(&label->size, shape->values[label->bindings[MS_LABEL_BINDING_SIZE].index]);
}
if(label->bindings[MS_LABEL_BINDING_COLOR].index != -1) {
MS_INIT_COLOR(label->color, -1,-1,-1,255);
bindColorAttribute(&label->color, shape->values[label->bindings[MS_LABEL_BINDING_COLOR].index]);
}
if(label->bindings[MS_LABEL_BINDING_OUTLINECOLOR].index != -1) {
MS_INIT_COLOR(label->outlinecolor, -1,-1,-1,255);
bindColorAttribute(&label->outlinecolor, shape->values[label->bindings[MS_LABEL_BINDING_OUTLINECOLOR].index]);
}
if(label->bindings[MS_LABEL_BINDING_FONT].index != -1) {
msFree(label->font);
label->font = msStrdup(shape->values[label->bindings[MS_LABEL_BINDING_FONT].index]);
}
if(label->bindings[MS_LABEL_BINDING_PRIORITY].index != -1) {
label->priority = MS_DEFAULT_LABEL_PRIORITY;
bindIntegerAttribute(&label->priority, shape->values[label->bindings[MS_LABEL_BINDING_PRIORITY].index]);
}
if(label->bindings[MS_LABEL_BINDING_SHADOWSIZEX].index != -1) {
label->shadowsizex = 1;
bindIntegerAttribute(&label->shadowsizex, shape->values[label->bindings[MS_LABEL_BINDING_SHADOWSIZEX].index]);
}
if(label->bindings[MS_LABEL_BINDING_SHADOWSIZEY].index != -1) {
label->shadowsizey = 1;
bindIntegerAttribute(&label->shadowsizey, shape->values[label->bindings[MS_LABEL_BINDING_SHADOWSIZEY].index]);
}
if(label->bindings[MS_LABEL_BINDING_POSITION].index != -1) {
int tmpPosition = 0;
bindIntegerAttribute(&tmpPosition, shape->values[label->bindings[MS_LABEL_BINDING_POSITION].index]);
if(tmpPosition != 0) { /* is this test sufficient? */
label->position = tmpPosition;
} else { /* Integer binding failed, look for strings like cc, ul, lr, etc... */
if(strlen(shape->values[label->bindings[MS_LABEL_BINDING_POSITION].index]) == 2) {
char *vp = shape->values[label->bindings[MS_LABEL_BINDING_POSITION].index];
if(!strncasecmp(vp,"ul",2))
label->position = MS_UL;
else if(!strncasecmp(vp,"lr",2))
label->position = MS_LR;
else if(!strncasecmp(vp,"ur",2))
label->position = MS_UR;
else if(!strncasecmp(vp,"ll",2))
label->position = MS_LL;
else if(!strncasecmp(vp,"cr",2))
label->position = MS_CR;
else if(!strncasecmp(vp,"cl",2))
label->position = MS_CL;
else if(!strncasecmp(vp,"uc",2))
label->position = MS_UC;
else if(!strncasecmp(vp,"lc",2))
label->position = MS_LC;
else if(!strncasecmp(vp,"cc",2))
label->position = MS_CC;
}
}
}
}
if (label->nexprbindings > 0)
{
if (label->exprBindings[MS_LABEL_BINDING_ANGLE].type == MS_EXPRESSION)
{
label->angle = msEvalDoubleExpression(
&(label->exprBindings[MS_LABEL_BINDING_ANGLE]),
shape);
}
if (label->exprBindings[MS_LABEL_BINDING_SIZE].type == MS_EXPRESSION)
{
label->size = msEvalDoubleExpression(
&(label->exprBindings[MS_LABEL_BINDING_SIZE]),
shape);
}
if (label->exprBindings[MS_LABEL_BINDING_COLOR].type == MS_EXPRESSION)
{
char* txt = msEvalTextExpression(
&(label->exprBindings[MS_LABEL_BINDING_COLOR]), shape);
bindColorAttribute(&label->color, txt);
msFree(txt);
}
if (label->exprBindings[MS_LABEL_BINDING_OUTLINECOLOR].type == MS_EXPRESSION)
{
char* txt = msEvalTextExpression(
&(label->exprBindings[MS_LABEL_BINDING_OUTLINECOLOR]), shape);
bindColorAttribute(&label->outlinecolor, txt);
msFree(txt);
}
}
}
/*
** Function to bind various layer properties to shape attributes.
*/
int msBindLayerToShape(layerObj *layer, shapeObj *shape, int drawmode)
{
int i, j;
if(!layer || !shape) return MS_FAILURE;
for(i=0; i<layer->numclasses; i++) {
/* check the styleObj's */
if(MS_DRAW_FEATURES(drawmode)) {
for(j=0; j<layer->class[i]->numstyles; j++) {
bindStyle(layer, shape, layer->class[i]->styles[j], drawmode);
}
}
/* check the labelObj's */
if(MS_DRAW_LABELS(drawmode)) {
for(j=0; j<layer->class[i]->numlabels; j++) {
bindLabel(layer, shape, layer->class[i]->labels[j], drawmode);
}
}
} /* next classObj */
return MS_SUCCESS;
}
/*
* Used to get red, green, blue integers separately based upon the color index
*/
int getRgbColor(mapObj *map,int i,int *r,int *g,int *b)
{
/* check index range */
int status=1;
*r=*g=*b=-1;
if ((i > 0 ) && (i <= map->palette.numcolors) ) {
*r=map->palette.colors[i-1].red;
*g=map->palette.colors[i-1].green;
*b=map->palette.colors[i-1].blue;
status=0;
}
return status;
}
static int searchContextForTag(mapObj *map, char **ltags, char *tag, char *context, int requires)
{
int i;
if(!context) return MS_FAILURE;
/* printf("\tin searchContextForTag, searching %s for %s\n", context, tag); */
if(strstr(context, tag) != NULL) return MS_SUCCESS; /* found the tag */
/* check referenced layers for the tag too */
for(i=0; i<map->numlayers; i++) {
if(strstr(context, ltags[i]) != NULL) { /* need to check this layer */
if(requires == MS_TRUE) {
if(searchContextForTag(map, ltags, tag, GET_LAYER(map, i)->requires, MS_TRUE) == MS_SUCCESS) return MS_SUCCESS;
} else {
if(searchContextForTag(map, ltags, tag, GET_LAYER(map, i)->labelrequires, MS_FALSE) == MS_SUCCESS) return MS_SUCCESS;
}
}
}
return MS_FAILURE;
}
/*
** Function to take a look at all layers with REQUIRES/LABELREQUIRES set to make sure there are no
** recursive context requirements set (e.g. layer1 requires layer2 and layer2 requires layer1). This
** is bug 1059.
*/
int msValidateContexts(mapObj *map)
{
int i;
char **ltags;
int status = MS_SUCCESS;
ltags = (char **) msSmallMalloc(map->numlayers*sizeof(char *));
for(i=0; i<map->numlayers; i++) {
if(GET_LAYER(map, i)->name == NULL) {
ltags[i] = msStrdup("[NULL]");
} else {
ltags[i] = (char *) msSmallMalloc(sizeof(char)*strlen(GET_LAYER(map, i)->name) + 3);
sprintf(ltags[i], "[%s]", GET_LAYER(map, i)->name);
}
}
/* check each layer's REQUIRES and LABELREQUIRES parameters */
for(i=0; i<map->numlayers; i++) {
/* printf("working on layer %s, looking for references to %s\n", GET_LAYER(map, i)->name, ltags[i]); */
if(searchContextForTag(map, ltags, ltags[i], GET_LAYER(map, i)->requires, MS_TRUE) == MS_SUCCESS) {
msSetError(MS_PARSEERR, "Recursion error found for REQUIRES parameter for layer %s.", "msValidateContexts", GET_LAYER(map, i)->name);
status = MS_FAILURE;
break;
}
if(searchContextForTag(map, ltags, ltags[i], GET_LAYER(map, i)->labelrequires, MS_FALSE) == MS_SUCCESS) {
msSetError(MS_PARSEERR, "Recursion error found for LABELREQUIRES parameter for layer %s.", "msValidateContexts", GET_LAYER(map, i)->name);
status = MS_FAILURE;
break;
}
/* printf("done layer %s\n", GET_LAYER(map, i)->name); */
}
/* clean up */
msFreeCharArray(ltags, map->numlayers);
return status;
}
int msEvalContext(mapObj *map, layerObj *layer, char *context)
{
int i, status;
char *tag=NULL;
expressionObj e;
parseObj p;
if(!context) return(MS_TRUE);
/* initialize a temporary expression (e) */
msInitExpression(&e);
e.string = msStrdup(context);
e.type = MS_EXPRESSION; /* todo */
for(i=0; i<map->numlayers; i++) { /* step through all the layers */
if(layer->index == i) continue; /* skip the layer in question */
if (GET_LAYER(map, i)->name == NULL) continue; /* Layer without name cannot be used in contexts */
tag = (char *)msSmallMalloc(sizeof(char)*strlen(GET_LAYER(map, i)->name) + 3);
sprintf(tag, "[%s]", GET_LAYER(map, i)->name);
if(strstr(e.string, tag)) {
if(msLayerIsVisible(map, (GET_LAYER(map, i))))
e.string = msReplaceSubstring(e.string, tag, "1");
else
e.string = msReplaceSubstring(e.string, tag, "0");
}
free(tag);
}
msTokenizeExpression(&e, NULL, NULL);
p.shape = NULL;
p.expr = &e;
p.expr->curtoken = p.expr->tokens; /* reset */
p.type = MS_PARSE_TYPE_BOOLEAN;
status = yyparse(&p);
msFreeExpression(&e);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse context", "msEvalContext");
return MS_FALSE; /* error in parse */
}
return p.result.intval;
}
/* msEvalExpression()
*
* Evaluates a mapserver expression for a given set of attribute values and
* returns the result of the expression (MS_TRUE or MS_FALSE)
* May also return MS_FALSE in case of parsing errors or invalid expressions
* (check the error stack if you care)
*
*/
int msEvalExpression(layerObj *layer, shapeObj *shape, expressionObj *expression, int itemindex)
{
if(MS_STRING_IS_NULL_OR_EMPTY(expression->string)) return MS_TRUE; /* NULL or empty expressions are ALWAYS true */
if(expression->native_string != NULL) return MS_TRUE; /* expressions that are evaluated natively are ALWAYS true */
switch(expression->type) {
case(MS_STRING):
if(itemindex == -1) {
msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()");
return MS_FALSE;
}
if(itemindex >= layer->numitems || itemindex >= shape->numvalues) {
msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()");
return MS_FALSE;
}
if(expression->flags & MS_EXP_INSENSITIVE) {
if(strcasecmp(expression->string, shape->values[itemindex]) == 0) return MS_TRUE; /* got a match */
} else {
if(strcmp(expression->string, shape->values[itemindex]) == 0) return MS_TRUE; /* got a match */
}
break;
case(MS_LIST):
if(itemindex == -1) {
msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()");
return MS_FALSE;
}
if(itemindex >= layer->numitems || itemindex >= shape->numvalues) {
msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()");
return MS_FALSE;
}
{
char *start,*end;
int value_len = strlen(shape->values[itemindex]);
start = expression->string;
while((end = strchr(start,',')) != NULL) {
if(value_len == end-start && !strncmp(start,shape->values[itemindex],end-start)) return MS_TRUE;
start = end+1;
}
if(!strcmp(start,shape->values[itemindex])) return MS_TRUE;
}
break;
case(MS_EXPRESSION): {
int status;
parseObj p;
p.shape = shape;
p.expr = expression;
p.expr->curtoken = p.expr->tokens; /* reset */
p.type = MS_PARSE_TYPE_BOOLEAN;
status = yyparse(&p);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse expression: %s", "msEvalExpression", expression->string);
return MS_FALSE;
}
return p.result.intval;
break;
}
case(MS_REGEX):
if(itemindex == -1) {
msSetError(MS_MISCERR, "Cannot evaluate expression, no item index defined.", "msEvalExpression()");
return MS_FALSE;
}
if(itemindex >= layer->numitems || itemindex >= shape->numvalues) {
msSetError(MS_MISCERR, "Invalid item index.", "msEvalExpression()");
return MS_FALSE;
}
if(MS_STRING_IS_NULL_OR_EMPTY(shape->values[itemindex]) == MS_TRUE) return MS_FALSE;
if(!expression->compiled) {
if(expression->flags & MS_EXP_INSENSITIVE) {
if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB|MS_REG_ICASE) != 0) { /* compile the expression */
msSetError(MS_REGEXERR, "Invalid regular expression.", "msEvalExpression()");
return MS_FALSE;
}
} else {
if(ms_regcomp(&(expression->regex), expression->string, MS_REG_EXTENDED|MS_REG_NOSUB) != 0) { /* compile the expression */
msSetError(MS_REGEXERR, "Invalid regular expression.", "msEvalExpression()");
return MS_FALSE;
}
}
expression->compiled = MS_TRUE;
}
if(ms_regexec(&(expression->regex), shape->values[itemindex], 0, NULL, 0) == 0) return MS_TRUE; /* got a match */
break;
}
return MS_FALSE;
}
int *msAllocateValidClassGroups(layerObj *lp, int *nclasses)
{
int *classgroup = NULL;
int nvalidclass = 0, i=0;
if (!lp || !lp->classgroup || lp->numclasses <=0 || !nclasses)
return NULL;
classgroup = (int *)msSmallMalloc(sizeof(int)*lp->numclasses);
nvalidclass = 0;
for (i=0; i<lp->numclasses; i++) {
if (lp->class[i]->group && strcasecmp(lp->class[i]->group, lp->classgroup) == 0) {
classgroup[nvalidclass] = i;
nvalidclass++;
}
}
if (nvalidclass > 0) {
classgroup = (int *)msSmallRealloc(classgroup, sizeof(int)*nvalidclass);
*nclasses = nvalidclass;
return classgroup;
}
if (classgroup)
msFree(classgroup);
return NULL;
}
int msShapeGetClass(layerObj *layer, mapObj *map, shapeObj *shape, int *classgroup, int numclasses)
{
return msShapeGetNextClass(-1, layer, map, shape, classgroup, numclasses);
}
int msShapeGetNextClass(int currentclass, layerObj *layer, mapObj *map,
shapeObj *shape, int *classgroup, int numclasses)
{
int i, iclass;
if (currentclass < 0)
currentclass = -1;
if (layer->numclasses > 0) {
if (classgroup == NULL || numclasses <=0)
numclasses = layer->numclasses;
for(i=currentclass+1; i<numclasses; i++) {
if (classgroup)
iclass = classgroup[i];
else
iclass = i;
if (iclass < 0 || iclass >= layer->numclasses)
continue; /* this should never happen but just in case */
if(map->scaledenom > 0) { /* verify scaledenom here */
if((layer->class[iclass]->maxscaledenom > 0) && (map->scaledenom > layer->class[iclass]->maxscaledenom))
continue; /* can skip this one, next class */
if((layer->class[iclass]->minscaledenom > 0) && (map->scaledenom <= layer->class[iclass]->minscaledenom))
continue; /* can skip this one, next class */
}
/* verify the minfeaturesize */
if ((shape->type == MS_SHAPE_LINE || shape->type == MS_SHAPE_POLYGON) && (layer->class[iclass]->minfeaturesize > 0)) {
double minfeaturesize = Pix2LayerGeoref(map, layer,
layer->class[iclass]->minfeaturesize);
if (msShapeCheckSize(shape, minfeaturesize) == MS_FALSE)
continue; /* skip this one, next class */
}
if(layer->class[iclass]->status != MS_DELETE && msEvalExpression(layer, shape, &(layer->class[iclass]->expression), layer->classitemindex) == MS_TRUE)
{
if (layer->class[iclass]->isfallback && currentclass != -1)
{
// Class is not applicable if it is flagged as fallback (<ElseFilter/> tag in SLD)
// but other classes have been applied before.
return -1;
}
else
{
return(iclass);
}
}
}
}
return(-1); /* no match */
}
static
char *msEvalTextExpressionInternal(expressionObj *expr, shapeObj *shape, int bJSonEscape)
{
char *result=NULL;
if(!expr->string) return result; /* nothing to do */
switch(expr->type) {
case(MS_STRING): {
char *target=NULL;
char *pszEscaped;
tokenListNodeObjPtr node=NULL;
tokenListNodeObjPtr nextNode=NULL;
result = msStrdup(expr->string);
node = expr->tokens;
if(node) {
while(node != NULL) {
nextNode = node->next;
if(node->token == MS_TOKEN_BINDING_DOUBLE || node->token == MS_TOKEN_BINDING_INTEGER || node->token == MS_TOKEN_BINDING_STRING || node->token == MS_TOKEN_BINDING_TIME) {
target = (char *) msSmallMalloc(strlen(node->tokenval.bindval.item) + 3);
sprintf(target, "[%s]", node->tokenval.bindval.item);
if( bJSonEscape )
pszEscaped = msEscapeJSonString(shape->values[node->tokenval.bindval.index]);
else
pszEscaped = msStrdup(shape->values[node->tokenval.bindval.index]);
result = msReplaceSubstring(result, target, pszEscaped);
msFree(pszEscaped);
msFree(target);
}
node = nextNode;
}
}
if(!strlen(result)) {
msFree(result);
result = NULL;
}
}
break;
case(MS_EXPRESSION): {
int status;
parseObj p;
p.shape = shape;
p.expr = expr;
p.expr->curtoken = p.expr->tokens; /* reset */
p.type = MS_PARSE_TYPE_STRING;
status = yyparse(&p);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to process text expression: %s", "msEvalTextExpression", expr->string);
return NULL;
}
result = p.result.strval;
break;
}
default:
break;
}
if(result && !strlen(result)) {
msFree(result);
result = NULL;
}
return result;
}
char *msEvalTextExpressionJSonEscape(expressionObj *expr, shapeObj *shape)
{
return msEvalTextExpressionInternal(expr, shape, MS_TRUE);
}
char *msEvalTextExpression(expressionObj *expr, shapeObj *shape)
{
return msEvalTextExpressionInternal(expr, shape, MS_FALSE);
}
double msEvalDoubleExpression(expressionObj *expression, shapeObj *shape)
{
double value;
int status;
parseObj p;
p.shape = shape;
p.expr = expression;
p.expr->curtoken = p.expr->tokens; /* reset */
p.type = MS_PARSE_TYPE_STRING;
status = yyparse(&p);
if (status != 0) {
msSetError(MS_PARSEERR, "Failed to parse expression: %s",
"bindStyle", expression->string);
value = 0.0;
} else {
value = atof(p.result.strval);
msFree(p.result.strval);
}
return value;
}
char* msShapeGetLabelAnnotation(layerObj *layer, shapeObj *shape, labelObj *lbl) {
assert(shape && lbl);
if(lbl->text.string) {
return msEvalTextExpression(&(lbl->text), shape);
} else if(layer->class[shape->classindex]->text.string) {
return msEvalTextExpression(&(layer->class[shape->classindex]->text), shape);
} else {
if (shape->values && layer->labelitemindex >= 0 && shape->values[layer->labelitemindex] && strlen(shape->values[layer->labelitemindex]) )
return msStrdup(shape->values[layer->labelitemindex]);
else if(shape->text)
return msStrdup(shape->text); /* last resort but common with iniline features */
}
return NULL;
}
int msGetLabelStatus(mapObj *map, layerObj *layer, shapeObj *shape, labelObj *lbl) {
assert(layer && lbl);
if(!msScaleInBounds(map->scaledenom,lbl->minscaledenom,lbl->maxscaledenom))
return MS_OFF;
if(msEvalExpression(layer, shape, &(lbl->expression), layer->labelitemindex) != MS_TRUE)
return MS_OFF;
/* TODO: check for minfeaturesize here ? */
return MS_ON;
}
/* Check if the shape is enough big to be drawn with the
layer::minfeaturesize setting. The minfeaturesize parameter should be
the value in geo ref (not in pixel) and should have been multiplied by
the resolution factor.
*/
int msShapeCheckSize(shapeObj *shape, double minfeaturesize)
{
double dx = (shape->bounds.maxx-shape->bounds.minx);
double dy = (shape->bounds.maxy-shape->bounds.miny);
if (pow(minfeaturesize,2.0) > (pow(dx,2.0)+pow(dy,2.0)))
return MS_FALSE;
return MS_TRUE;
}
/*
** Adjusts an image size in one direction to fit an extent.
*/
int msAdjustImage(rectObj rect, int *width, int *height)
{
if(*width == -1 && *height == -1) {
msSetError(MS_MISCERR, "Cannot calculate both image height and width.", "msAdjustImage()");
return(-1);
}
if(*width > 0)
*height = MS_NINT((rect.maxy - rect.miny)/((rect.maxx - rect.minx)/(*width)));
else
*width = MS_NINT((rect.maxx - rect.minx)/((rect.maxy - rect.miny)/(*height)));
return(0);
}
/*
** Make sure extent fits image window to be created. Returns cellsize of output image.
*/
double msAdjustExtent(rectObj *rect, int width, int height)
{
double cellsize, ox, oy;
if(width == 1 || height == 1)
return 0;
cellsize = MS_MAX(MS_CELLSIZE(rect->minx, rect->maxx, width), MS_CELLSIZE(rect->miny, rect->maxy, height));
if(cellsize <= 0) /* avoid division by zero errors */
return(0);
ox = MS_MAX(((width-1) - (rect->maxx - rect->minx)/cellsize)/2,0); /* these were width-1 and height-1 */
oy = MS_MAX(((height-1) - (rect->maxy - rect->miny)/cellsize)/2,0);
rect->minx = rect->minx - ox*cellsize;
rect->miny = rect->miny - oy*cellsize;
rect->maxx = rect->maxx + ox*cellsize;
rect->maxy = rect->maxy + oy*cellsize;
return(cellsize);
}
/*
** Rect must always contain a portion of bounds. If not, rect is
** shifted to overlap by overlay percent. The dimensions of rect do
** not change but placement relative to bounds can.
*/
int msConstrainExtent(rectObj *bounds, rectObj *rect, double overlay)
{
double offset=0;
/* check left edge, and if necessary the right edge of bounds */
if(rect->maxx <= bounds->minx) {
offset = overlay*(rect->maxx - rect->minx);
rect->minx += offset; /* shift right */
rect->maxx += offset;
} else if(rect->minx >= bounds->maxx) {
offset = overlay*(rect->maxx - rect->minx);
rect->minx -= offset; /* shift left */
rect->maxx -= offset;
}
/* check top edge, and if necessary the bottom edge of bounds */
if(rect->maxy <= bounds->miny) {
offset = overlay*(rect->maxy - rect->miny);
rect->miny -= offset; /* shift down */
rect->maxy -= offset;
} else if(rect->miny >= bounds->maxy) {
offset = overlay*(rect->maxy - rect->miny);
rect->miny += offset; /* shift up */
rect->maxy += offset;
}
return(MS_SUCCESS);
}
/*
** Generic function to save an image to a file.
**
** Note that map may be NULL. If it is set, then it is used for two things:
** - Deal with relative imagepaths (compute absolute path relative to map path)
** - Extract the georeferenced extents and coordinate system
** of the map for writing out with the image when appropriate
** (primarily this means via msSaveImageGDAL() to something like GeoTIFF).
**
** The filename is NULL when the image is supposed to be written to stdout.
*/
int msSaveImage(mapObj *map, imageObj *img, const char *filename)
{
int nReturnVal = MS_FAILURE;
char szPath[MS_MAXPATHLEN];
struct mstimeval starttime={0}, endtime={0};
if(map && map->debug >= MS_DEBUGLEVEL_TUNING) {
msGettimeofday(&starttime, NULL);
}
if (img) {
if( MS_DRIVER_GDAL(img->format) ) {
if (map != NULL && filename != NULL )
nReturnVal = msSaveImageGDAL(map, img,
msBuildPath(szPath, map->mappath,
filename));
else
nReturnVal = msSaveImageGDAL(map, img, filename);
} else
if (MS_RENDERER_PLUGIN(img->format)) {
rendererVTableObj *renderer = img->format->vtable;
FILE *stream = NULL;
if(filename) {
if(map)
stream = fopen(msBuildPath(szPath, map->mappath, filename),"wb");
else
stream = fopen(filename,"wb");
if(!stream) {
msSetError(MS_IOERR,
"Failed to create output file (%s).",
"msSaveImage()", (map?szPath:filename) );
return MS_FAILURE;
}
} else {
if ( msIO_needBinaryStdout() == MS_FAILURE )
return MS_FAILURE;
stream = stdout;
}
if(renderer->supports_pixel_buffer) {
rasterBufferObj data;
if(renderer->getRasterBufferHandle(img,&data) != MS_SUCCESS) {
if( stream != stdout )
fclose(stream);
return MS_FAILURE;
}
nReturnVal = msSaveRasterBuffer(map,&data,stream,img->format );