-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathLocalFilterer.js
More file actions
338 lines (304 loc) · 12.3 KB
/
LocalFilterer.js
File metadata and controls
338 lines (304 loc) · 12.3 KB
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
// Part of the LayersTool that deals with filtering
import $ from 'jquery'
import F_ from '../Basics/Formulae_/Formulae_'
import L_ from '../Basics/Layers_/Layers_'
import flat from 'flat'
import { booleanIntersects, booleanContains } from '@turf/turf'
const LocalFilterer = {
make: function (container, layerName) {
const layerObj = L_.layers.data[layerName]
if (layerObj == null) return
const type = layerObj.type
if (type === 'vector') {
} else if (type === 'query') {
}
},
destroy: function (layerName) {},
getAggregations: function (geojson) {
const aggs = {
'geometry.type': { type: 'string', aggs: {} },
}
geojson.features.forEach((feature) => {
const flatProps = flat.flatten(feature.properties)
for (let p in flatProps) {
let value = flatProps[p]
let type
if (!isNaN(value) && !isNaN(parseFloat(value))) type = 'number'
else if (typeof value === 'string') type = 'string'
else if (typeof value === 'number') type = 'number'
else if (typeof value === 'boolean') type = 'boolean'
if (type != null) {
// First type will be from index 0
aggs[p] = aggs[p] || { type: type, aggs: {} }
// Because of that, strings can usurp numbers (ex. ["1", "2", "Melon", "Pastry"])
if (aggs[p].type === 'number' && type === 'string')
aggs[p].type = type
aggs[p].aggs[flatProps[p]] = aggs[p].aggs[flatProps[p]] || 0
aggs[p].aggs[flatProps[p]]++
}
}
// feature.geometry.type
if (feature.geometry != null) {
aggs['geometry.type'].aggs[feature.geometry.type] =
aggs['geometry.type'].aggs[feature.geometry.type] || 0
aggs['geometry.type'].aggs[feature.geometry.type]++
}
})
// sort values
Object.keys(aggs).forEach((agg) => {
const sortedAggs = {}
Object.keys(aggs[agg].aggs)
.sort()
.reverse()
.forEach((agg2) => {
sortedAggs[agg2] = aggs[agg].aggs[agg2]
})
aggs[agg].aggs = sortedAggs
})
return aggs
},
filter: function (layerName, filter, refreshFunction) {
const geojson = filter.geojson
const filteredGeoJSON = JSON.parse(JSON.stringify(geojson))
filteredGeoJSON.features = []
// Filter
const halfFilteredGeoJSONFeatures = []
geojson.features.forEach((f) => {
if (LocalFilterer.match(f, filter))
halfFilteredGeoJSONFeatures.push(f)
})
// Spatial Filter (after filter to make it quicker)
halfFilteredGeoJSONFeatures.forEach((f) => {
if (filter.spatial == null || filter.spatial.center == null) {
filteredGeoJSON.features.push(f)
} else {
if (filter.spatial.radius > 0) {
// Circle Intersects
if (
booleanIntersects(
filter.spatial.feature.geometry,
f.geometry
)
)
filteredGeoJSON.features.push(f)
} else {
// Point Contained
if (
booleanContains(
f.geometry,
filter.spatial.feature.geometry
)
)
filteredGeoJSON.features.push(f)
}
}
})
// Set count
$('#layersTool_filtering_count').text(
`(${filteredGeoJSON.features.length}/${geojson.features.length})`
)
// Update layer
if (typeof refreshFunction === 'function') {
refreshFunction(filteredGeoJSON)
} else {
L_.clearVectorLayer(layerName)
L_.updateVectorLayer(layerName, filteredGeoJSON, null, true)
}
},
match: function (feature, filter) {
if (filter.values.length === 0) return true
// Perform the per row match
for (let i = 0; i < filter.values.length; i++) {
const v = filter.values[i]
if (v && v.isGroup === true) {
} else if (v && v.key != null) {
let featureValue =
v.key === 'geometry.type'
? feature.geometry.type
: F_.getIn(feature.properties, v.key)
let filterValue = v.value
if (v.type === 'number' && v.op != ',')
filterValue = parseFloat(filterValue)
else if (v.type === 'boolean') {
if (featureValue == null) featureValue = false
filterValue = filterValue == 'true'
}
if (featureValue != null) {
switch (v.op) {
case '=':
if (featureValue == filterValue) v.matches = true
else v.matches = false
break
case ',':
if (filterValue != null) {
const stringFilterValue = filterValue + ''
const stringFeatureValue = featureValue + ''
if (
stringFilterValue
.split(',')
.includes(stringFeatureValue)
)
v.matches = true
else v.matches = false
} else v.matches = false
break
case '<':
if (
v.type === 'string'
? featureValue.localeCompare(filterValue) >
0
: featureValue < filterValue
)
v.matches = true
else v.matches = false
break
case '>':
if (
v.type === 'string'
? featureValue.localeCompare(filterValue) <
0
: featureValue > filterValue
)
v.matches = true
else v.matches = false
break
case 'contains':
if (
String(featureValue).indexOf(
String(filterValue)
) != -1
)
v.matches = true
else v.matches = false
break
case 'beginswith':
if (
String(featureValue).startsWith(
String(filterValue)
)
)
v.matches = true
else v.matches = false
break
case 'endswith':
if (
String(featureValue).endsWith(
String(filterValue)
)
)
v.matches = true
else v.matches = false
break
default:
break
}
//if (!matches) return false
} else {
v.matches = false
}
}
}
/*
// Now group together all matching keys and process
// Filter values with the same key are ORed together if = and ANDed if not
// i.e. sol = 50, sol = 51 becomes sol == 50 OR sol == 51
// sol > 50, sol < 100 becomes sol > 50 AND sol < 100
// sol > 50, sol < 100, sol = 200 becomes (sol > 50 AND sol < 100) OR sol == 101
const groupedValuesByKey = {}
filter.values.forEach((v) => {
if (v && v.key != null) {
groupedValuesByKey[v.key] = groupedValuesByKey[v.key] || []
groupedValuesByKey[v.key].push(v)
}
})
const matches = []
Object.keys(groupedValuesByKey).forEach((key) => {
// For grouped values to pass, (all the >,< ANDed must be true) OR (all the rest ORed must be true)
// To facilitate that, first group by operator
const groupedValuesByOp = {}
groupedValuesByKey[key].forEach((v) => {
let op = v.op
if (op === '<' || op === '>') op = '<>'
groupedValuesByOp[op] = groupedValuesByOp[op] || []
groupedValuesByOp[op].push(v)
})
const opMatches = []
Object.keys(groupedValuesByOp).forEach((op) => {
let match = null
groupedValuesByOp[op].forEach((v) => {
if (op === '<>') {
if (match === null) match = true
match = match && v.matches
} else {
if (match === null) match = false
match = match || v.matches
}
})
opMatches.push(match)
})
// If at least one true, the op match passes
matches.push(opMatches.includes(true))
})
// If all are true
return matches.filter(Boolean).length === matches.length
*/
let fvalues = JSON.parse(JSON.stringify(filter.values))
fvalues = fvalues.filter(Boolean)
if (filter.valuesOrder) {
fvalues.sort((a, b) => {
return (
filter.valuesOrder.indexOf(a.id) -
filter.valuesOrder.indexOf(b.id)
)
})
}
return LocalFilterer.evaluateMatchGroupings(fvalues)
},
evaluateMatchGroupings(conditions) {
const groups = []
let currentOp = 'AND' // Default to AND if no op is provided
let currentGroup = []
let negateNextGroup = false
for (let i = 0; i < conditions.length; i++) {
const item = conditions[i]
if (item.isGroup && item.op) {
// Save the current group before switching op
if (currentGroup.length > 0) {
groups.push({
op: currentOp,
matches: currentGroup,
})
currentGroup = []
}
currentOp = item.op
} else if ('matches' in item) {
currentGroup.push(item.matches)
}
}
// Push the final group
if (currentGroup.length > 0) {
groups.push({
op: currentOp,
matches: currentGroup,
})
}
// Evaluate each group
const evaluatedGroups = groups.map((group) => {
let result
if (group.op === 'OR') {
result = group.matches.some(Boolean)
} else if (group.op === 'NOT_AND') {
result = !group.matches.every(Boolean)
} else if (group.op === 'NOT_OR') {
result = !group.matches.some(Boolean)
} else {
// default to AND
result = group.matches.every(Boolean)
}
return result
})
// Final result: AND all evaluated group results
return evaluatedGroups.every(Boolean)
},
}
export default LocalFilterer