/django/contrib/gis/db/backends/util.py

https://code.google.com/p/mango-py/ · Python · 56 lines · 33 code · 7 blank · 16 comment · 3 complexity · baca56eb5a2eadf4da017f994ab14614 MD5 · raw file

  1. """
  2. A collection of utility routines and classes used by the spatial
  3. backends.
  4. """
  5. def gqn(val):
  6. """
  7. The geographic quote name function; used for quoting tables and
  8. geometries (they use single rather than the double quotes of the
  9. backend quotename function).
  10. """
  11. if isinstance(val, basestring):
  12. if isinstance(val, unicode): val = val.encode('ascii')
  13. return "'%s'" % val
  14. else:
  15. return str(val)
  16. class SpatialOperation(object):
  17. """
  18. Base class for generating spatial SQL.
  19. """
  20. sql_template = '%(geo_col)s %(operator)s %(geometry)s'
  21. def __init__(self, function='', operator='', result='', **kwargs):
  22. self.function = function
  23. self.operator = operator
  24. self.result = result
  25. self.extra = kwargs
  26. def as_sql(self, geo_col, geometry='%s'):
  27. return self.sql_template % self.params(geo_col, geometry)
  28. def params(self, geo_col, geometry):
  29. params = {'function' : self.function,
  30. 'geo_col' : geo_col,
  31. 'geometry' : geometry,
  32. 'operator' : self.operator,
  33. 'result' : self.result,
  34. }
  35. params.update(self.extra)
  36. return params
  37. class SpatialFunction(SpatialOperation):
  38. """
  39. Base class for generating spatial SQL related to a function.
  40. """
  41. sql_template = '%(function)s(%(geo_col)s, %(geometry)s)'
  42. def __init__(self, func, result='', operator='', **kwargs):
  43. # Getting the function prefix.
  44. default = {'function' : func,
  45. 'operator' : operator,
  46. 'result' : result
  47. }
  48. kwargs.update(default)
  49. super(SpatialFunction, self).__init__(**kwargs)