This module defines a Comparator class for use with geometry and geography objects. This is where spatial operators, like &&, &<, are defined. Spatial operators very often apply to the bounding boxes of geometries. For example, geom1 && geom2 indicates if geom1’s bounding box intersects geom2’s.
Select the objects whose bounding boxes are to the left of the bounding box of POLYGON((-5 45,5 45,5 -45,-5 -45,-5 45)):
select([table]).where(table.c.geom.to_left(
'POLYGON((-5 45,5 45,5 -45,-5 -45,-5 45))'))
The << and >> operators are a bit specific, because they have corresponding Python operator (__lshift__ and __rshift__). The above SELECT expression can thus be rewritten like this:
select([table]).where(
table.c.geom << 'POLYGON((-5 45,5 45,5 -45,-5 -45,-5 45))')
Operators can also be used when using the ORM. For example:
Session.query(Cls).filter(
Cls.geom << 'POLYGON((-5 45,5 45,5 -45,-5 -45,-5 45))')
Now some other examples with the <#> operator.
Select the ten objects that are the closest to POINT(0 0) (typical closed neighbors problem):
select([table]).order_by(table.c.geom.distance_box('POINT(0 0)')).limit(10)
Using the ORM:
Session.query(Cls).order_by(Cls.geom.distance_box('POINT(0 0)')).limit(10)
Bases: sqlalchemy.sql.type_api.Comparator
A custom comparator base class. It adds the ability to call spatial functions on columns that use this kind of comparator. It also defines functions that map to operators supported by Geometry, Geography and Raster columns.
This comparator is used by the geoalchemy2.types.Raster.
Bases: geoalchemy2.comparator.BaseComparator
A custom comparator class. Used in geoalchemy2.types.Geometry and geoalchemy2.types.Geography.
This is where spatial operators like << and <-> are defined.
The << operator. A’s BBOX is strictly to the left of B’s. Same as to_left, so:
table.c.geom << 'POINT(1 2)'
is the same as:
table.c.geom.to_left('POINT(1 2)')