-
Notifications
You must be signed in to change notification settings - Fork 20
/
filter.js
253 lines (238 loc) · 6.77 KB
/
filter.js
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
/**
* Factory methods for filterelements
* @see http://schemas.opengis.net/filter/1.0.0/filter.xsd
*
* @module
*/
const TYPE_COMPARISON = 'comparison';
/**
* @var string[] element names of binary comparison
* @private
*/
const BINARY_COMPARISON_NAMES = [
'PropertyIsEqualTo',
'PropertyIsNotEqualTo',
'PropertyIsLessThan',
'PropertyIsLessThanOrEqualTo',
'PropertyIsGreaterThan',
'PropertyIsGreaterThanOrEqualTo',
];
const COMPARISON_NAMES = BINARY_COMPARISON_NAMES.concat([
'PropertyIsLike',
'PropertyIsNull',
'PropertyIsBetween',
]);
/**
* @private
* @param {string} localName
*
* @return null|string
*/
function getChildTextContent(node, localName) {
const propertyNameElement = node
.getElementsByTagNameNS(node.namespaceURI, localName)
.item(0);
if (!propertyNameElement) {
return null;
}
if (propertyNameElement.parentNode !== node) {
throw new Error('Expected direct descant');
}
return propertyNameElement ? propertyNameElement.textContent.trim() : null;
}
function isComparison(element) {
return COMPARISON_NAMES.includes(element.localName);
}
function isBinary(element) {
return ['or', 'and'].includes(element.localName.toLowerCase());
}
/**
* factory for comparisonOps
* @private
* @param {Element} element
*
* @return {object}
*/
function createComparison(element) {
if (BINARY_COMPARISON_NAMES.includes(element.localName)) {
return createBinaryFilterComparison(element);
}
if (element.localName === 'PropertyIsBetween') {
return createIsBetweenComparison(element);
}
if (element.localName === 'PropertyIsNull') {
return createIsNullComparison(element);
}
if (element.localName === 'PropertyIsLike') {
return createIsLikeComparison(element);
}
throw new Error(`Unknown comparison element ${element.localName}`);
}
/**
* factory for element type BinaryComparisonOpType
* @private
* @param {Element} element
*
* @return {object}
*/
function createBinaryFilterComparison(element) {
const propertyname = getChildTextContent(element, 'PropertyName');
const literal = getChildTextContent(element, 'Literal');
return {
type: TYPE_COMPARISON,
operator: element.localName.toLowerCase(),
propertyname,
literal,
};
}
/**
* factory for element type PropertyIsLikeType
* @private
* @param {Element} element
*
* @return {object}
*/
function createIsLikeComparison(element) {
const propertyname = getChildTextContent(element, 'PropertyName');
const literal = getChildTextContent(element, 'Literal');
return {
type: TYPE_COMPARISON,
operator: element.localName.toLowerCase(),
propertyname,
literal,
wildcard: element.getAttribute('wildCard'),
singlechar: element.getAttribute('singleChar'),
escapechar: element.getAttribute('escapeChar'),
};
}
/**
* factory for element type PropertyIsNullType
* @private
* @param {Element} element
*
* @return {object}
*/
function createIsNullComparison(element) {
const propertyname = getChildTextContent(element, 'PropertyName');
return {
type: TYPE_COMPARISON,
operator: element.localName.toLowerCase(),
propertyname,
};
}
/**
* factory for element type PropertyIsBetweenType
* @private
* @param {Element} element
*
* @return {object}
*/
function createIsBetweenComparison(element) {
const propertyname = getChildTextContent(element, 'PropertyName');
const lowerboundary = getChildTextContent(element, 'LowerBoundary');
const upperboundary = getChildTextContent(element, 'UpperBoundary');
return {
type: TYPE_COMPARISON,
operator: element.localName.toLowerCase(),
lowerboundary,
upperboundary,
propertyname,
};
}
/**
* Factory for and/or filter
* @private
* @param {Element} element
*
* @return {object}
*/
function createBinaryLogic(element) {
const predicates = [];
for (let n = element.firstElementChild; n; n = n.nextElementSibling) {
if (isComparison(n)) {
predicates.push(createComparison(n));
}
}
return {
type: element.localName.toLowerCase(),
predicates,
};
}
/**
* Factory for not filter
* @private
* @param {Element} element
*
* @return {object}
*/
function createUnaryLogic(element) {
let predicate = null;
const childElement = element.firstElementChild;
if (childElement && isComparison(childElement)) {
predicate = createComparison(childElement);
}
if (childElement && isBinary(childElement)) {
predicate = createBinaryLogic(childElement);
}
return {
type: element.localName.toLowerCase(),
predicate,
};
}
/**
* Factory root filter element
* @param {Element} element
*
* @return {Filter}
*/
export default function createFilter(element) {
let filter = {};
for (let n = element.firstElementChild; n; n = n.nextElementSibling) {
if (isComparison(n)) {
filter = createComparison(n);
}
if (isBinary(n)) {
filter = createBinaryLogic(n);
}
if (n.localName.toLowerCase() === 'not') {
filter = createUnaryLogic(n);
}
if (n.localName.toLowerCase() === 'featureid') {
filter.type = 'featureid';
filter.fids = filter.fids || [];
filter.fids.push(n.getAttribute('fid'));
}
}
return filter;
}
/**
* A filter predicate.
* @typedef Filter
* @name Filter
* @description [filter operators](http://schemas.opengis.net/filter/1.1.0/filter.xsd), see also
* [geoserver](http://docs.geoserver.org/stable/en/user/styling/sld/reference/filters.html)
* @property {string} type Can be 'comparison', 'and', 'or', 'not', or 'featureid'.
* @property {Array<string>} [fids] An array of feature id's. Required for type='featureid'.
* @property {string} [operator] Required for type='comparison'. Can be one of
* 'propertyisequalto',
* 'propertyisnotequalto',
* 'propertyislessthan',
* 'propertyislessthanorequalto',
* 'propertyisgreaterthan',
* 'propertyisgreaterthanorequalto',
* 'propertyislike',
* 'propertyisbetween'
* @property {Filter[]} [predicates] Required for type='and' or type='or'.
* An array of filter predicates that must all evaluate to true for 'and', or
* for which at least one must evaluate to true for 'or'.
* @property {Filter} [predicate] Required for type='not'. A single predicate to negate.
* @property {string} [propertyname] Required for type='comparison'.
* @property {string} [literal] A literal value to use in a comparison,
* required for type='comparison'.
* @property {string} [lowerboundary] Lower boundary, required for operator='propertyisbetween'.
* @property {string} [upperboundary] Upper boundary, required for operator='propertyisbetween'.
* @property {string} [wildcard] Required wildcard character for operator='propertyislike'.
* @property {string} [singlechar] Required single char match character,
* required for operator='propertyislike'.
* @property {string} [escapechar] Required escape character for operator='propertyislike'.
*/