Django

Code

Changeset 4788

Show
Ignore:
Timestamp:
03/23/07 12:47:48 (2 years ago)
Author:
jdunck
Message:

gis: And added missing fields init.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/gis/django/contrib/gis/db/models/fields/__init__.py

    r4675 r4788  
    1 from django.db import models 
     1# The Django base Field class. 
    22from django.db.models.fields import Field 
    3 #from GeoTypes import Point 
     3from django.contrib.gis.db.models.postgis import POSTGIS_TERMS 
    44 
    5 # Creates the SQL to add the model to the database.  Defaults to using an 
    6 # SRID of 4326 (WGS84 Datum -- 'normal' lat/lon coordinates) 
     5# Creates the SQL to add the model to the database.  
    76def _add_geom(geom, srid, style, model, field, dim=2): 
    8     from django.db import backend 
    9  
    107    # Constructing the AddGeometryColumn(...) command -- the style 
    118    # object is passed in from the management module and is used 
     
    1613          style.SQL_FIELD(field) + "', " + \ 
    1714          style.SQL_FIELD(str(srid)) + ", '" + \ 
    18           style.SQL_KEYWORD(geom) + "', " + \ 
     15          style.SQL_COLTYPE(geom) + "', " + \ 
    1916          style.SQL_KEYWORD(str(dim)) + \ 
    2017          ');' 
    2118    return sql 
    2219 
     20# Creates an index for the given geometry. 
     21def _geom_index(geom, style, model, field, 
     22                index_type='GIST', 
     23                index_opts='GIST_GEOMETRY_OPTS'): 
     24    sql = style.SQL_KEYWORD('CREATE INDEX ') + \ 
     25          style.SQL_FIELD(field + '_idx') + \ 
     26          style.SQL_KEYWORD(' ON ') + \ 
     27          style.SQL_TABLE(model) + \ 
     28          style.SQL_KEYWORD(' USING ') + \ 
     29          style.SQL_COLTYPE(index_type) + ' ( ' + \ 
     30          style.SQL_FIELD(field) + ' ' + \ 
     31          style.SQL_KEYWORD(index_opts) + ' );' 
     32    return sql 
     33 
    2334class GeometryField(Field): 
    24     """The base GIS field -- maps to an OpenGIS Geometry type.""" 
    25      
     35    "The base GIS field -- maps to the OpenGIS Geometry type." 
     36 
     37    # The OpenGIS Geometry name. 
    2638    _geom = 'GEOMETRY' 
    27     _srid = 4326 
     39 
     40    def __init__(self, srid=4326, index=False, **kwargs): 
     41        # Calling the Field initialization function first 
     42        super(GeometryField, self).__init__(**kwargs) 
     43 
     44        # The SRID for the geometry, defaults to 4326 
     45        self._srid = srid 
     46        self._index = index 
    2847 
    2948    def get_internal_type(self): 
     
    3352        """Returns SQL that will be executed after the model has been created.  Geometry 
    3453        columns must be added after creation with the PostGIS AddGeometryColumn() function.""" 
    35         return _add_geom(self._geom, self._srid, *args, **kwargs) 
     54        post_sql = _add_geom(self._geom, self._srid, *args, **kwargs) 
     55        if self._index: 
     56            # Creating geometry indices doesn't yet work. 
     57            #return '%s\n%s' % (post_sql, _geom_index(self._geom, *args, **kwargs)) 
     58            return post_sql 
     59        else: 
     60            return post_sql 
    3661 
    3762    def get_db_prep_lookup(self, lookup_type, value): 
    38         "Returns field's value prepared for database lookup." 
    39         if lookup_type in ('exact', 'gt', 'gte', 'lt', 'lte', 'month', 'day', 'search', 'overlaps'): 
    40             return [value] 
    41         elif lookup_type in ('range', 'in'): 
    42             return value 
    43         elif lookup_type in ('contains', 'icontains'): 
    44             return ["%%%s%%" % prep_for_like_query(value)] 
    45         elif lookup_type == 'iexact': 
    46             return [prep_for_like_query(value)] 
    47         elif lookup_type in ('startswith', 'istartswith'): 
    48             return ["%s%%" % prep_for_like_query(value)] 
    49         elif lookup_type in ('endswith', 'iendswith'): 
    50             return ["%%%s" % prep_for_like_query(value)] 
    51         elif lookup_type == 'isnull': 
    52             return [] 
    53         elif lookup_type == 'year': 
    54             try: 
    55                 value = int(value) 
    56             except ValueError: 
    57                 raise ValueError("The __year lookup type requires an integer argument") 
    58             return ['%s-01-01 00:00:00' % value, '%s-12-31 23:59:59.999999' % value] 
     63        """Returns field's value prepared for database lookup; the SRID of the geometry is 
     64        included by default in these queries.""" 
     65        if lookup_type in POSTGIS_TERMS: 
     66            return ['SRID=%d;%s' % (self._srid, value)] 
    5967        raise TypeError("Field has invalid lookup: %s" % lookup_type) 
    6068 
    6169    def get_db_prep_save(self, value): 
     70        "Making sure the SRID is included before saving." 
    6271        return 'SRID=%d;%s' % (self._srid, value) 
    6372     
    64  
     73# The OpenGIS Geometry Type Fields 
    6574class PointField(GeometryField): 
    6675    _geom = 'POINT' 
     76 
     77class LineString(GeometryField): 
     78    _geom = 'LINESTRING' 
    6779 
    6880class PolygonField(GeometryField): 
    6981    _geom = 'POLYGON' 
    7082 
     83class MultiPointField(GeometryField): 
     84    _geom = 'MULTIPOINT' 
     85 
     86class MultiLineStringField(GeometryField): 
     87    _geom = 'MULTILINESTRING' 
     88 
    7189class MultiPolygonField(GeometryField): 
    7290    _geom = 'MULTIPOLYGON' 
    7391 
    74 class GeometryManager(models.Manager): 
    75     pass 
     92class GeometryCollectionField(GeometryField): 
     93    _geom = 'GEOMETRYCOLLECTION'