Django

Code

Show
Ignore:
Timestamp:
02/10/08 20:25:01 (1 year ago)
Author:
jbronn
Message:

gis: Fixed 6414, and applied DRY to spatial backend internals. Changes include:

(1) Support for distance calculations on geometry fields with geodetic coordinate systems (e.g., WGS84, the default).
(2) The get_db_prep_save and get_db_prep_lookup have been moved from the spatial backends to common implementations in GeometryField.
(3) Simplified SQL construction for GeoQuerySet methods.
(4) SpatialBackend now contains all spatial backend dependent settings.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/db/backend/mysql/field.py

    r6527 r7104  
    1 import re 
    2 from types import StringType, UnicodeType 
    31from django.db import connection 
    42from django.db.models.fields import Field # Django base Field class 
    5 from django.contrib.gis.geos import GEOSGeometry 
    6 from django.contrib.gis.db.backend.util import GeoFieldSQL 
    7 from django.contrib.gis.db.backend.mysql.query import MYSQL_GIS_TERMS, GEOM_FROM_TEXT 
     3from django.contrib.gis.db.backend.mysql.query import GEOM_FROM_TEXT 
    84 
    95# Quotename & geographic quotename, respectively. 
    106qn = connection.ops.quote_name 
    11 def gqn(value): 
    12     if isinstance(value, UnicodeType): value = value.encode('ascii') 
    13     return "'%s'" % value 
    147 
    158class MySQLGeoField(Field): 
     
    2417        Thus, for best spatial performance, you should use MyISAM tables 
    2518        (which do not support transactions).  For more information, see Ch.  
    26         17.6.1 of the MySQL 5.0 documentation. 
     19        16.6.1 of the MySQL 5.0 documentation. 
    2720        """ 
    2821 
     
    5144        "The OpenGIS name is returned for the MySQL database column type." 
    5245        return self._geom 
    53          
    54     def get_db_prep_lookup(self, lookup_type, value): 
    55         """ 
    56         Returns field's value prepared for database lookup, accepts WKT and  
    57         GEOS Geometries for the value. 
    58         """ 
    59         if lookup_type in MYSQL_GIS_TERMS: 
    60             # special case for isnull lookup 
    61             if lookup_type == 'isnull': return GeoFieldSQL([], []) 
    62  
    63             # When the input is not a GEOS geometry, attempt to construct one 
    64             # from the given string input. 
    65             if isinstance(value, GEOSGeometry): 
    66                 pass 
    67             elif isinstance(value, (StringType, UnicodeType)): 
    68                 try: 
    69                     value = GEOSGeometry(value) 
    70                 except GEOSException: 
    71                     raise TypeError("Could not create geometry from lookup value: %s" % str(value)) 
    72             else: 
    73                 raise TypeError('Cannot use parameter of %s type as lookup parameter.' % type(value)) 
    74  
    75             return GeoFieldSQL(['%s(%%s)' % GEOM_FROM_TEXT], [value]) 
    76  
    77         else: 
    78             raise TypeError("Field has invalid lookup: %s" % lookup_type) 
    79  
    80     def get_db_prep_save(self, value): 
    81         "Prepares the value for saving in the database." 
    82         if not bool(value): return None 
    83         if isinstance(value, GEOSGeometry): 
    84             return value 
    85         else: 
    86             raise TypeError('Geometry Proxy should only return GEOSGeometry objects.') 
    8746 
    8847    def get_placeholder(self, value): 
    8948        """ 
    90         Nothing special happens here because MySQL does not support transformations. 
     49        The placeholder here has to include MySQL's WKT constructor.  Because  
     50        MySQL does not support spatial transformations, there is no need to  
     51        modify the placeholder based on the contents of the given value. 
    9152        """ 
    9253        return '%s(%%s)' % GEOM_FROM_TEXT