Changeset 4788
- Timestamp:
- 03/23/07 12:47:48 (2 years ago)
- 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. 2 2 from django.db.models.fields import Field 3 #from GeoTypes import Point 3 from django.contrib.gis.db.models.postgis import POSTGIS_TERMS 4 4 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. 7 6 def _add_geom(geom, srid, style, model, field, dim=2): 8 from django.db import backend9 10 7 # Constructing the AddGeometryColumn(...) command -- the style 11 8 # object is passed in from the management module and is used … … 16 13 style.SQL_FIELD(field) + "', " + \ 17 14 style.SQL_FIELD(str(srid)) + ", '" + \ 18 style.SQL_ KEYWORD(geom) + "', " + \15 style.SQL_COLTYPE(geom) + "', " + \ 19 16 style.SQL_KEYWORD(str(dim)) + \ 20 17 ');' 21 18 return sql 22 19 20 # Creates an index for the given geometry. 21 def _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 23 34 class 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. 26 38 _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 28 47 29 48 def get_internal_type(self): … … 33 52 """Returns SQL that will be executed after the model has been created. Geometry 34 53 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 36 61 37 62 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)] 59 67 raise TypeError("Field has invalid lookup: %s" % lookup_type) 60 68 61 69 def get_db_prep_save(self, value): 70 "Making sure the SRID is included before saving." 62 71 return 'SRID=%d;%s' % (self._srid, value) 63 72 64 73 # The OpenGIS Geometry Type Fields 65 74 class PointField(GeometryField): 66 75 _geom = 'POINT' 76 77 class LineString(GeometryField): 78 _geom = 'LINESTRING' 67 79 68 80 class PolygonField(GeometryField): 69 81 _geom = 'POLYGON' 70 82 83 class MultiPointField(GeometryField): 84 _geom = 'MULTIPOINT' 85 86 class MultiLineStringField(GeometryField): 87 _geom = 'MULTILINESTRING' 88 71 89 class MultiPolygonField(GeometryField): 72 90 _geom = 'MULTIPOLYGON' 73 91 74 class Geometry Manager(models.Manager):75 pass92 class GeometryCollectionField(GeometryField): 93 _geom = 'GEOMETRYCOLLECTION'
