2009-03-31 01:15:49 +08:00
|
|
|
"""
|
|
|
|
A collection of utility routines and classes used by the spatial
|
|
|
|
backends.
|
|
|
|
"""
|
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class SpatialOperation(object):
|
|
|
|
"""
|
|
|
|
Base class for generating spatial SQL.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
sql_template = '%(geo_col)s %(operator)s %(geometry)s'
|
|
|
|
|
|
|
|
def __init__(self, function='', operator='', result='', **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
self.function = function
|
|
|
|
self.operator = operator
|
|
|
|
self.result = result
|
2009-12-22 23:18:51 +08:00
|
|
|
self.extra = kwargs
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def as_sql(self, geo_col, geometry='%s'):
|
2013-02-10 23:15:49 +08:00
|
|
|
return self.sql_template % self.params(geo_col, geometry), []
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-12-22 23:18:51 +08:00
|
|
|
def params(self, geo_col, geometry):
|
2013-10-27 09:27:42 +08:00
|
|
|
params = {'function': self.function,
|
|
|
|
'geo_col': geo_col,
|
|
|
|
'geometry': geometry,
|
|
|
|
'operator': self.operator,
|
|
|
|
'result': self.result,
|
2009-12-22 23:18:51 +08:00
|
|
|
}
|
|
|
|
params.update(self.extra)
|
|
|
|
return params
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2013-11-03 01:18:46 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
class SpatialFunction(SpatialOperation):
|
|
|
|
"""
|
|
|
|
Base class for generating spatial SQL related to a function.
|
|
|
|
"""
|
2009-12-22 23:18:51 +08:00
|
|
|
sql_template = '%(function)s(%(geo_col)s, %(geometry)s)'
|
|
|
|
|
|
|
|
def __init__(self, func, result='', operator='', **kwargs):
|
2008-08-06 02:13:06 +08:00
|
|
|
# Getting the function prefix.
|
2013-10-27 09:27:42 +08:00
|
|
|
default = {'function': func,
|
|
|
|
'operator': operator,
|
|
|
|
'result': result
|
2009-12-22 23:18:51 +08:00
|
|
|
}
|
|
|
|
kwargs.update(default)
|
2008-08-06 02:13:06 +08:00
|
|
|
super(SpatialFunction, self).__init__(**kwargs)
|