Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

ST_ functions improvements #33

Closed
wants to merge 2 commits into from

1 participant

@Turbo87
Owner

No description provided.

@Turbo87
Owner

@elemoine I would have liked to move the ST_FunctionMixin class to the functions module but then there is a circular dependency: __init__ > types > comparator > functions > types and I don't know how to solve it.

@Turbo87
Owner

nevermind... doesn't seem to work quite as well as I had imagined it...

@Turbo87 Turbo87 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 21 deletions.
  1. +19 −5 geoalchemy2/comparator.py
  2. +2 −16 geoalchemy2/elements.py
View
24 geoalchemy2/comparator.py
@@ -46,12 +46,12 @@
from sqlalchemy.sql import expression
-class Comparator(UserDefinedType.Comparator):
+class ST_FunctionMixin(object):
"""
- A custom comparator class. Used in :class:`geoalchemy2.types.Geometry`
- and :class:`geoalchemy2.types.Geography`.
+ This is how things like lake.geom.ST_Buffer(2) creates SQL expressions
+ of this form:
- This is where spatial operators like ``&&`` and ``&<`` are defined.
+ ST_Buffer(ST_GeomFromWKB(:ST_GeomFromWKB_1), :param_1)
"""
def __getattr__(self, name):
@@ -67,9 +67,23 @@ def __getattr__(self, name):
# SQLAlchemy's "func" object. This is to be able to "bind" the
# function to the SQL expression. See also GenericFunction above.
- func_ = expression._FunctionGenerator(expr=self.expr)
+ if hasattr(self, 'expr'):
+ expr = self.expr
+ else:
+ expr = self
+
+ func_ = expression._FunctionGenerator(expr=expr)
return getattr(func_, name)
+
+class Comparator(UserDefinedType.Comparator, ST_FunctionMixin):
+ """
+ A custom comparator class. Used in :class:`geoalchemy2.types.Geometry`
+ and :class:`geoalchemy2.types.Geography`.
+
+ This is where spatial operators like ``&&`` and ``&<`` are defined.
+ """
+
def intersects(self, other):
"""
The ``&&`` operator. A's BBOX intersects B's.
View
18 geoalchemy2/elements.py
@@ -1,9 +1,10 @@
import binascii
from sqlalchemy.sql import expression
+from .comparator import ST_FunctionMixin
-class _SpatialElement(object):
+class _SpatialElement(ST_FunctionMixin):
"""
The base class for :class:`geoalchemy2.elements.WKTElement` and
:class:`geoalchemy2.elements.WKBElement`.
@@ -78,18 +79,3 @@ def desc(self):
This element's description string.
"""
return binascii.hexlify(self.data)
-
- def __getattr__(self, name):
- #
- # This is how things like lake.geom.ST_Buffer(2) creates
- # SQL expressions of this form:
- #
- # ST_Buffer(ST_GeomFromWKB(:ST_GeomFromWKB_1), :param_1)
- #
-
- # We create our own _FunctionGenerator here, and use it in place of
- # SQLAlchemy's "func" object. This is to be able to "bind" the
- # function to the SQL expression. See also GenericFunction above.
-
- func_ = expression._FunctionGenerator(expr=self)
- return getattr(func_, name)
Something went wrong with that request. Please try again.