utils.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. """
  2. A collection of utility routines and classes used by the spatial
  3. backends.
  4. """
  5. class SpatialOperation(object):
  6. """
  7. Base class for generating spatial SQL.
  8. """
  9. sql_template = '%(geo_col)s %(operator)s %(geometry)s'
  10. def __init__(self, function='', operator='', result='', **kwargs):
  11. self.function = function
  12. self.operator = operator
  13. self.result = result
  14. self.extra = kwargs
  15. def as_sql(self, geo_col, geometry='%s'):
  16. return self.sql_template % self.params(geo_col, geometry), []
  17. def params(self, geo_col, geometry):
  18. params = {'function': self.function,
  19. 'geo_col': geo_col,
  20. 'geometry': geometry,
  21. 'operator': self.operator,
  22. 'result': self.result,
  23. }
  24. params.update(self.extra)
  25. return params
  26. class SpatialFunction(SpatialOperation):
  27. """
  28. Base class for generating spatial SQL related to a function.
  29. """
  30. sql_template = '%(function)s(%(geo_col)s, %(geometry)s)'
  31. def __init__(self, func, result='', operator='', **kwargs):
  32. # Getting the function prefix.
  33. default = {'function': func,
  34. 'operator': operator,
  35. 'result': result
  36. }
  37. kwargs.update(default)
  38. super(SpatialFunction, self).__init__(**kwargs)