Skip to content

Commit

Permalink
Merge pull request #42 from bitner/removeshapelyfromcql2json
Browse files Browse the repository at this point in the history
remove shapely dependency from cql2_json
  • Loading branch information
constantinius authored Feb 8, 2022
2 parents f4e5b5f + 34980b8 commit 117ec52
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
24 changes: 16 additions & 8 deletions pygeofilter/backends/cql2_json/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,21 @@
# ------------------------------------------------------------------------------

from typing import Dict, Optional
from datetime import datetime

import shapely.geometry
from datetime import datetime, date

from ..evaluator import Evaluator, handle
from ... import ast
from ...cql2 import get_op
from ... import values
import json


def json_serializer(obj):
if isinstance(obj, (datetime, date)):
return obj.isoformat()
if hasattr(obj, "name"):
return obj.name
raise TypeError(f"{obj} with type {type(obj)} is not serializable.")


class CQL2Evaluator(Evaluator):
Expand Down Expand Up @@ -102,18 +109,19 @@ def literal(self, node):

@handle(values.Geometry)
def geometry(self, node: values.Geometry):
return shapely.geometry.shape(node).__geo_interface__
return 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__
return node.__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)
return json.dumps(
CQL2Evaluator(field_mapping, function_map).evaluate(root),
default=json_serializer,
)
22 changes: 22 additions & 0 deletions pygeofilter/values.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ class Envelope:
y1: float
y2: float

@property
def geometry(self):
return {
"type": "Polygon",
"coordinates": [[
[self.x1, self.y1],
[self.x1, self.y2],
[self.x2, self.y2],
[self.x2, self.y1],
[self.x1, self.y1]
]]
}

@property
def __geo_interface__(self):
return self.geometry

def __eq__(self, o: object) -> bool:
return (
as_shape(self).__geo_interface__ == as_shape(o).__geo_interface__
)


@dataclass
class Interval:
Expand Down

0 comments on commit 117ec52

Please sign in to comment.