Django

Code

Changeset 5448

Show
Ignore:
Timestamp:
06/09/07 14:55:42 (2 years ago)
Author:
jbronn
Message:

gis: NULL geometries are now allowed (thanks Robert Coup!), field parameter no longer needed for _post_create_sql(), and added extra instance methods for SpatialReference?/SRID.

Files:

Legend:

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

    r5397 r5448  
    3434        super(GeometryField, self).__init__(**kwargs) # Calling the parent initializtion function 
    3535 
    36     def _add_geom(self, style, db_table, field): 
     36    def _add_geom(self, style, db_table): 
    3737        """Constructs the addition of the geometry to the table using the 
    38         AddGeometryColumn(...) PostGIS (and OGC standard) function
     38        AddGeometryColumn(...) PostGIS (and OGC standard) stored procedure
    3939 
    40         Takes the style object (provides syntax highlighting) as well as the 
    41          database table and field
     40        Takes the style object (provides syntax highlighting) and the 
     41         database table as parameters
    4242        """ 
    4343        sql = style.SQL_KEYWORD('SELECT ') + \ 
    4444              style.SQL_TABLE('AddGeometryColumn') + '(' + \ 
    4545              style.SQL_TABLE(quotename(db_table)) + ', ' + \ 
    46               style.SQL_FIELD(quotename(field)) + ', ' + \ 
     46              style.SQL_FIELD(quotename(self.column)) + ', ' + \ 
    4747              style.SQL_FIELD(str(self._srid)) + ', ' + \ 
    4848              style.SQL_COLTYPE(quotename(self._geom)) + ', ' + \ 
    4949              style.SQL_KEYWORD(str(self._dim)) + ');' 
     50 
     51        if not self.null: 
     52            # Add a NOT NULL constraint to the field 
     53            sql += '\n' + \ 
     54                   style.SQL_KEYWORD('ALTER TABLE ') + \ 
     55                   style.SQL_TABLE(quotename(db_table, dbl=True)) + \ 
     56                   style.SQL_KEYWORD(' ALTER ') + \ 
     57                   style.SQL_FIELD(quotename(self.column, dbl=True)) + \ 
     58                   style.SQL_KEYWORD(' SET NOT NULL') + ';'  
    5059        return sql 
    5160 
    52     def _geom_index(self, style, db_table, field, 
     61    def _geom_index(self, style, db_table, 
    5362                    index_type='GIST', index_opts='GIST_GEOMETRY_OPS'): 
    5463        "Creates a GiST index for this geometry field." 
    5564        sql = style.SQL_KEYWORD('CREATE INDEX ') + \ 
    56               style.SQL_TABLE(quotename('%s_%s_id' % (db_table, field), dbl=True)) + \ 
     65              style.SQL_TABLE(quotename('%s_%s_id' % (db_table, self.column), dbl=True)) + \ 
    5766              style.SQL_KEYWORD(' ON ') + \ 
    5867              style.SQL_TABLE(quotename(db_table, dbl=True)) + \ 
    5968              style.SQL_KEYWORD(' USING ') + \ 
    6069              style.SQL_COLTYPE(index_type) + ' ( ' + \ 
    61               style.SQL_FIELD(quotename(field, dbl=True)) + ' ' + \ 
     70              style.SQL_FIELD(quotename(self.column, dbl=True)) + ' ' + \ 
    6271              style.SQL_KEYWORD(index_opts) + ' );' 
    6372        return sql 
    6473 
    65     def _post_create_sql(self, style, db_table, field): 
     74    def _post_create_sql(self, style, db_table): 
    6675        """Returns SQL that will be executed after the model has been 
    6776        created. Geometry columns must be added after creation with the 
     
    7079        # Getting the AddGeometryColumn() SQL necessary to create a PostGIS 
    7180        # geometry field. 
    72         post_sql = self._add_geom(style, db_table, field
     81        post_sql = self._add_geom(style, db_table
    7382 
    7483        # If the user wants to index this data, then get the indexing SQL as well. 
    7584        if self._index: 
    76             return '%s\n%s' % (post_sql, self._geom_index(style, db_table, field)) 
     85            return '%s\n%s' % (post_sql, self._geom_index(style, db_table)) 
    7786        else: 
    7887            return post_sql 
     
    8493        setattr(cls, 'get_%s_geos' % self.name, curry(cls._get_GEOM_geos, field=self)) 
    8594        setattr(cls, 'get_%s_ogr' % self.name, curry(cls._get_GEOM_ogr, field=self, srid=self._srid)) 
     95        setattr(cls, 'get_%s_srid' % self.name, curry(cls._get_GEOM_srid, srid=self._srid)) 
     96        setattr(cls, 'get_%s_srs' % self.name, curry(cls._get_GEOM_srs, srid=self._srid)) 
    8697        setattr(cls, 'get_%s_wkt' % self.name, curry(cls._get_GEOM_wkt, field=self)) 
    8798        setattr(cls, 'get_%s_centroid' % self.name, curry(cls._get_GEOM_centroid, field=self)) 
     
    95106        included by default in these queries.""" 
    96107        if lookup_type in POSTGIS_TERMS: 
    97             return ['SRID=%d;%s' % (self._srid, value)
     108            return [value and ("SRID=%d;%s" % (self._srid, value)) or None
    98109        raise TypeError("Field has invalid lookup: %s" % lookup_type) 
    99110 
    100111    def get_db_prep_save(self, value): 
    101112        "Making sure the SRID is included before saving." 
    102         return 'SRID=%d;%s' % (self._srid, value) 
     113        return value and ("SRID=%d;%s" % (self._srid, value)) or None 
    103114 
    104115    def get_manipulator_field_objs(self): 
  • django/branches/gis/django/contrib/gis/db/models/GeoMixin.py

    r5397 r5448  
    66# the necessary functions that may be contributed for geographic objects. 
    77class GeoMixin: 
    8     "The Geographic Mixin class, provides routines for geographic objects." 
     8    "The Geographic Mixin class provides routines for geographic objects." 
    99 
    1010    # A subclass of Model is specifically needed so that these geographic 
    1111    # routines are present for instantiations of the models. 
    1212    def _get_GEOM_geos(self, field): 
    13         "Gets a GEOS Python object for the geometry." 
     13        "Returns a GEOS Python object for the geometry." 
    1414        return GEOSGeometry(getattr(self, field.attname), 'hex') 
    1515 
    1616    def _get_GEOM_ogr(self, field, srid): 
    17         "Gets an OGR Python object for the geometry." 
     17        "Returns an OGR Python object for the geometry." 
    1818        return OGRGeometry(hex_to_wkt(getattr(self, field.attname)), 
    1919                           SpatialReference('EPSG:%d' % srid)) 
    2020 
     21    def _get_GEOM_srid(self, srid): 
     22        "Returns the spatial reference identifier (SRID) of the geometry." 
     23        return srid 
     24 
     25    def _get_GEOM_srs(self, srid): 
     26        "Returns ane OGR Spatial Reference object of the geometry." 
     27        return SpatialReference('EPSG:%d' % srid) 
     28 
    2129    def _get_GEOM_wkt(self, field): 
    22         "Gets the WKT of the geometry." 
     30        "Returns the WKT of the geometry." 
    2331        hex = getattr(self, field.attname) 
    2432        return hex_to_wkt(hex) 
    2533 
    2634    def _get_GEOM_centroid(self, field): 
    27         "Gets the centroid of the geometry, in WKT." 
     35        "Returns the centroid of the geometry, in WKT." 
    2836        hex = getattr(self, field.attname) 
    2937        return centroid(hex) 
    3038     
    3139    def _get_GEOM_area(self, field): 
    32         "Gets the area of the geometry, in projected units." 
     40        "Returns the area of the geometry, in projected units." 
    3341        hex = getattr(self, field.attname) 
    3442        return area(hex) 
  • django/branches/gis/django/core/management.py

    r4786 r5448  
    389389    for f in opts.fields: 
    390390        if hasattr(f, '_post_create_sql'): 
    391             output.append(f._post_create_sql(style, model._meta.db_table, f.column)) 
     391            output.append(f._post_create_sql(style, model._meta.db_table)) 
    392392 
    393393    return output