Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 12 additions & 4 deletions pygeofilter/backends/cql2_json/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,14 +103,22 @@ def attribute(self, node: ast.Attribute):
def interval(self, node: values.Interval, start, end):
return {"interval": [start, end]}

@handle(datetime)
def datetime(self, node: ast.Attribute):
return {"timestamp": node.name}

@handle(*values.LITERALS)
def literal(self, node):
return node

@handle(date)
def date_(self, node: date):
return {"date": node.isoformat()}

@handle(datetime)
def datetime_(self, node: datetime):
if node.microsecond:
ts = node.strftime("%Y-%m-%dT%H:%M:%S.") + f"{node.microsecond:06d}Z"
else:
ts = node.strftime("%Y-%m-%dT%H:%M:%SZ")
return {"timestamp": ts}

@handle(values.Geometry)
def geometry(self, node: values.Geometry):
return node.__geo_interface__
Expand Down
2 changes: 1 addition & 1 deletion pygeofilter/parsers/cql2_text/grammar.lark
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ DATETIME: /[0-9]{4}-?[0-1][0-9]-?[0-3][0-9][T ][0-2][0-9]:?[0-5][0-9]:?[0-5][0-9
?interval: "INTERVAL" "(" "'" DATETIME "'" "," "'" DATETIME "'" ")"
?date: "DATE" "(" "'" DATE "'" ")"

attribute: /[a-zA-Z][a-zA-Z_:0-9.]+/
attribute: /[a-zA-Z][a-zA-Z_:0-9.]*/
| DOUBLE_QUOTED


Expand Down
2 changes: 0 additions & 2 deletions pygeofilter/parsers/wkt.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@
@v_args(meta=False, inline=True)
class WKTTransformer(Transformer):
def wkt__geometry_with_srid(self, srid, geometry):
print(srid, geometry)
geometry["crs"] = {
"type": "name",
"properties": {"name": f"urn:ogc:def:crs:EPSG::{srid}"},
Expand Down Expand Up @@ -66,7 +65,6 @@ def wkt__multipoint(self, coordinates):
}

def wkt__multipoint_2(self, *coordinates):
print(coordinates)
return {
"type": "MultiPoint",
"coordinates": coordinates,
Expand Down
16 changes: 16 additions & 0 deletions tests/parsers/cql2_json/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
from pygeoif import geometry

from pygeofilter import ast, values
from pygeofilter.backends.cql2_json.evaluate import to_cql2
from pygeofilter.parsers.cql2_json import parse


Expand Down Expand Up @@ -717,3 +718,18 @@ def test_function_attr_string_arg():
],
),
)


def test_encode_date():
node = ast.Equal(ast.Attribute("attr"), date(2000, 1, 1))
result = json.loads(to_cql2(node))
assert result["args"][1] == {"date": "2000-01-01"}


def test_encode_timestamp():
node = ast.Equal(
ast.Attribute("attr"),
datetime(2000, 1, 1, 0, 0, 0, tzinfo=StaticTzInfo("Z", timedelta(0))),
)
result = json.loads(to_cql2(node))
assert result["args"][1] == {"timestamp": "2000-01-01T00:00:00Z"}
1 change: 1 addition & 0 deletions tests/parsers/cql2_text/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -477,3 +477,4 @@ def test_not_eq():
assert result == ast.Not(
ast.Equal(ast.Attribute("attr"), 2)
)

Loading