-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfilters.py
More file actions
62 lines (54 loc) · 2.3 KB
/
filters.py
File metadata and controls
62 lines (54 loc) · 2.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
import re
class FilteringFailedException(Exception):
pass
class BasicFilterer(object):
"""Filter that reads in a dictionary and filters based on feature properties"""
def __init__(self, filter_def, filter_operator):
super(BasicFilterer, self).__init__()
self.filter_def = filter_def
self.operator = filter_operator
if not self.operator in ["and", "or"]:
raise FilteringFailedException(
"Unknown filter operator:%s" % filter_operator
)
def keep(self, feature):
for item in self.filter_def:
if not "expression" in item:
raise FilteringFailedException(
"Expression missing from filter definition" % (key, value)
)
if item["expression"] == "not null":
test_success = (
item["key"] in feature["properties"]
and feature["properties"][item["key"]] is not None
)
elif item["expression"] == "=":
test_success = (
item["key"] in feature["properties"]
and feature["properties"][item["key"]] == item["value"]
)
elif item["expression"] == "!=":
test_success = (
item["key"] not in feature["properties"]
or feature["properties"][item["key"]] != item["value"]
)
elif item["expression"] == "match" or item["expression"] == "not match":
test_success = (
item["key"] in feature["properties"]
and feature["properties"][item["key"]] is not None
and re.match(item["value"], feature["properties"][item["key"]])
)
if item["expression"] == "not match":
test_success = not test_success
else:
raise FilteringFailedException(
"Unhandled filtering expression:%s" % (item["expression"])
)
if self.operator == "and" and not test_success:
return False
elif self.operator == "or" and test_success:
return True
if self.operator == "or":
return False
else:
return True