-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from bitner/cqlbackend
CQL 2 Support
- Loading branch information
Showing
18 changed files
with
1,371 additions
and
694 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
from .evaluate import to_cql2 | ||
|
||
__all__ = ["to_cql2"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
# ------------------------------------------------------------------------------ | ||
# | ||
# Project: pygeofilter <https://github.com/geopython/pygeofilter> | ||
# Authors: Fabian Schindler <[email protected]>, | ||
# David Bitner <[email protected]> | ||
# | ||
# ------------------------------------------------------------------------------ | ||
# Copyright (C) 2021 EOX IT Services GmbH | ||
# | ||
# 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 DEALINGS IN | ||
# THE SOFTWARE. | ||
# ------------------------------------------------------------------------------ | ||
|
||
from typing import Dict, Optional | ||
from datetime import datetime | ||
|
||
import shapely.geometry | ||
|
||
from ..evaluator import Evaluator, handle | ||
from ... import ast | ||
from ...cql2 import get_op | ||
from ... import values | ||
|
||
|
||
class CQL2Evaluator(Evaluator): | ||
def __init__( | ||
self, | ||
attribute_map: Optional[Dict[str, str]], | ||
function_map: Optional[Dict[str, str]], | ||
): | ||
self.attribute_map = attribute_map | ||
self.function_map = function_map | ||
|
||
@handle( | ||
ast.Condition, | ||
ast.Comparison, | ||
ast.TemporalPredicate, | ||
ast.SpatialComparisonPredicate, | ||
ast.Arithmetic, | ||
ast.ArrayPredicate, | ||
subclasses=True, | ||
) | ||
def comparison(self, node, *args): | ||
op = get_op(node) | ||
return {"op": op, "args": [*args]} | ||
|
||
@handle(ast.Between) | ||
def between(self, node, lhs, low, high): | ||
return {"op": "between", "args": [lhs, [low, high]]} | ||
|
||
@handle(ast.Like) | ||
def like(self, node, *subargs): | ||
return {"op": "like", "args": [node.lhs, node.pattern]} | ||
|
||
@handle(ast.IsNull) | ||
def isnull(self, node, arg): | ||
return {"op": "isNull", "args": arg} | ||
|
||
@handle(ast.Function) | ||
def function(self, node, *args): | ||
name = node.name.lower() | ||
if name == "lower": | ||
ret = {"lower": args[0]} | ||
else: | ||
ret = {"function": name, "args": [*args]} | ||
return ret | ||
|
||
@handle(ast.In) | ||
def in_(self, node, lhs, *options): | ||
return {"in": {"value": lhs, "list": options}} | ||
|
||
@handle(ast.Attribute) | ||
def attribute(self, node: ast.Attribute): | ||
return {"property": node.name} | ||
|
||
@handle(values.Interval) | ||
def interval(self, node: values.Interval): | ||
return {"interval": [node.start, node.end]} | ||
|
||
@handle(datetime) | ||
def datetime(self, node: ast.Attribute): | ||
return {"timestamp": node.name} | ||
|
||
@handle(*values.LITERALS) | ||
def literal(self, node): | ||
return node | ||
|
||
@handle(values.Geometry) | ||
def geometry(self, node: values.Geometry): | ||
return shapely.geometry.shape(node).__geo_interface__ | ||
|
||
@handle(values.Envelope) | ||
def envelope(self, node: values.Envelope): | ||
return shapely.geometry.box( | ||
node.x1, node.y1, node.x2, node.y2 | ||
).__geo_interface__ | ||
|
||
|
||
def to_cql2( | ||
root: ast.Node, | ||
field_mapping: Optional[Dict[str, str]] = None, | ||
function_map: Optional[Dict[str, str]] = None, | ||
) -> str: | ||
return CQL2Evaluator(field_mapping, function_map).evaluate(root) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
# Common configurations for cql2 parsers and evaluators. | ||
from typing import Dict, Type, Union | ||
from . import ast | ||
|
||
# https://github.com/opengeospatial/ogcapi-features/tree/master/cql2 | ||
|
||
|
||
COMPARISON_MAP: Dict[str, Type[ast.Node]] = { | ||
"=": ast.Equal, | ||
"eq": ast.Equal, | ||
"<>": ast.NotEqual, | ||
"!=": ast.NotEqual, | ||
"ne": ast.NotEqual, | ||
"<": ast.LessThan, | ||
"lt": ast.LessThan, | ||
"<=": ast.LessEqual, | ||
"lte": ast.LessEqual, | ||
">": ast.GreaterThan, | ||
"gt": ast.GreaterThan, | ||
">=": ast.GreaterEqual, | ||
"gte": ast.GreaterEqual, | ||
"like": ast.Like, | ||
} | ||
|
||
SPATIAL_PREDICATES_MAP: Dict[str, Type[ast.SpatialComparisonPredicate]] = { | ||
"s_intersects": ast.GeometryIntersects, | ||
"s_equals": ast.GeometryEquals, | ||
"s_disjoint": ast.GeometryDisjoint, | ||
"s_touches": ast.GeometryTouches, | ||
"s_within": ast.GeometryWithin, | ||
"s_overlaps": ast.GeometryOverlaps, | ||
"s_crosses": ast.GeometryCrosses, | ||
"s_contains": ast.GeometryContains, | ||
} | ||
|
||
TEMPORAL_PREDICATES_MAP: Dict[str, Type[ast.TemporalPredicate]] = { | ||
"t_before": ast.TimeBefore, | ||
"t_after": ast.TimeAfter, | ||
"t_meets": ast.TimeMeets, | ||
"t_metby": ast.TimeMetBy, | ||
"t_overlaps": ast.TimeOverlaps, | ||
"t_overlappedby": ast.TimeOverlappedBy, | ||
"t_begins": ast.TimeBegins, | ||
"t_begunby": ast.TimeBegunBy, | ||
"t_during": ast.TimeDuring, | ||
"t_contains": ast.TimeContains, | ||
"t_ends": ast.TimeEnds, | ||
"t_endedby": ast.TimeEndedBy, | ||
"t_equals": ast.TimeEquals, | ||
"t_intersects": ast.TimeOverlaps, | ||
} | ||
|
||
|
||
ARRAY_PREDICATES_MAP: Dict[str, Type[ast.ArrayPredicate]] = { | ||
"a_equals": ast.ArrayEquals, | ||
"a_contains": ast.ArrayContains, | ||
"a_containedby": ast.ArrayContainedBy, | ||
"a_overlaps": ast.ArrayOverlaps, | ||
} | ||
|
||
ARITHMETIC_MAP: Dict[str, Type[ast.Arithmetic]] = { | ||
"+": ast.Add, | ||
"-": ast.Sub, | ||
"*": ast.Mul, | ||
"/": ast.Div, | ||
} | ||
|
||
CONDITION_MAP: Dict[str, Type[ast.Node]] = { | ||
"and": ast.And, | ||
"or": ast.Or, | ||
"not": ast.Not, | ||
"isNull": ast.IsNull, | ||
} | ||
|
||
BINARY_OP_PREDICATES_MAP: Dict[ | ||
str, | ||
Union[ | ||
Type[ast.Node], | ||
Type[ast.Comparison], | ||
Type[ast.SpatialComparisonPredicate], | ||
Type[ast.TemporalPredicate], | ||
Type[ast.ArrayPredicate], | ||
Type[ast.Arithmetic], | ||
], | ||
] = { | ||
**COMPARISON_MAP, | ||
**SPATIAL_PREDICATES_MAP, | ||
**TEMPORAL_PREDICATES_MAP, | ||
**ARRAY_PREDICATES_MAP, | ||
**ARITHMETIC_MAP, | ||
**CONDITION_MAP, | ||
} | ||
|
||
|
||
def get_op(node: ast.Node) -> Union[str, None]: | ||
# Get the cql2 operator string from a node. | ||
for k, v in BINARY_OP_PREDICATES_MAP.items(): | ||
if isinstance(node, v): | ||
return k | ||
return None |
Oops, something went wrong.