| 13 | | def get_srid(field, geom): |
|---|
| | 13 | def as_sql(self, quote=False): |
|---|
| | 14 | if not quote: |
|---|
| | 15 | return self.where[0] % tuple(self.params) |
|---|
| | 16 | else: |
|---|
| | 17 | # Used for quoting WKT on certain backends. |
|---|
| | 18 | tmp_params = ["'%s'" % self.params[0]] |
|---|
| | 19 | tmp_params.extend(self.params[1:]) |
|---|
| | 20 | return self.where[0] % tuple(tmp_params) |
|---|
| | 21 | |
|---|
| | 22 | class SpatialOperation(object): |
|---|
| 18 | | if geom.srid is None or (geom.srid == -1 and field._srid != -1): |
|---|
| 19 | | return field._srid |
|---|
| 20 | | else: |
|---|
| 21 | | return geom.srid |
|---|
| | 26 | def __init__(self, function='', operator='', result='', beg_subst='', end_subst=''): |
|---|
| | 27 | self.function = function |
|---|
| | 28 | self.operator = operator |
|---|
| | 29 | self.result = result |
|---|
| | 30 | self.beg_subst = beg_subst |
|---|
| | 31 | try: |
|---|
| | 32 | # Try and put the operator and result into to the |
|---|
| | 33 | # end substitution. |
|---|
| | 34 | self.end_subst = end_subst % (operator, result) |
|---|
| | 35 | except TypeError: |
|---|
| | 36 | self.end_subst = end_subst |
|---|
| | 37 | |
|---|
| | 38 | @property |
|---|
| | 39 | def sql_subst(self): |
|---|
| | 40 | return ''.join([self.beg_subst, self.end_subst]) |
|---|
| | 41 | |
|---|
| | 42 | def as_sql(self, geo_col): |
|---|
| | 43 | return self.sql_subst % self.params(geo_col) |
|---|
| | 44 | |
|---|
| | 45 | def params(self, geo_col): |
|---|
| | 46 | return (geo_col, self.operator) |
|---|
| | 47 | |
|---|
| | 48 | class SpatialFunction(SpatialOperation): |
|---|
| | 49 | """ |
|---|
| | 50 | Base class for generating spatial SQL related to a function. |
|---|
| | 51 | """ |
|---|
| | 52 | def __init__(self, func, beg_subst='%s(%s, %%s', end_subst=')', result='', operator=''): |
|---|
| | 53 | # Getting the function prefix. |
|---|
| | 54 | kwargs = {'function' : func, 'operator' : operator, 'result' : result, |
|---|
| | 55 | 'beg_subst' : beg_subst, 'end_subst' : end_subst,} |
|---|
| | 56 | super(SpatialFunction, self).__init__(**kwargs) |
|---|
| | 57 | |
|---|
| | 58 | def params(self, geo_col): |
|---|
| | 59 | return (self.function, geo_col) |
|---|