Django

Code

Changeset 6427

Show
Ignore:
Timestamp:
09/27/07 11:42:20 (1 year ago)
Author:
jbronn
Message:

gis: SpatialRefSys? model now uses HAS_GDAL flag, no longer uses _cache_osr(), and improved docstrings.

Files:

Legend:

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

    r6238 r6427  
    164164 
    165165def load_postgis_sql(db_name, verbosity=1): 
    166     """" 
     166    """ 
    167167    This routine loads up the PostGIS SQL files lwpostgis.sql and  
    168168     spatial_ref_sys.sql. 
  • django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py

    r5881 r6427  
    11""" 
    2   The PostGIS spatial database backend module. 
     2 The PostGIS spatial database backend module. 
    33""" 
    44from django.contrib.gis.db.backend.postgis.query import \ 
  • django/branches/gis/django/contrib/gis/models.py

    r6018 r6427  
     1""" 
     2 Models for the PostGIS/OGC database tables. 
     3""" 
    14import re 
    25from django.db import models 
    36 
    4 # Checking for the presence of GDAL 
    5 try: 
     7# Checking for the presence of GDAL (needed for the SpatialReference object) 
     8from django.contrib.gis.gdal import HAS_GDAL 
     9if HAS_GDAL: 
    610    from django.contrib.gis.gdal import SpatialReference 
    7     HAS_OSR = True 
    8 except ImportError: 
    9     HAS_OSR = False 
    10  
    11 """ 
    12   Models for the PostGIS/OGC database tables. 
    13 """ 
    1411 
    1512# For pulling out the spheroid from the spatial reference string. This 
     
    3431 
    3532    def __str__(self): 
    36         return "%s.%s - %dD %s field (SRID: %d)" % (self.f_table_name, self.f_geometry_column, self.coord_dimension, self.type, self.srid) 
     33        return "%s.%s - %dD %s field (SRID: %d)" % \ 
     34               (self.f_table_name, self.f_geometry_column, 
     35                self.coord_dimension, self.type, self.srid) 
    3736 
    3837# This is the global 'spatial_ref_sys' table from PostGIS. 
     
    4847        db_table = 'spatial_ref_sys' 
    4948 
    50     def _cache_osr(self): 
    51         "Caches a GDAL OSR SpatialReference object for this SpatialRefSys model." 
    52         if HAS_OSR: 
    53             if not hasattr(self, '_srs'): 
     49    @property 
     50    def srs(self): 
     51        """ 
     52        Returns a GDAL SpatialReference object, if GDAL is installed. 
     53        """ 
     54        if HAS_GDAL: 
     55            if hasattr(self, '_srs'): 
     56                # Returning a clone of the cached SpatialReference object. 
     57                return self._srs.clone() 
     58            else: 
     59                # Attempting to cache a SpatialReference object. 
     60 
    5461                # Trying to get from WKT first 
    5562                try: 
    5663                    self._srs = SpatialReference(self.srtext, 'wkt') 
    57                     return 
    58                 except Exception, msg
     64                    return self._srs.clone() 
     65                except Exception, msg1
    5966                    pass 
    6067 
     
    6269                try: 
    6370                    self._srs = SpatialReference(self.proj4text, 'proj4') 
    64                     return 
    65                 except Exception, msg
     71                    return self._srs.clone() 
     72                except Exception, msg2
    6673                    pass 
    6774 
    68                 raise Exception, 'Could not get a OSR Spatial Reference: %s' % msg 
     75                raise Exception, 'Could not get an OSR Spatial Reference:\n\tWKT error: %s\n\tPROJ.4 error: %s' % (msg1, msg2) 
    6976        else: 
    7077            raise Exception, 'GDAL is not installed!' 
    71  
    72     @property 
    73     def srs(self): 
    74         "Returns the SpatialReference equivalent of this model." 
    75         self._cache_osr() 
    76         return self._srs.clone() 
    7778                                                                                 
    7879    @property 
    7980    def ellipsoid(self): 
    80         """Returns a tuple of the ellipsoid parameters: 
    81         (semimajor axis, semiminor axis, and inverse flattening).""" 
    82         if HAS_OSR: 
    83             self._cache_osr() 
    84             return self._srs.ellipsoid 
     81        """ 
     82        Returns a tuple of the ellipsoid parameters: 
     83        (semimajor axis, semiminor axis, and inverse flattening). 
     84        """ 
     85        if HAS_GDAL: 
     86            return self.srs.ellipsoid 
    8587        else: 
    8688            m = spheroid_regex.match(self.srtext) 
     
    9193    def name(self): 
    9294        "Returns the projection name." 
    93         self._cache_osr() 
    94         return self._srs.name 
     95        return self.srs.name 
    9596 
    9697    @property 
    9798    def spheroid(self): 
    9899        "Returns the spheroid for this spatial reference." 
    99         self._cache_osr() 
    100         return self._srs['spheroid'] 
     100        return self.srs['spheroid'] 
    101101 
    102102    @property 
    103103    def datum(self): 
    104104        "Returns the datum for this spatial reference." 
    105         self._cache_osr() 
    106         return self._srs['datum'] 
     105        return self.srs['datum'] 
    107106 
    108107    @property 
    109108    def projected(self): 
    110109        "Is this Spatial Reference projected?" 
    111         self._cache_osr() 
    112         return self._srs.projected 
     110        return self.srs.projected 
    113111 
    114112    @property 
    115113    def local(self): 
    116114        "Is this Spatial Reference local?" 
    117         self._cache_osr() 
    118         return self._srs.local 
     115        return self.srs.local 
    119116 
    120117    @property 
    121118    def geographic(self): 
    122119        "Is this Spatial Reference geographic?" 
    123         self._cache_osr() 
    124         return self._srs.geographic 
     120        return self.srs.geographic 
    125121 
    126122    @property 
    127123    def linear_name(self): 
    128124        "Returns the linear units name." 
    129         self._cache_osr() 
    130         return self._srs.linear_name 
     125        return self.srs.linear_name 
    131126 
    132127    @property 
    133128    def linear_units(self): 
    134129        "Returns the linear units." 
    135         self._cache_osr() 
    136         return self._srs.linear_units 
     130        return self.srs.linear_units 
    137131 
    138132    @property 
    139133    def angular_units(self): 
    140134        "Returns the angular units." 
    141         self._cache_osr() 
    142         return self._srs.angular_units 
     135        return self.srs.angular_units 
    143136 
    144137    @property 
    145138    def angular_name(self): 
    146139        "Returns the name of the angular units." 
    147         self._cache_osr() 
    148         return self._srs.angular_name 
     140        return self.srs.angular_name 
    149141 
    150142    def __str__(self): 
    151         "Returns the string representation.  If GDAL is installed, it will be 'pretty' OGC WKT." 
    152         if HAS_OSR: 
    153             self._cache_osr() 
    154             if hasattr(self, '_srs'): return str(self._srs) 
    155         return "%d:%s " % (self.srid, self.auth_name) 
     143        """ 
     144        Returns the string representation.  If GDAL is installed, 
     145        it will be 'pretty' OGC WKT. 
     146        """ 
     147        if HAS_GDAL: return str(self.srs) 
     148        else: return "%d:%s " % (self.srid, self.auth_name)