PageRenderTime 102ms CodeModel.GetById 60ms app.highlight 5ms RepoModel.GetById 35ms app.codeStats 0ms

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

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