forked from MapServer/MapServer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapogcfiltercommon.c
726 lines (618 loc) · 24.8 KB
/
mapogcfiltercommon.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
/**********************************************************************
* $Id$
*
* Project: MapServer
* Purpose: OGC Filter Encoding implementation
* Author: Y. Assefa, DM Solutions Group ([email protected])
*
**********************************************************************
* Copyright (c) 2003, Y. Assefa, DM Solutions Group Inc
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies of this Software or works derived from this Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
****************************************************************************/
#ifdef USE_OGR
#include "cpl_minixml.h"
#endif
#include "mapogcfilter.h"
#include "mapserver.h"
#include "mapows.h"
#include "mapowscommon.h"
#ifdef USE_OGR
char *FLTGetIsLikeComparisonCommonExpression(FilterEncodingNode *psFilterNode)
{
const size_t bufferSize = 1024;
char szBuffer[1024];
char szTmp[256];
char *pszValue = NULL;
const char *pszWild = NULL;
const char *pszSingle = NULL;
const char *pszEscape = NULL;
int bCaseInsensitive = 0;
FEPropertyIsLike* propIsLike;
int nLength=0, i=0, iTmp=0;
if (!psFilterNode || !psFilterNode->pOther || !psFilterNode->psLeftNode || !psFilterNode->psRightNode || !psFilterNode->psRightNode->pszValue)
return NULL;
propIsLike = (FEPropertyIsLike *)psFilterNode->pOther;
pszWild = propIsLike->pszWildCard;
pszSingle = propIsLike->pszSingleChar;
pszEscape = propIsLike->pszEscapeChar;
bCaseInsensitive = propIsLike->bCaseInsensitive;
if (!pszWild || strlen(pszWild) == 0 || !pszSingle || strlen(pszSingle) == 0 || !pszEscape || strlen(pszEscape) == 0)
return NULL;
/* -------------------------------------------------------------------- */
/* Use operand with regular expressions. */
/* -------------------------------------------------------------------- */
szBuffer[0] = '\0';
sprintf(szTmp, "%s", " (\"[");
szTmp[4] = '\0';
strlcat(szBuffer, szTmp, bufferSize);
/* attribute */
strlcat(szBuffer, psFilterNode->psLeftNode->pszValue, bufferSize);
szBuffer[strlen(szBuffer)] = '\0';
/* #3521 */
if (bCaseInsensitive == 1)
sprintf(szTmp, "%s", "]\" ~* \"");
else
sprintf(szTmp, "%s", "]\" ~ \"");
szTmp[7] = '\0';
strlcat(szBuffer, szTmp, bufferSize);
szBuffer[strlen(szBuffer)] = '\0';
pszValue = psFilterNode->psRightNode->pszValue;
nLength = strlen(pszValue);
iTmp =0;
if (nLength > 0 && pszValue[0] != pszWild[0] && pszValue[0] != pszSingle[0] && pszValue[0] != pszEscape[0]) {
szTmp[iTmp]= '^';
iTmp++;
}
for (i=0; i<nLength; i++) {
if (pszValue[i] != pszWild[0] && pszValue[i] != pszSingle[0] && pszValue[i] != pszEscape[0]) {
szTmp[iTmp] = pszValue[i];
iTmp++;
szTmp[iTmp] = '\0';
} else if (pszValue[i] == pszSingle[0]) {
szTmp[iTmp] = '.';
iTmp++;
szTmp[iTmp] = '\0';
} else if (pszValue[i] == pszEscape[0]) {
szTmp[iTmp] = '\\';
iTmp++;
szTmp[iTmp] = '\0';
} else if (pszValue[i] == pszWild[0]) {
szTmp[iTmp++] = '.';
szTmp[iTmp++] = '*';
szTmp[iTmp] = '\0';
}
}
szTmp[iTmp] = '"';
szTmp[++iTmp] = '\0';
strlcat(szBuffer, szTmp, bufferSize);
strlcat(szBuffer, ")", bufferSize);
return msStrdup(szBuffer);
}
char *FLTGetIsBetweenComparisonCommonExpresssion(FilterEncodingNode *psFilterNode, layerObj *lp)
{
const size_t bufferSize = 1024;
char szBuffer[1024];
char **aszBounds = NULL;
int nBounds = 0;
int bString=0;
int bDateTime = 0;
char *pszExpression=NULL, *pszTmpEscaped;
if (!psFilterNode || !(strcasecmp(psFilterNode->pszValue, "PropertyIsBetween") == 0))
return NULL;
if (psFilterNode->psLeftNode == NULL || psFilterNode->psRightNode == NULL )
return NULL;
/* -------------------------------------------------------------------- */
/* Get the bounds value which are stored like boundmin;boundmax */
/* -------------------------------------------------------------------- */
aszBounds = msStringSplit(psFilterNode->psRightNode->pszValue, ';', &nBounds);
if (nBounds != 2) {
msFreeCharArray(aszBounds, nBounds);
return NULL;
}
/* -------------------------------------------------------------------- */
/* check if the value is a numeric value or alphanumeric. If it */
/* is alphanumeric, add quotes around attribute and values. */
/* -------------------------------------------------------------------- */
bString = 0;
if (aszBounds[0]) {
const char* pszType;
snprintf(szBuffer, bufferSize, "%s_type", psFilterNode->psLeftNode->pszValue);
pszType = msOWSLookupMetadata(&(lp->metadata), "OFG", szBuffer);
if (pszType != NULL && (strcasecmp(pszType, "Character") == 0))
bString = 1;
else if (pszType != NULL && (strcasecmp(pszType, "Date") == 0))
bDateTime = 1;
else if (FLTIsNumeric(aszBounds[0]) == MS_FALSE)
bString = 1;
}
if (!bString && !bDateTime) {
if (aszBounds[1]) {
if (FLTIsNumeric(aszBounds[1]) == MS_FALSE)
bString = 1;
}
}
/* -------------------------------------------------------------------- */
/* build expresssion. */
/* -------------------------------------------------------------------- */
/* attribute */
if (bString)
sprintf(szBuffer, "%s", " (\"[");
else
sprintf(szBuffer, "%s", " ([");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, psFilterNode->psLeftNode->pszValue);
if (bString)
sprintf(szBuffer, "%s", "]\" ");
else
sprintf(szBuffer, "%s", "] ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
sprintf(szBuffer, "%s", " >= ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
if (bString) {
pszExpression = msStringConcatenate(pszExpression, "\"");
}
else if (bDateTime) {
pszExpression = msStringConcatenate(pszExpression, "`");
}
pszTmpEscaped = msStringEscape(aszBounds[0]);
snprintf(szBuffer, bufferSize, "%s", pszTmpEscaped);
if(pszTmpEscaped != aszBounds[0] ) msFree(pszTmpEscaped);
pszExpression = msStringConcatenate(pszExpression, szBuffer);
if (bString) {
pszExpression = msStringConcatenate(pszExpression, "\"");
}
else if (bDateTime) {
pszExpression = msStringConcatenate(pszExpression, "`");
}
sprintf(szBuffer, "%s", " AND ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
if (bString)
sprintf(szBuffer, "%s", " \"[");
else
sprintf(szBuffer, "%s", " [");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
/* attribute */
pszExpression = msStringConcatenate(pszExpression, psFilterNode->psLeftNode->pszValue);
if (bString)
sprintf(szBuffer, "%s", "]\" ");
else
sprintf(szBuffer, "%s", "] ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
sprintf(szBuffer, "%s", " <= ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
if (bString) {
pszExpression = msStringConcatenate(pszExpression, "\"");
}
else if (bDateTime) {
pszExpression = msStringConcatenate(pszExpression, "`");
}
pszTmpEscaped = msStringEscape(aszBounds[1]);
snprintf(szBuffer, bufferSize, "%s", pszTmpEscaped);
if (pszTmpEscaped != aszBounds[1]) msFree(pszTmpEscaped);
pszExpression = msStringConcatenate(pszExpression, szBuffer);
if (bString) {
pszExpression = msStringConcatenate(pszExpression, "\"");
}
else if (bDateTime) {
pszExpression = msStringConcatenate(pszExpression, "`");
}
sprintf(szBuffer, "%s", ")");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
msFreeCharArray(aszBounds, nBounds);
return pszExpression;
}
char *FLTGetBinaryComparisonCommonExpression(FilterEncodingNode *psFilterNode, layerObj *lp)
{
char szTmp[1024];
char *pszExpression = NULL, *pszTmpEscaped;
int bString;
int bDateTime;
if (psFilterNode == NULL)
return NULL;
/* -------------------------------------------------------------------- */
/* check if the value is a numeric value or alphanumeric. If it */
/* is alphanumeric, add quotes around attribute and values. */
/* -------------------------------------------------------------------- */
bString = 0;
bDateTime = 0;
if (psFilterNode->psRightNode->pszValue) {
const char* pszType;
snprintf(szTmp, sizeof(szTmp), "%s_type", psFilterNode->psLeftNode->pszValue);
pszType = msOWSLookupMetadata(&(lp->metadata), "OFG", szTmp);
if (pszType!= NULL && (strcasecmp(pszType, "Character") == 0))
bString = 1;
else if (pszType!= NULL && (strcasecmp(pszType, "Date") == 0))
bDateTime = 1;
else if (FLTIsNumeric(psFilterNode->psRightNode->pszValue) == MS_FALSE)
bString = 1;
}
/* specical case to be able to have empty strings in the expression. */
/* propertyislike is always treated as string */
if (psFilterNode->psRightNode->pszValue == NULL || strcasecmp(psFilterNode->pszValue, "PropertyIsLike") == 0)
bString = 1;
/* attribute */
if (bString)
sprintf(szTmp, "%s", " (\"[");
else
sprintf(szTmp, "%s"," ([");
pszExpression = msStringConcatenate(pszExpression, szTmp);
pszExpression = msStringConcatenate(pszExpression, psFilterNode->psLeftNode->pszValue);
if (bString)
sprintf(szTmp, "%s","]\" ");
else
sprintf(szTmp, "%s", "] ");
pszExpression = msStringConcatenate(pszExpression, szTmp);
if (strcasecmp(psFilterNode->pszValue, "PropertyIsEqualTo") == 0) {
/* case insensitive set ? */
if (psFilterNode->psRightNode->pOther && (*(int *)psFilterNode->psRightNode->pOther) == 1)
sprintf(szTmp, "%s", "=*");
else
sprintf(szTmp, "%s", "=");
} else if (strcasecmp(psFilterNode->pszValue, "PropertyIsNotEqualTo") == 0)
sprintf(szTmp, "%s", " != ");
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsLessThan") == 0)
sprintf(szTmp, "%s", " < ");
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsGreaterThan") == 0)
sprintf(szTmp, "%s", " > ");
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsLessThanOrEqualTo") == 0)
sprintf(szTmp, "%s", " <= ");
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsGreaterThanOrEqualTo") == 0)
sprintf(szTmp, "%s", " >= ");
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsLike") == 0)
sprintf(szTmp, "%s", " ~ ");
pszExpression = msStringConcatenate(pszExpression, szTmp);
/* value */
if (bString) {
sprintf(szTmp, "%s", "\"");
pszExpression = msStringConcatenate(pszExpression, szTmp);
}
else if (bDateTime) {
sprintf(szTmp, "%s", "`");
pszExpression = msStringConcatenate(pszExpression, szTmp);
}
if (psFilterNode->psRightNode->pszValue) {
pszTmpEscaped = msStringEscape(psFilterNode->psRightNode->pszValue);
pszExpression = msStringConcatenate(pszExpression, pszTmpEscaped);
if(pszTmpEscaped != psFilterNode->psRightNode->pszValue ) msFree(pszTmpEscaped);
}
if (bString) {
sprintf(szTmp, "%s", "\"");
pszExpression = msStringConcatenate(pszExpression, szTmp);
}
else if (bDateTime) {
sprintf(szTmp, "%s", "`");
pszExpression = msStringConcatenate(pszExpression, szTmp);
}
sprintf(szTmp, "%s", ")");
pszExpression = msStringConcatenate(pszExpression, szTmp);
return pszExpression;
}
char *FLTGetLogicalComparisonCommonExpression(FilterEncodingNode *psFilterNode, layerObj *lp)
{
char *pszExpression = NULL;
char *pszTmp = NULL;
char szBuffer[256];
if (!psFilterNode || !FLTIsLogicalFilterType(psFilterNode->pszValue))
return NULL;
/* -------------------------------------------------------------------- */
/* OR and AND */
/* -------------------------------------------------------------------- */
if (psFilterNode->psLeftNode && psFilterNode->psRightNode) {
pszTmp = FLTGetCommonExpression(psFilterNode->psLeftNode, lp);
if (!pszTmp)
return NULL;
sprintf(szBuffer, "%s", " (");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, pszTmp);
msFree(pszTmp);
sprintf(szBuffer, "%s", " ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, psFilterNode->pszValue);
sprintf(szBuffer, "%s", " ");
pszTmp = FLTGetCommonExpression(psFilterNode->psRightNode, lp);
if (!pszTmp) {
msFree(pszExpression);
return NULL;
}
pszExpression = msStringConcatenate(pszExpression, pszTmp);
msFree(pszTmp);
sprintf(szBuffer, "%s", ") ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
}
/* -------------------------------------------------------------------- */
/* NOT */
/* -------------------------------------------------------------------- */
else if (psFilterNode->psLeftNode && strcasecmp(psFilterNode->pszValue, "NOT") == 0) {
pszTmp = FLTGetCommonExpression(psFilterNode->psLeftNode, lp);
if (!pszTmp)
return NULL;
sprintf(szBuffer, "%s", " (NOT ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, pszTmp);
msFree(pszTmp);
sprintf(szBuffer, "%s", ") ");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
}
return pszExpression;
}
char *FLTGetSpatialComparisonCommonExpression(FilterEncodingNode *psNode, layerObj *lp)
{
char *pszExpression = NULL;
shapeObj *psQueryShape = NULL;
double dfDistance = -1;
int nUnit = -1, nLayerUnit = -1;
char *pszWktText = NULL;
char szBuffer[256];
char *pszTmp=NULL;
projectionObj sProjTmp;
rectObj sQueryRect;
shapeObj *psTmpShape=NULL;
int bBBoxQuery = 0;
int bAlreadyReprojected = 0;
if (psNode == NULL || lp == NULL)
return NULL;
if (psNode->eType != FILTER_NODE_TYPE_SPATIAL)
return NULL;
/* get the shape */
if (FLTIsBBoxFilter(psNode)) {
char szPolygon[512];
FLTGetBBOX(psNode, &sQueryRect);
snprintf(szPolygon, sizeof(szPolygon),
"POLYGON((%.18f %.18f,%.18f %.18f,%.18f %.18f,%.18f %.18f,%.18f %.18f))",
sQueryRect.minx, sQueryRect.miny,
sQueryRect.minx, sQueryRect.maxy,
sQueryRect.maxx, sQueryRect.maxy,
sQueryRect.maxx, sQueryRect.miny,
sQueryRect.minx, sQueryRect.miny);
psTmpShape = msShapeFromWKT(szPolygon);
/*
** This is a horrible hack to deal with world-extent requests and
** reprojection. msProjectRect() detects if reprojection from longlat to
** projected SRS, and in that case it transforms the bbox to -1e-15,-1e-15,1e15,1e15
** to ensure that all features are returned.
**
** Make wfs_200_cite_filter_bbox_world.xml and wfs_200_cite_postgis_bbox_world.xml pass
*/
if (fabs(sQueryRect.minx - -180.0) < 1e-5 &&
fabs(sQueryRect.miny - -90.0) < 1e-5 &&
fabs(sQueryRect.maxx - 180.0) < 1e-5 &&
fabs(sQueryRect.maxy - 90.0) < 1e-5)
{
if (lp->projection.numargs > 0) {
if (psNode->pszSRS)
msInitProjection(&sProjTmp);
if (psNode->pszSRS) {
/* Use the non EPSG variant since axis swapping is done in FLTDoAxisSwappingIfNecessary */
if (msLoadProjectionString(&sProjTmp, psNode->pszSRS) == 0) {
msProjectRect(&sProjTmp, &lp->projection, &sQueryRect);
}
} else if (lp->map->projection.numargs > 0)
msProjectRect(&lp->map->projection, &lp->projection, &sQueryRect);
if (psNode->pszSRS)
msFreeProjection(&sProjTmp);
}
if (sQueryRect.minx <= -1e14) {
msFreeShape(psTmpShape);
msFree(psTmpShape);
psTmpShape = (shapeObj*) msSmallMalloc(sizeof(shapeObj));
msInitShape(psTmpShape);
msRectToPolygon(sQueryRect, psTmpShape);
bAlreadyReprojected = 1;
}
}
bBBoxQuery = 1;
} else {
/* other geos type operations */
/* project shape to layer projection. If the proj is not part of the filter query,
assume that the cooredinates are in the map projection */
psQueryShape = FLTGetShape(psNode, &dfDistance, &nUnit);
if ((strcasecmp(psNode->pszValue, "DWithin") == 0 || strcasecmp(psNode->pszValue, "Beyond") == 0 ) && dfDistance > 0) {
nLayerUnit = lp->units;
if(nLayerUnit == -1) nLayerUnit = GetMapserverUnitUsingProj(&lp->projection);
if(nLayerUnit == -1) nLayerUnit = lp->map->units;
if(nLayerUnit == -1) nLayerUnit = GetMapserverUnitUsingProj(&lp->map->projection);
if (nUnit >= 0 && nUnit != nLayerUnit)
dfDistance *= msInchesPerUnit(nUnit,0)/msInchesPerUnit(nLayerUnit,0); /* target is layer units */
}
psTmpShape = psQueryShape;
}
if (psTmpShape) {
/*
** target is layer projection
*/
if (!bAlreadyReprojected && lp->projection.numargs > 0) {
if (psNode->pszSRS)
msInitProjection(&sProjTmp);
if (psNode->pszSRS) {
/* Use the non EPSG variant since axis swapping is done in FLTDoAxisSwappingIfNecessary */
if (msLoadProjectionString(&sProjTmp, psNode->pszSRS) == 0) {
msProjectShape(&sProjTmp, &lp->projection, psTmpShape);
}
} else if (lp->map->projection.numargs > 0)
msProjectShape(&lp->map->projection, &lp->projection, psTmpShape);
if (psNode->pszSRS)
msFreeProjection(&sProjTmp);
}
/* function name */
if (bBBoxQuery) {
sprintf(szBuffer, "%s", "intersects");
} else {
if (strncasecmp(psNode->pszValue, "intersect", 9) == 0)
sprintf(szBuffer, "%s", "intersects");
else {
pszTmp = msStrdup(psNode->pszValue);
msStringToLower(pszTmp);
sprintf(szBuffer, "%s", pszTmp);
msFree(pszTmp);
}
}
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, "(");
/* geometry binding */
sprintf(szBuffer, "%s", "[shape]");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, ",");
/* filter geometry */
pszWktText = msGEOSShapeToWKT(psTmpShape);
sprintf(szBuffer, "%s", "fromText('");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
pszExpression = msStringConcatenate(pszExpression, pszWktText);
sprintf(szBuffer, "%s", "')");
pszExpression = msStringConcatenate(pszExpression, szBuffer);
msGEOSFreeWKT(pszWktText);
/* (optional) beyond/dwithin distance, always 0.0 since we apply the distance as a buffer earlier */
if ((strcasecmp(psNode->pszValue, "DWithin") == 0 || strcasecmp(psNode->pszValue, "Beyond") == 0)) {
// pszExpression = msStringConcatenate(pszExpression, ",0.0");
sprintf(szBuffer, ",%g", dfDistance);
pszExpression = msStringConcatenate(pszExpression, szBuffer);
}
/* terminate the function */
pszExpression = msStringConcatenate(pszExpression, ") = TRUE");
}
/*
** Cleanup
*/
if (bBBoxQuery) {
msFreeShape(psTmpShape);
msFree(psTmpShape);
}
return pszExpression;
}
char *FLTGetFeatureIdCommonExpression(FilterEncodingNode *psFilterNode, layerObj *lp)
{
char *pszExpression = NULL;
int nTokens = 0, i=0, bString=0;
char **tokens = NULL;
const char *pszAttribute=NULL;
#if defined(USE_WMS_SVR) || defined(USE_WFS_SVR) || defined(USE_WCS_SVR) || defined(USE_SOS_SVR)
if (psFilterNode->pszValue) {
pszAttribute = msOWSLookupMetadata(&(lp->metadata), "OFG", "featureid");
if (pszAttribute) {
tokens = msStringSplit(psFilterNode->pszValue,',', &nTokens);
if (tokens && nTokens > 0) {
for (i=0; i<nTokens; i++) {
char *pszTmp = NULL;
int bufferSize = 0;
const char* pszId = tokens[i];
const char* pszDot = strchr(pszId, '.');
if( pszDot )
pszId = pszDot + 1;
if (i == 0) {
if(FLTIsNumeric(pszId) == MS_FALSE)
bString = 1;
}
if (bString) {
bufferSize = 11+strlen(pszId)+strlen(pszAttribute)+1;
pszTmp = (char *)msSmallMalloc(bufferSize);
snprintf(pszTmp, bufferSize, "(\"[%s]\" ==\"%s\")" , pszAttribute, pszId);
} else {
bufferSize = 8+strlen(pszId)+strlen(pszAttribute)+1;
pszTmp = (char *)msSmallMalloc(bufferSize);
snprintf(pszTmp, bufferSize, "([%s] == %s)" , pszAttribute, pszId);
}
if (pszExpression != NULL)
pszExpression = msStringConcatenate(pszExpression, " OR ");
else
pszExpression = msStringConcatenate(pszExpression, "(");
pszExpression = msStringConcatenate(pszExpression, pszTmp);
msFree(pszTmp);
}
msFreeCharArray(tokens, nTokens);
}
}
/* opening and closing brackets are needed for mapserver expressions */
if (pszExpression)
pszExpression = msStringConcatenate(pszExpression, ")");
}
#endif
return pszExpression;
}
char* FLTGetTimeExpression(FilterEncodingNode *psFilterNode, layerObj *lp)
{
char* pszExpression = NULL;
const char* pszTimeField;
const char* pszTimeValue;
if (psFilterNode == NULL || lp == NULL)
return NULL;
if (psFilterNode->eType != FILTER_NODE_TYPE_TEMPORAL)
return NULL;
pszTimeValue = FLTGetDuring(psFilterNode, &pszTimeField);
if (pszTimeField && pszTimeValue) {
expressionObj old_filter;
msInitExpression(&old_filter);
msCopyExpression(&old_filter, &lp->filter); /* save existing filter */
msFreeExpression(&lp->filter);
if (msLayerSetTimeFilter(lp, pszTimeValue, pszTimeField) == MS_TRUE) {
pszExpression = msStrdup(lp->filter.string);
}
msCopyExpression(&lp->filter, &old_filter); /* restore old filter */
msFreeExpression(&old_filter);
}
return pszExpression;
}
char *FLTGetCommonExpression(FilterEncodingNode *psFilterNode, layerObj *lp)
{
char *pszExpression = NULL;
if (!psFilterNode)
return NULL;
if (psFilterNode->eType == FILTER_NODE_TYPE_COMPARISON) {
if ( psFilterNode->psLeftNode && psFilterNode->psRightNode) {
if (FLTIsBinaryComparisonFilterType(psFilterNode->pszValue))
pszExpression = FLTGetBinaryComparisonCommonExpression(psFilterNode, lp);
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsLike") == 0)
pszExpression = FLTGetIsLikeComparisonCommonExpression(psFilterNode);
else if (strcasecmp(psFilterNode->pszValue, "PropertyIsBetween") == 0)
pszExpression = FLTGetIsBetweenComparisonCommonExpresssion(psFilterNode, lp);
}
} else if (psFilterNode->eType == FILTER_NODE_TYPE_LOGICAL) {
pszExpression = FLTGetLogicalComparisonCommonExpression(psFilterNode, lp);
} else if (psFilterNode->eType == FILTER_NODE_TYPE_SPATIAL) {
pszExpression = FLTGetSpatialComparisonCommonExpression(psFilterNode, lp);
} else if (psFilterNode->eType == FILTER_NODE_TYPE_FEATUREID) {
pszExpression = FLTGetFeatureIdCommonExpression(psFilterNode, lp);
} else if (psFilterNode->eType == FILTER_NODE_TYPE_TEMPORAL) {
pszExpression = FLTGetTimeExpression(psFilterNode, lp);
}
return pszExpression;
}
int FLTApplyFilterToLayerCommonExpression(mapObj *map, int iLayerIndex, const char *pszExpression)
{
int retval;
int save_startindex;
int save_maxfeatures;
int save_only_cache_result_count;
save_startindex = map->query.startindex;
save_maxfeatures = map->query.maxfeatures;
save_only_cache_result_count = map->query.only_cache_result_count;
msInitQuery(&(map->query));
map->query.startindex = save_startindex;
map->query.maxfeatures = save_maxfeatures;
map->query.only_cache_result_count = save_only_cache_result_count;
map->query.type = MS_QUERY_BY_FILTER;
map->query.mode = MS_QUERY_MULTIPLE;
msInitExpression(&map->query.filter);
map->query.filter.string = msStrdup(pszExpression);
map->query.filter.type = MS_EXPRESSION; /* a logical expression */
map->query.layer = iLayerIndex;
/* TODO: if there is a bbox in the node, get it and set the map extent (projected to map->projection */
map->query.rect = map->extent;
retval = msQueryByFilter(map);
return retval;
}
#endif