Changeset 6427
- Timestamp:
- 09/27/07 11:42:20 (1 year ago)
- Files:
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/gis/django/contrib/gis/db/backend/postgis/creation.py
r6238 r6427 164 164 165 165 def load_postgis_sql(db_name, verbosity=1): 166 """ "166 """ 167 167 This routine loads up the PostGIS SQL files lwpostgis.sql and 168 168 spatial_ref_sys.sql. django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py
r5881 r6427 1 1 """ 2 The PostGIS spatial database backend module.2 The PostGIS spatial database backend module. 3 3 """ 4 4 from 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 """ 1 4 import re 2 5 from django.db import models 3 6 4 # Checking for the presence of GDAL 5 try: 7 # Checking for the presence of GDAL (needed for the SpatialReference object) 8 from django.contrib.gis.gdal import HAS_GDAL 9 if HAS_GDAL: 6 10 from django.contrib.gis.gdal import SpatialReference 7 HAS_OSR = True8 except ImportError:9 HAS_OSR = False10 11 """12 Models for the PostGIS/OGC database tables.13 """14 11 15 12 # For pulling out the spheroid from the spatial reference string. This … … 34 31 35 32 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) 37 36 38 37 # This is the global 'spatial_ref_sys' table from PostGIS. … … 48 47 db_table = 'spatial_ref_sys' 49 48 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 54 61 # Trying to get from WKT first 55 62 try: 56 63 self._srs = SpatialReference(self.srtext, 'wkt') 57 return 58 except Exception, msg :64 return self._srs.clone() 65 except Exception, msg1: 59 66 pass 60 67 … … 62 69 try: 63 70 self._srs = SpatialReference(self.proj4text, 'proj4') 64 return 65 except Exception, msg :71 return self._srs.clone() 72 except Exception, msg2: 66 73 pass 67 74 68 raise Exception, 'Could not get a OSR Spatial Reference: %s' % msg75 raise Exception, 'Could not get an OSR Spatial Reference:\n\tWKT error: %s\n\tPROJ.4 error: %s' % (msg1, msg2) 69 76 else: 70 77 raise Exception, 'GDAL is not installed!' 71 72 @property73 def srs(self):74 "Returns the SpatialReference equivalent of this model."75 self._cache_osr()76 return self._srs.clone()77 78 78 79 @property 79 80 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 85 87 else: 86 88 m = spheroid_regex.match(self.srtext) … … 91 93 def name(self): 92 94 "Returns the projection name." 93 self._cache_osr() 94 return self._srs.name 95 return self.srs.name 95 96 96 97 @property 97 98 def spheroid(self): 98 99 "Returns the spheroid for this spatial reference." 99 self._cache_osr() 100 return self._srs['spheroid'] 100 return self.srs['spheroid'] 101 101 102 102 @property 103 103 def datum(self): 104 104 "Returns the datum for this spatial reference." 105 self._cache_osr() 106 return self._srs['datum'] 105 return self.srs['datum'] 107 106 108 107 @property 109 108 def projected(self): 110 109 "Is this Spatial Reference projected?" 111 self._cache_osr() 112 return self._srs.projected 110 return self.srs.projected 113 111 114 112 @property 115 113 def local(self): 116 114 "Is this Spatial Reference local?" 117 self._cache_osr() 118 return self._srs.local 115 return self.srs.local 119 116 120 117 @property 121 118 def geographic(self): 122 119 "Is this Spatial Reference geographic?" 123 self._cache_osr() 124 return self._srs.geographic 120 return self.srs.geographic 125 121 126 122 @property 127 123 def linear_name(self): 128 124 "Returns the linear units name." 129 self._cache_osr() 130 return self._srs.linear_name 125 return self.srs.linear_name 131 126 132 127 @property 133 128 def linear_units(self): 134 129 "Returns the linear units." 135 self._cache_osr() 136 return self._srs.linear_units 130 return self.srs.linear_units 137 131 138 132 @property 139 133 def angular_units(self): 140 134 "Returns the angular units." 141 self._cache_osr() 142 return self._srs.angular_units 135 return self.srs.angular_units 143 136 144 137 @property 145 138 def angular_name(self): 146 139 "Returns the name of the angular units." 147 self._cache_osr() 148 return self._srs.angular_name 140 return self.srs.angular_name 149 141 150 142 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)
