Author: jbronn
Date: 2007-09-27 11:42:20 -0500 (Thu, 27 Sep 2007)
New Revision: 6427
Modified:
django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py
django/branches/gis/django/contrib/gis/db/backend/postgis/creation.py
django/branches/gis/django/contrib/gis/models.py
Log:
gis: SpatialRefSys model now uses HAS_GDAL flag, no longer uses _cache_osr(),
and improved docstrings.
Modified: django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py
2007-09-26 04:36:33 UTC (rev 6426)
+++ django/branches/gis/django/contrib/gis/db/backend/postgis/__init__.py
2007-09-27 16:42:20 UTC (rev 6427)
@@ -1,5 +1,5 @@
"""
- The PostGIS spatial database backend module.
+ The PostGIS spatial database backend module.
"""
from django.contrib.gis.db.backend.postgis.query import \
get_geo_where_clause, geo_quotename, \
Modified: django/branches/gis/django/contrib/gis/db/backend/postgis/creation.py
===================================================================
--- django/branches/gis/django/contrib/gis/db/backend/postgis/creation.py
2007-09-26 04:36:33 UTC (rev 6426)
+++ django/branches/gis/django/contrib/gis/db/backend/postgis/creation.py
2007-09-27 16:42:20 UTC (rev 6427)
@@ -163,7 +163,7 @@
return settings.DATABASE_NAME
def load_postgis_sql(db_name, verbosity=1):
- """"
+ """
This routine loads up the PostGIS SQL files lwpostgis.sql and
spatial_ref_sys.sql.
"""
Modified: django/branches/gis/django/contrib/gis/models.py
===================================================================
--- django/branches/gis/django/contrib/gis/models.py 2007-09-26 04:36:33 UTC
(rev 6426)
+++ django/branches/gis/django/contrib/gis/models.py 2007-09-27 16:42:20 UTC
(rev 6427)
@@ -1,17 +1,14 @@
+"""
+ Models for the PostGIS/OGC database tables.
+"""
import re
from django.db import models
-# Checking for the presence of GDAL
-try:
+# Checking for the presence of GDAL (needed for the SpatialReference object)
+from django.contrib.gis.gdal import HAS_GDAL
+if HAS_GDAL:
from django.contrib.gis.gdal import SpatialReference
- HAS_OSR = True
-except ImportError:
- HAS_OSR = False
-"""
- Models for the PostGIS/OGC database tables.
-"""
-
# For pulling out the spheroid from the spatial reference string. This
# regular expression is used only if the user does not have GDAL installed.
# TODO: Flattening not used in all ellipsoids, could also be a minor axis, or
'b'
@@ -33,7 +30,9 @@
db_table = 'geometry_columns'
def __str__(self):
- return "%s.%s - %dD %s field (SRID: %d)" % (self.f_table_name,
self.f_geometry_column, self.coord_dimension, self.type, self.srid)
+ return "%s.%s - %dD %s field (SRID: %d)" % \
+ (self.f_table_name, self.f_geometry_column,
+ self.coord_dimension, self.type, self.srid)
# This is the global 'spatial_ref_sys' table from PostGIS.
# See PostGIS Documentation at Ch. 4.2.1
@@ -47,41 +46,44 @@
class Meta:
db_table = 'spatial_ref_sys'
- def _cache_osr(self):
- "Caches a GDAL OSR SpatialReference object for this SpatialRefSys
model."
- if HAS_OSR:
- if not hasattr(self, '_srs'):
+ @property
+ def srs(self):
+ """
+ Returns a GDAL SpatialReference object, if GDAL is installed.
+ """
+ if HAS_GDAL:
+ if hasattr(self, '_srs'):
+ # Returning a clone of the cached SpatialReference object.
+ return self._srs.clone()
+ else:
+ # Attempting to cache a SpatialReference object.
+
# Trying to get from WKT first
try:
self._srs = SpatialReference(self.srtext, 'wkt')
- return
- except Exception, msg:
+ return self._srs.clone()
+ except Exception, msg1:
pass
# Trying the proj4 text next
try:
self._srs = SpatialReference(self.proj4text, 'proj4')
- return
- except Exception, msg:
+ return self._srs.clone()
+ except Exception, msg2:
pass
- raise Exception, 'Could not get a OSR Spatial Reference: %s' %
msg
+ raise Exception, 'Could not get an OSR Spatial
Reference:\n\tWKT error: %s\n\tPROJ.4 error: %s' % (msg1, msg2)
else:
raise Exception, 'GDAL is not installed!'
-
- @property
- def srs(self):
- "Returns the SpatialReference equivalent of this model."
- self._cache_osr()
- return self._srs.clone()
@property
def ellipsoid(self):
- """Returns a tuple of the ellipsoid parameters:
- (semimajor axis, semiminor axis, and inverse flattening)."""
- if HAS_OSR:
- self._cache_osr()
- return self._srs.ellipsoid
+ """
+ Returns a tuple of the ellipsoid parameters:
+ (semimajor axis, semiminor axis, and inverse flattening).
+ """
+ if HAS_GDAL:
+ return self.srs.ellipsoid
else:
m = spheroid_regex.match(self.srtext)
if m: return (float(m.group('major')),
float(m.group('flattening')))
@@ -90,66 +92,57 @@
@property
def name(self):
"Returns the projection name."
- self._cache_osr()
- return self._srs.name
+ return self.srs.name
@property
def spheroid(self):
"Returns the spheroid for this spatial reference."
- self._cache_osr()
- return self._srs['spheroid']
+ return self.srs['spheroid']
@property
def datum(self):
"Returns the datum for this spatial reference."
- self._cache_osr()
- return self._srs['datum']
+ return self.srs['datum']
@property
def projected(self):
"Is this Spatial Reference projected?"
- self._cache_osr()
- return self._srs.projected
+ return self.srs.projected
@property
def local(self):
"Is this Spatial Reference local?"
- self._cache_osr()
- return self._srs.local
+ return self.srs.local
@property
def geographic(self):
"Is this Spatial Reference geographic?"
- self._cache_osr()
- return self._srs.geographic
+ return self.srs.geographic
@property
def linear_name(self):
"Returns the linear units name."
- self._cache_osr()
- return self._srs.linear_name
+ return self.srs.linear_name
@property
def linear_units(self):
"Returns the linear units."
- self._cache_osr()
- return self._srs.linear_units
+ return self.srs.linear_units
@property
def angular_units(self):
"Returns the angular units."
- self._cache_osr()
- return self._srs.angular_units
+ return self.srs.angular_units
@property
def angular_name(self):
"Returns the name of the angular units."
- self._cache_osr()
- return self._srs.angular_name
+ return self.srs.angular_name
def __str__(self):
- "Returns the string representation. If GDAL is installed, it will be
'pretty' OGC WKT."
- if HAS_OSR:
- self._cache_osr()
- if hasattr(self, '_srs'): return str(self._srs)
- return "%d:%s " % (self.srid, self.auth_name)
+ """
+ Returns the string representation. If GDAL is installed,
+ it will be 'pretty' OGC WKT.
+ """
+ if HAS_GDAL: return str(self.srs)
+ else: return "%d:%s " % (self.srid, self.auth_name)
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---