forked from szekerest/mapserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mapogcfilter.c
3432 lines (2921 loc) · 130 KB
/
mapogcfilter.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**********************************************************************
* $Id$
*
* Project: MapServer
* Purpose: OGC 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
****************************************************************************/
#define _GNU_SOURCE
#include "mapserver-config.h"
#ifdef USE_OGR
#include "cpl_minixml.h"
#include "cpl_string.h"
#endif
#include "mapogcfilter.h"
#include "mapserver.h"
#include "mapowscommon.h"
int FLTIsNumeric(char *pszValue)
{
if (pszValue != NULL && *pszValue != '\0' && !isspace(*pszValue)) {
/*the regex seems to have a problem on windows when mapserver is built using
PHP regex*/
#if defined(_WIN32) && !defined(__CYGWIN__)
int i = 0, nLength=0, bString=0;
nLength = strlen(pszValue);
for (i=0; i<nLength; i++) {
if (i == 0) {
if (!isdigit(pszValue[i]) && pszValue[i] != '-') {
bString = 1;
break;
}
} else if (!isdigit(pszValue[i]) && pszValue[i] != '.') {
bString = 1;
break;
}
}
if (!bString)
return MS_TRUE;
#else
char * p;
strtod (pszValue, &p);
if (*p == '\0') return MS_TRUE;
#endif
}
return MS_FALSE;
}
/*
** Apply an expression to the layer's filter element.
**
*/
int FLTApplyExpressionToLayer(layerObj *lp, char *pszExpression)
{
char *pszFinalExpression=NULL, *pszBuffer = NULL;
/*char *escapedTextString=NULL;*/
int bConcatWhere=0, bHasAWhere=0;
if (lp && pszExpression) {
if (lp->connectiontype == MS_POSTGIS || lp->connectiontype == MS_ORACLESPATIAL ||
lp->connectiontype == MS_SDE || lp->connectiontype == MS_PLUGIN) {
pszFinalExpression = msStrdup("(");
pszFinalExpression = msStringConcatenate(pszFinalExpression, pszExpression);
pszFinalExpression = msStringConcatenate(pszFinalExpression, ")");
} else if (lp->connectiontype == MS_OGR) {
pszFinalExpression = msStrdup(pszExpression);
if (lp->filter.type != MS_EXPRESSION) {
bConcatWhere = 1;
} else {
if (lp->filter.string && EQUALN(lp->filter.string,"WHERE ",6)) {
bHasAWhere = 1;
bConcatWhere =1;
}
}
} else
pszFinalExpression = msStrdup(pszExpression);
if (bConcatWhere)
pszBuffer = msStringConcatenate(pszBuffer, "WHERE ");
/* if the filter is set and it's an expression type, concatenate it with
this filter. If not just free it */
if (lp->filter.string && lp->filter.type == MS_EXPRESSION) {
pszBuffer = msStringConcatenate(pszBuffer, "((");
if (bHasAWhere)
pszBuffer = msStringConcatenate(pszBuffer, lp->filter.string+6);
else
pszBuffer = msStringConcatenate(pszBuffer, lp->filter.string);
pszBuffer = msStringConcatenate(pszBuffer, ") and ");
} else if (lp->filter.string)
freeExpression(&lp->filter);
pszBuffer = msStringConcatenate(pszBuffer, pszFinalExpression);
if(lp->filter.string && lp->filter.type == MS_EXPRESSION)
pszBuffer = msStringConcatenate(pszBuffer, ")");
/*assuming that expression was properly escaped
escapedTextString = msStringEscape(pszBuffer);
msLoadExpressionString(&lp->filter,
(char*)CPLSPrintf("%s", escapedTextString));
msFree(escapedTextString);
*/
msLoadExpressionString(&lp->filter, pszBuffer);
msFree(pszFinalExpression);
if (pszBuffer)
msFree(pszBuffer);
return MS_TRUE;
}
return MS_FALSE;
}
char *FLTGetExpressionForValuesRanges(layerObj *lp, char *item, char *value, int forcecharcter)
{
int bIscharacter, bSqlLayer;
char *pszExpression = NULL, *pszEscapedStr=NULL, *pszTmpExpression=NULL;
char **paszElements = NULL, **papszRangeElements=NULL;
int numelements,i,nrangeelements;
/* double minval, maxval; */
if (lp && item && value) {
if (lp->connectiontype == MS_POSTGIS || lp->connectiontype == MS_ORACLESPATIAL ||
lp->connectiontype == MS_SDE || lp->connectiontype == MS_PLUGIN)
bSqlLayer = MS_TRUE;
else
bSqlLayer = MS_FALSE;
if (strstr(value, "/") == NULL) {
/*value(s)*/
paszElements = msStringSplit (value, ',', &numelements);
if (paszElements && numelements > 0) {
if (forcecharcter)
bIscharacter = MS_TRUE;
bIscharacter= !FLTIsNumeric(paszElements[0]);
pszTmpExpression = msStringConcatenate(pszTmpExpression, "(");
for (i=0; i<numelements; i++) {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "(");
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
else {
if (bIscharacter)
pszTmpExpression = msStringConcatenate(pszTmpExpression, "\"");
pszTmpExpression = msStringConcatenate(pszTmpExpression, "[");
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
pszTmpExpression = msStringConcatenate(pszTmpExpression, "]");
if (bIscharacter)
pszTmpExpression = msStringConcatenate(pszTmpExpression, "\"");
}
if (bIscharacter) {
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, " = '");
else
pszTmpExpression = msStringConcatenate(pszTmpExpression, " = \"");
} else
pszTmpExpression = msStringConcatenate(pszTmpExpression, " = ");
pszEscapedStr = msLayerEscapeSQLParam(lp, paszElements[i]);
pszTmpExpression = msStringConcatenate(pszTmpExpression, pszEscapedStr);
if (bIscharacter) {
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, "'");
else
pszTmpExpression = msStringConcatenate(pszTmpExpression, "\"");
}
pszTmpExpression = msStringConcatenate(pszTmpExpression, ")");
msFree(pszEscapedStr);
pszEscapedStr=NULL;
if (pszExpression != NULL)
pszExpression = msStringConcatenate(pszExpression, " OR ");
pszExpression = msStringConcatenate(pszExpression, pszTmpExpression);
msFree(pszTmpExpression);
pszTmpExpression = NULL;
}
pszExpression = msStringConcatenate(pszExpression, ")");
msFreeCharArray(paszElements, numelements);
}
} else {
/*range(s)*/
paszElements = msStringSplit (value, ',', &numelements);
if (paszElements && numelements > 0) {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "(");
for (i=0; i<numelements; i++) {
papszRangeElements = msStringSplit (paszElements[i], '/', &nrangeelements);
if (papszRangeElements && nrangeelements > 0) {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "(");
if (nrangeelements == 2 || nrangeelements == 3) {
/*
minval = atof(papszRangeElements[0]);
maxval = atof(papszRangeElements[1]);
*/
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
else {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "[");
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
pszTmpExpression = msStringConcatenate(pszTmpExpression, "]");
}
pszTmpExpression = msStringConcatenate(pszTmpExpression, " >= ");
pszEscapedStr = msLayerEscapeSQLParam(lp, papszRangeElements[0]);
pszTmpExpression = msStringConcatenate(pszTmpExpression, pszEscapedStr);
msFree(pszEscapedStr);
pszEscapedStr=NULL;
pszTmpExpression = msStringConcatenate(pszTmpExpression, " AND ");
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
else {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "[");
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
pszTmpExpression = msStringConcatenate(pszTmpExpression, "]");
}
pszTmpExpression = msStringConcatenate(pszTmpExpression, " <= ");
pszEscapedStr = msLayerEscapeSQLParam(lp, papszRangeElements[1]);
pszTmpExpression = msStringConcatenate(pszTmpExpression, pszEscapedStr);
msFree(pszEscapedStr);
pszEscapedStr=NULL;
pszTmpExpression = msStringConcatenate(pszTmpExpression, ")");
} else if (nrangeelements == 1) {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "(");
if (bSqlLayer)
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
else {
pszTmpExpression = msStringConcatenate(pszTmpExpression, "[");
pszTmpExpression = msStringConcatenate(pszTmpExpression, item);
pszTmpExpression = msStringConcatenate(pszTmpExpression, "]");
}
pszTmpExpression = msStringConcatenate(pszTmpExpression, " = ");
pszEscapedStr = msLayerEscapeSQLParam(lp, papszRangeElements[0]);
pszTmpExpression = msStringConcatenate(pszTmpExpression, pszEscapedStr);
msFree(pszEscapedStr);
pszEscapedStr=NULL;
pszTmpExpression = msStringConcatenate(pszTmpExpression, ")");
}
if (pszExpression != NULL)
pszExpression = msStringConcatenate(pszExpression, " OR ");
pszExpression = msStringConcatenate(pszExpression, pszTmpExpression);
msFree(pszTmpExpression);
pszTmpExpression = NULL;
msFreeCharArray(papszRangeElements, nrangeelements);
}
}
pszExpression = msStringConcatenate(pszExpression, ")");
msFreeCharArray(paszElements, numelements);
}
}
}
return pszExpression;
}
#ifdef USE_OGR
int FLTogrConvertGeometry(OGRGeometryH hGeometry, shapeObj *psShape,
OGRwkbGeometryType nType)
{
return msOGRGeometryToShape(hGeometry, psShape, nType);
}
int FLTShapeFromGMLTree(CPLXMLNode *psTree, shapeObj *psShape , char **ppszSRS)
{
char *pszSRS = NULL;
if (psTree && psShape) {
CPLXMLNode *psNext = psTree->psNext;
OGRGeometryH hGeometry = NULL;
psTree->psNext = NULL;
hGeometry = OGR_G_CreateFromGMLTree(psTree );
psTree->psNext = psNext;
if (hGeometry) {
OGRwkbGeometryType nType;
nType = OGR_G_GetGeometryType(hGeometry);
if (nType == wkbPolygon25D || nType == wkbMultiPolygon25D)
nType = wkbPolygon;
else if (nType == wkbLineString25D || nType == wkbMultiLineString25D)
nType = wkbLineString;
else if (nType == wkbPoint25D || nType == wkbMultiPoint25D)
nType = wkbPoint;
FLTogrConvertGeometry(hGeometry, psShape, nType);
OGR_G_DestroyGeometry(hGeometry);
}
pszSRS = (char *)CPLGetXMLValue(psTree, "srsName", NULL);
if (ppszSRS && pszSRS)
*ppszSRS = msStrdup(pszSRS);
return MS_TRUE;
}
return MS_FALSE;
}
int FLTGetGeosOperator(char *pszValue)
{
if (!pszValue)
return -1;
if (strcasecmp(pszValue, "Equals") == 0)
return MS_GEOS_EQUALS;
else if (strcasecmp(pszValue, "Intersect") == 0 ||
strcasecmp(pszValue, "Intersects") == 0)
return MS_GEOS_INTERSECTS;
else if (strcasecmp(pszValue, "Disjoint") == 0)
return MS_GEOS_DISJOINT;
else if (strcasecmp(pszValue, "Touches") == 0)
return MS_GEOS_TOUCHES;
else if (strcasecmp(pszValue, "Crosses") == 0)
return MS_GEOS_CROSSES;
else if (strcasecmp(pszValue, "Within") == 0)
return MS_GEOS_WITHIN;
else if (strcasecmp(pszValue, "Contains") == 0)
return MS_GEOS_CONTAINS;
else if (strcasecmp(pszValue, "Overlaps") == 0)
return MS_GEOS_OVERLAPS;
else if (strcasecmp(pszValue, "Beyond") == 0)
return MS_GEOS_BEYOND;
else if (strcasecmp(pszValue, "DWithin") == 0)
return MS_GEOS_DWITHIN;
return -1;
}
int FLTIsGeosNode(char *pszValue)
{
if (FLTGetGeosOperator(pszValue) == -1)
return MS_FALSE;
return MS_TRUE;
}
int FLTParseEpsgString(char *pszEpsg, projectionObj *psProj)
{
int nStatus = MS_FALSE;
int nTokens = 0;
char **tokens = NULL;
int nEpsgTmp=0;
#ifdef USE_PROJ
if (pszEpsg && psProj) {
nTokens = 0;
/* There are several forms an epsg code may be given. In any case the */
/* epsg code is the last token. */
/* TODO: To make sure it is an epsg code check the string, too. */
/* - urn:ogc:def:crs:EPSG:6.5:4326 */
/* - urn:ogc:def:crs:EPSG::4326 */
/* - http://www.opengis.net/gml/srs/epsg.xml#4326 */
/* - epsg:4326 */
tokens = msStringSplit(pszEpsg,'#', &nTokens);
if( tokens && nTokens == 1 ) {
msFreeCharArray(tokens, nTokens);
tokens = msStringSplit(pszEpsg,':', &nTokens);
}
if (tokens && nTokens > 1) {
nEpsgTmp = atoi(tokens[nTokens-1]);
if (nEpsgTmp > 0) {
char szTmp[32];
snprintf(szTmp, sizeof(szTmp), "init=epsg:%d",nEpsgTmp);
msInitProjection(psProj);
if (msLoadProjectionString(psProj, szTmp) == 0)
nStatus = MS_TRUE;
}
}
if (tokens)
msFreeCharArray(tokens, nTokens);
}
#endif
return nStatus;
}
int FLTGML2Shape_XMLNode(CPLXMLNode *psNode, shapeObj *psShp)
{
lineObj line= {0,NULL};
CPLXMLNode *psCoordinates = NULL;
char *pszTmpCoord = NULL;
char **szCoords = NULL;
int nCoords = 0;
if (!psNode || !psShp)
return MS_FALSE;
if( strcasecmp(psNode->pszValue,"PointType") == 0
|| strcasecmp(psNode->pszValue,"Point") == 0) {
psCoordinates = CPLGetXMLNode(psNode, "coordinates");
if (psCoordinates && psCoordinates->psChild &&
psCoordinates->psChild->pszValue) {
pszTmpCoord = psCoordinates->psChild->pszValue;
szCoords = msStringSplit(pszTmpCoord, ',', &nCoords);
if (szCoords && nCoords >= 2) {
line.numpoints = 1;
line.point = (pointObj *)malloc(sizeof(pointObj));
line.point[0].x = atof(szCoords[0]);
line.point[0].y = atof(szCoords[1]);
#ifdef USE_POINT_Z_M
line.point[0].m = 0;
#endif
psShp->type = MS_SHAPE_POINT;
msAddLine(psShp, &line);
free(line.point);
return MS_TRUE;
}
}
}
return MS_FALSE;
}
/************************************************************************/
/* FLTIsSimpleFilterNoSpatial */
/* */
/* Filter encoding with only attribute queries */
/************************************************************************/
int FLTIsSimpleFilterNoSpatial(FilterEncodingNode *psNode)
{
if (FLTIsSimpleFilter(psNode) &&
FLTNumberOfFilterType(psNode, "BBOX") == 0)
return MS_TRUE;
return MS_FALSE;
}
/************************************************************************/
/* FLTApplySimpleSQLFilter() */
/************************************************************************/
int FLTApplySimpleSQLFilter(FilterEncodingNode *psNode, mapObj *map,
int iLayerIndex)
{
layerObj *lp = NULL;
char *szExpression = NULL;
rectObj sQueryRect = map->extent;
char *szEPSG = NULL;
projectionObj sProjTmp;
char *pszBuffer = NULL;
int bConcatWhere = 0;
int bHasAWhere =0;
char *pszTmp = NULL, *pszTmp2 = NULL;
char *tmpfilename = NULL;
lp = (GET_LAYER(map, iLayerIndex));
/* if there is a bbox use it */
szEPSG = FLTGetBBOX(psNode, &sQueryRect);
if(szEPSG && map->projection.numargs > 0) {
if (FLTParseEpsgString(szEPSG, &sProjTmp)) {
msProjectRect(&sProjTmp, &map->projection, &sQueryRect);
msFreeProjection(&sProjTmp);
}
}
/* make sure that the layer can be queried*/
if (!lp->template)
lp->template = msStrdup("ttt.html");
/* if there is no class, create at least one, so that query by rect
would work*/
if (lp->numclasses == 0) {
if (msGrowLayerClasses(lp) == NULL)
return MS_FAILURE;
initClass(lp->class[0]);
}
bConcatWhere = 0;
bHasAWhere = 0;
if (lp->connectiontype == MS_POSTGIS || lp->connectiontype == MS_ORACLESPATIAL ||
lp->connectiontype == MS_SDE || lp->connectiontype == MS_PLUGIN) {
szExpression = FLTGetSQLExpression(psNode, lp);
if (szExpression) {
pszTmp = msStrdup("(");
pszTmp = msStringConcatenate(pszTmp, szExpression);
pszTmp = msStringConcatenate(pszTmp, ")");
msFree(szExpression);
szExpression = pszTmp;
}
}
/* concatenates the WHERE clause for OGR layers. This only applies if
the expression was empty or not of an expression string. If there
is an sql type expression, it is assumed to have the WHERE clause.
If it is an expression and does not have a WHERE it is assumed to be a mapserver
type expression*/
else if (lp->connectiontype == MS_OGR) {
if (lp->filter.type != MS_EXPRESSION) {
szExpression = FLTGetSQLExpression(psNode, lp);
bConcatWhere = 1;
} else {
if (lp->filter.string && EQUALN(lp->filter.string,"WHERE ",6)) {
szExpression = FLTGetSQLExpression(psNode, lp);
bHasAWhere = 1;
bConcatWhere =1;
} else {
szExpression = FLTGetCommonExpression(psNode, lp);
}
}
} else {
szExpression = FLTGetCommonExpression(psNode, lp);
}
if (szExpression) {
if (bConcatWhere)
pszBuffer = msStringConcatenate(pszBuffer, "WHERE ");
/* if the filter is set and it's an expression type, concatenate it with
this filter. If not just free it */
if (lp->filter.string && lp->filter.type == MS_EXPRESSION) {
pszBuffer = msStringConcatenate(pszBuffer, "((");
if (bHasAWhere)
pszBuffer = msStringConcatenate(pszBuffer, lp->filter.string+6);
else
pszBuffer = msStringConcatenate(pszBuffer, lp->filter.string);
pszBuffer = msStringConcatenate(pszBuffer, ") and ");
} else if (lp->filter.string)
freeExpression(&lp->filter);
pszBuffer = msStringConcatenate(pszBuffer, szExpression);
if(lp->filter.string && lp->filter.type == MS_EXPRESSION)
pszBuffer = msStringConcatenate(pszBuffer, ")");
msLoadExpressionString(&lp->filter, pszBuffer);
free(szExpression);
}
if (pszBuffer)
free(pszBuffer);
map->query.type = MS_QUERY_BY_RECT;
map->query.mode = MS_QUERY_MULTIPLE;
map->query.layer = lp->index;
map->query.rect = sQueryRect;
if(map->debug == MS_DEBUGLEVEL_VVV) {
tmpfilename = msTmpFile(map, map->mappath, NULL, "_filter.map");
if (tmpfilename == NULL) {
tmpfilename = msTmpFile(map, NULL, NULL, "_filter.map" );
}
if (tmpfilename) {
msSaveMap(map,tmpfilename);
msDebug("FLTApplySimpleSQLFilter(): Map file after Filter was applied %s\n", tmpfilename);
msFree(tmpfilename);
}
}
/*for oracle connection, if we have a simple filter with no spatial constraints
we should set the connection function to NONE to have a better performance
(#2725)*/
if (lp->connectiontype == MS_ORACLESPATIAL && FLTIsSimpleFilterNoSpatial(psNode)) {
if (strcasestr(lp->data, "USING") == 0)
lp->data = msStringConcatenate(lp->data, " USING NONE");
else if (strcasestr(lp->data, "NONE") == 0) {
/*if one of the functions is used, just replace it with NONE*/
if (strcasestr(lp->data, "FILTER"))
lp->data = msCaseReplaceSubstring(lp->data, "FILTER", "NONE");
else if (strcasestr(lp->data, "GEOMRELATE"))
lp->data = msCaseReplaceSubstring(lp->data, "GEOMRELATE", "NONE");
else if (strcasestr(lp->data, "RELATE"))
lp->data = msCaseReplaceSubstring(lp->data, "RELATE", "NONE");
else if (strcasestr(lp->data, "VERSION")) {
/*should add NONE just before the VERSION. Cases are:
DATA "ORA_GEOMETRY FROM data USING VERSION 10g
DATA "ORA_GEOMETRY FROM data USING UNIQUE FID VERSION 10g"
*/
pszTmp = (char *)strcasestr(lp->data, "VERSION");
pszTmp2 = msStringConcatenate(pszTmp2, " NONE ");
pszTmp2 = msStringConcatenate(pszTmp2, pszTmp);
lp->data = msCaseReplaceSubstring(lp->data, pszTmp, pszTmp2);
msFree(pszTmp2);
} else if (strcasestr(lp->data, "SRID")) {
lp->data = msStringConcatenate(lp->data, " NONE");
}
}
}
return msQueryByRect(map);
/* return MS_SUCCESS; */
}
/************************************************************************/
/* FLTIsSimpleFilter */
/* */
/* Filter encoding with only attribute queries and only one bbox. */
/************************************************************************/
int FLTIsSimpleFilter(FilterEncodingNode *psNode)
{
if (FLTValidForBBoxFilter(psNode)) {
if (FLTNumberOfFilterType(psNode, "DWithin") == 0 &&
FLTNumberOfFilterType(psNode, "Intersect") == 0 &&
FLTNumberOfFilterType(psNode, "Intersects") == 0 &&
FLTNumberOfFilterType(psNode, "Equals") == 0 &&
FLTNumberOfFilterType(psNode, "Disjoint") == 0 &&
FLTNumberOfFilterType(psNode, "Touches") == 0 &&
FLTNumberOfFilterType(psNode, "Crosses") == 0 &&
FLTNumberOfFilterType(psNode, "Within") == 0 &&
FLTNumberOfFilterType(psNode, "Contains") == 0 &&
FLTNumberOfFilterType(psNode, "Overlaps") == 0 &&
FLTNumberOfFilterType(psNode, "Beyond") == 0)
return TRUE;
}
return FALSE;
}
/************************************************************************/
/* FLTApplyFilterToLayer */
/* */
/* Use the filter encoding node to create mapserver expressions */
/* and apply it to the layer. */
/************************************************************************/
int FLTApplyFilterToLayer(FilterEncodingNode *psNode, mapObj *map,
int iLayerIndex)
{
layerObj *layer = GET_LAYER(map, iLayerIndex);
if ( ! layer->vtable) {
int rv = msInitializeVirtualTable(layer);
if (rv != MS_SUCCESS)
return rv;
}
return layer->vtable->LayerApplyFilterToLayer(psNode, map, iLayerIndex);
}
/************************************************************************/
/* FLTLayerApplyCondSQLFilterToLayer */
/* */
/* Helper function for layer virtual table architecture */
/************************************************************************/
int FLTLayerApplyCondSQLFilterToLayer(FilterEncodingNode *psNode, mapObj *map,
int iLayerIndex)
{
/* ==================================================================== */
/* Check here to see if it is a simple filter and if that is */
/* the case, we are going to use the FILTER element on */
/* the layer. */
/* ==================================================================== */
if (FLTIsSimpleFilter(psNode)) {
return FLTApplySimpleSQLFilter(psNode, map, iLayerIndex);
}
return FLTLayerApplyPlainFilterToLayer(psNode, map, iLayerIndex);
}
/************************************************************************/
/* FLTLayerApplyPlainFilterToLayer */
/* */
/* Helper function for layer virtual table architecture */
/************************************************************************/
int FLTLayerApplyPlainFilterToLayer(FilterEncodingNode *psNode, mapObj *map,
int iLayerIndex)
{
char *pszExpression =NULL;
int status =MS_FALSE;
pszExpression = FLTGetCommonExpression(psNode, GET_LAYER(map, iLayerIndex));
if(map->debug == MS_DEBUGLEVEL_VVV)
msDebug("FLTLayerApplyPlainFilterToLayer(): %s\n", pszExpression);
if (pszExpression) {
status = FLTApplyFilterToLayerCommonExpression(map, iLayerIndex, pszExpression);
msFree(pszExpression);
}
return status;
}
/************************************************************************/
/* FilterNode *FLTPaserFilterEncoding(char *szXMLString) */
/* */
/* Parses an Filter Encoding XML string and creates a */
/* FilterEncodingNodes corresponding to the string. */
/* Returns a pointer to the first node or NULL if */
/* unsuccessfull. */
/* Calling function should use FreeFilterEncodingNode function */
/* to free memeory. */
/************************************************************************/
FilterEncodingNode *FLTParseFilterEncoding(char *szXMLString)
{
CPLXMLNode *psRoot = NULL, *psChild=NULL, *psFilter=NULL;
CPLXMLNode *psFilterStart = NULL;
FilterEncodingNode *psFilterNode = NULL;
if (szXMLString == NULL || strlen(szXMLString) <= 0 ||
(strstr(szXMLString, "Filter") == NULL))
return NULL;
psRoot = CPLParseXMLString(szXMLString);
if( psRoot == NULL)
return NULL;
/* strip namespaces. We srtip all name spaces (#1350)*/
CPLStripXMLNamespace(psRoot, NULL, 1);
/* -------------------------------------------------------------------- */
/* get the root element (Filter). */
/* -------------------------------------------------------------------- */
psChild = psRoot;
psFilter = NULL;
while( psChild != NULL ) {
if (psChild->eType == CXT_Element &&
EQUAL(psChild->pszValue,"Filter")) {
psFilter = psChild;
break;
} else
psChild = psChild->psNext;
}
if (!psFilter)
return NULL;
psChild = psFilter->psChild;
psFilterStart = NULL;
while (psChild) {
if (FLTIsSupportedFilterType(psChild)) {
psFilterStart = psChild;
psChild = NULL;
} else
psChild = psChild->psNext;
}
if (psFilterStart && FLTIsSupportedFilterType(psFilterStart)) {
psFilterNode = FLTCreateFilterEncodingNode();
FLTInsertElementInNode(psFilterNode, psFilterStart);
}
CPLDestroyXMLNode( psRoot );
/* -------------------------------------------------------------------- */
/* validate the node tree to make sure that all the nodes are valid.*/
/* -------------------------------------------------------------------- */
if (!FLTValidFilterNode(psFilterNode))
return NULL;
return psFilterNode;
}
/************************************************************************/
/* int FLTValidFilterNode(FilterEncodingNode *psFilterNode) */
/* */
/* Validate that all the nodes are filled properly. We could */
/* have parts of the nodes that are correct and part which */
/* could be incorrect if the filter string sent is corrupted */
/* (eg missing a value :<PropertyName><PropertyName>) */
/************************************************************************/
int FLTValidFilterNode(FilterEncodingNode *psFilterNode)
{
int bReturn = 0;
if (!psFilterNode)
return 0;
if (psFilterNode->eType == FILTER_NODE_TYPE_UNDEFINED)
return 0;
if (psFilterNode->psLeftNode) {
bReturn = FLTValidFilterNode(psFilterNode->psLeftNode);
if (bReturn == 0)
return 0;
else if (psFilterNode->psRightNode)
return FLTValidFilterNode(psFilterNode->psRightNode);
}
return 1;
}
/************************************************************************/
/* FLTFreeFilterEncodingNode */
/* */
/* recursive freeing of Filer Encoding nodes. */
/************************************************************************/
void FLTFreeFilterEncodingNode(FilterEncodingNode *psFilterNode)
{
if (psFilterNode) {
if (psFilterNode->psLeftNode) {
FLTFreeFilterEncodingNode(psFilterNode->psLeftNode);
psFilterNode->psLeftNode = NULL;
}
if (psFilterNode->psRightNode) {
FLTFreeFilterEncodingNode(psFilterNode->psRightNode);
psFilterNode->psRightNode = NULL;
}
if (psFilterNode->pszSRS)
free( psFilterNode->pszSRS);
if( psFilterNode->pOther ) {
if (psFilterNode->pszValue != NULL &&
strcasecmp(psFilterNode->pszValue, "PropertyIsLike") == 0) {
if( ((FEPropertyIsLike *)psFilterNode->pOther)->pszWildCard )
free( ((FEPropertyIsLike *)psFilterNode->pOther)->pszWildCard );
if( ((FEPropertyIsLike *)psFilterNode->pOther)->pszSingleChar )
free( ((FEPropertyIsLike *)psFilterNode->pOther)->pszSingleChar );
if( ((FEPropertyIsLike *)psFilterNode->pOther)->pszEscapeChar )
free( ((FEPropertyIsLike *)psFilterNode->pOther)->pszEscapeChar );
} else if (psFilterNode->eType == FILTER_NODE_TYPE_GEOMETRY_POINT ||
psFilterNode->eType == FILTER_NODE_TYPE_GEOMETRY_LINE ||
psFilterNode->eType == FILTER_NODE_TYPE_GEOMETRY_POLYGON) {
msFreeShape((shapeObj *)(psFilterNode->pOther));
}
/* else */
/* TODO free pOther special fields */
free( psFilterNode->pOther );
}
/* Cannot free pszValue before, 'cause we are testing it above */
if( psFilterNode->pszValue )
free( psFilterNode->pszValue );
free(psFilterNode);
}
}
/************************************************************************/
/* FLTCreateFilterEncodingNode */
/* */
/* return a FilerEncoding node. */
/************************************************************************/
FilterEncodingNode *FLTCreateFilterEncodingNode(void)
{
FilterEncodingNode *psFilterNode = NULL;
psFilterNode =
(FilterEncodingNode *)malloc(sizeof (FilterEncodingNode));
psFilterNode->eType = FILTER_NODE_TYPE_UNDEFINED;
psFilterNode->pszValue = NULL;
psFilterNode->pOther = NULL;
psFilterNode->pszSRS = NULL;
psFilterNode->psLeftNode = NULL;
psFilterNode->psRightNode = NULL;
return psFilterNode;
}
FilterEncodingNode *FLTCreateBinaryCompFilterEncodingNode(void)
{
FilterEncodingNode *psFilterNode = NULL;
psFilterNode = FLTCreateFilterEncodingNode();
/* used to store case sensitivity flag. Default is 0 meaning the
comparing is case snetitive */
psFilterNode->pOther = (int *)malloc(sizeof(int));
(*(int *)(psFilterNode->pOther)) = 0;
return psFilterNode;
}
/************************************************************************/
/* FLTInsertElementInNode */
/* */
/* Utility function to parse an XML node and transfter the */
/* contennts into the Filer Encoding node structure. */
/************************************************************************/
void FLTInsertElementInNode(FilterEncodingNode *psFilterNode,
CPLXMLNode *psXMLNode)
{
int nStrLength = 0;
char *pszTmp = NULL;
FilterEncodingNode *psCurFilNode= NULL;
CPLXMLNode *psCurXMLNode = NULL;
CPLXMLNode *psTmpNode = NULL;
CPLXMLNode *psFeatureIdNode = NULL;
char *pszFeatureId=NULL, *pszFeatureIdList=NULL;
char **tokens = NULL;
int nTokens = 0;
if (psFilterNode && psXMLNode && psXMLNode->pszValue) {
psFilterNode->pszValue = msStrdup(psXMLNode->pszValue);
psFilterNode->psLeftNode = NULL;
psFilterNode->psRightNode = NULL;
/* -------------------------------------------------------------------- */
/* Logical filter. AND, OR and NOT are supported. Example of */
/* filer using logical filters : */
/* <Filter> */
/* <And> */
/* <PropertyIsGreaterThan> */
/* <PropertyName>Person/Age</PropertyName> */
/* <Literal>50</Literal> */
/* </PropertyIsGreaterThan> */
/* <PropertyIsEqualTo> */
/* <PropertyName>Person/Address/City</PropertyName> */
/* <Literal>Toronto</Literal> */
/* </PropertyIsEqualTo> */
/* </And> */
/* </Filter> */
/* -------------------------------------------------------------------- */
if (FLTIsLogicalFilterType(psXMLNode->pszValue)) {
psFilterNode->eType = FILTER_NODE_TYPE_LOGICAL;
if (strcasecmp(psFilterNode->pszValue, "AND") == 0 ||
strcasecmp(psFilterNode->pszValue, "OR") == 0) {
if (psXMLNode->psChild && psXMLNode->psChild->psNext) {
/*2 operators */
if (psXMLNode->psChild->psNext->psNext == NULL) {
psFilterNode->psLeftNode = FLTCreateFilterEncodingNode();
FLTInsertElementInNode(psFilterNode->psLeftNode,
psXMLNode->psChild);
psFilterNode->psRightNode = FLTCreateFilterEncodingNode();
FLTInsertElementInNode(psFilterNode->psRightNode,
psXMLNode->psChild->psNext);
} else {
psCurXMLNode = psXMLNode->psChild;
psCurFilNode = psFilterNode;
while(psCurXMLNode) {
if (psCurXMLNode->psNext && psCurXMLNode->psNext->psNext) {
psCurFilNode->psLeftNode =
FLTCreateFilterEncodingNode();
FLTInsertElementInNode(psCurFilNode->psLeftNode,
psCurXMLNode);
psCurFilNode->psRightNode =
FLTCreateFilterEncodingNode();
psCurFilNode->psRightNode->eType =
FILTER_NODE_TYPE_LOGICAL;
psCurFilNode->psRightNode->pszValue =