web/lib/django/contrib/gis/db/backends/util.py
changeset 29 cc9b7e14412b
equal deleted inserted replaced
28:b758351d191f 29:cc9b7e14412b
       
     1 """
       
     2 A collection of utility routines and classes used by the spatial
       
     3 backends.
       
     4 """
       
     5 
       
     6 def getstatusoutput(cmd):
       
     7     """
       
     8     Executes a shell command on the platform using subprocess.Popen and
       
     9     return a tuple of the status and stdout output.
       
    10     """
       
    11     from subprocess import Popen, PIPE
       
    12     # Set stdout and stderr to PIPE because we want to capture stdout and
       
    13     # prevent stderr from displaying.
       
    14     p = Popen(cmd, shell=True, stdout=PIPE, stderr=PIPE)
       
    15     # We use p.communicate() instead of p.wait() to avoid deadlocks if the
       
    16     # output buffers exceed POSIX buffer size.
       
    17     stdout, stderr = p.communicate()
       
    18     return p.returncode, stdout.strip()
       
    19 
       
    20 def gqn(val):
       
    21     """
       
    22     The geographic quote name function; used for quoting tables and
       
    23     geometries (they use single rather than the double quotes of the
       
    24     backend quotename function).
       
    25     """
       
    26     if isinstance(val, basestring):
       
    27         if isinstance(val, unicode): val = val.encode('ascii')
       
    28         return "'%s'" % val
       
    29     else:
       
    30         return str(val)
       
    31 
       
    32 class SpatialOperation(object):
       
    33     """
       
    34     Base class for generating spatial SQL.
       
    35     """
       
    36     sql_template = '%(geo_col)s %(operator)s %(geometry)s'
       
    37 
       
    38     def __init__(self, function='', operator='', result='', **kwargs):
       
    39         self.function = function
       
    40         self.operator = operator
       
    41         self.result = result
       
    42         self.extra = kwargs
       
    43 
       
    44     def as_sql(self, geo_col, geometry='%s'):
       
    45         return self.sql_template % self.params(geo_col, geometry)
       
    46 
       
    47     def params(self, geo_col, geometry):
       
    48         params = {'function' : self.function,
       
    49                   'geo_col' : geo_col,
       
    50                   'geometry' : geometry,
       
    51                   'operator' : self.operator,
       
    52                   'result' : self.result,
       
    53                   }
       
    54         params.update(self.extra)
       
    55         return params
       
    56 
       
    57 class SpatialFunction(SpatialOperation):
       
    58     """
       
    59     Base class for generating spatial SQL related to a function.
       
    60     """
       
    61     sql_template = '%(function)s(%(geo_col)s, %(geometry)s)'
       
    62 
       
    63     def __init__(self, func, result='', operator='', **kwargs):
       
    64         # Getting the function prefix.
       
    65         default = {'function' : func,
       
    66                    'operator' : operator,
       
    67                    'result' : result
       
    68                    }
       
    69         kwargs.update(default)
       
    70         super(SpatialFunction, self).__init__(**kwargs)