1
0
mirror of https://github.com/django/django.git synced 2025-07-04 17:59:13 +00:00

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

git-svn-id: http://code.djangoproject.com/svn/django/branches/gis@6427 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Justin Bronn 2007-09-27 16:42:20 +00:00
parent e835868621
commit cfb807a1fb
3 changed files with 50 additions and 57 deletions

View File

@ -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, \

View File

@ -163,7 +163,7 @@ def get_spatial_db(test=False):
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.
"""

View File

@ -1,16 +1,13 @@
"""
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.
@ -33,7 +30,9 @@ class GeometryColumns(models.Model):
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 SpatialRefSys(models.Model):
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 @@ class SpatialRefSys(models.Model):
@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)