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:
parent
e835868621
commit
cfb807a1fb
@ -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 \
|
from django.contrib.gis.db.backend.postgis.query import \
|
||||||
get_geo_where_clause, geo_quotename, \
|
get_geo_where_clause, geo_quotename, \
|
||||||
|
@ -163,7 +163,7 @@ def get_spatial_db(test=False):
|
|||||||
return settings.DATABASE_NAME
|
return settings.DATABASE_NAME
|
||||||
|
|
||||||
def load_postgis_sql(db_name, verbosity=1):
|
def load_postgis_sql(db_name, verbosity=1):
|
||||||
""""
|
"""
|
||||||
This routine loads up the PostGIS SQL files lwpostgis.sql and
|
This routine loads up the PostGIS SQL files lwpostgis.sql and
|
||||||
spatial_ref_sys.sql.
|
spatial_ref_sys.sql.
|
||||||
"""
|
"""
|
||||||
|
@ -1,16 +1,13 @@
|
|||||||
|
"""
|
||||||
|
Models for the PostGIS/OGC database tables.
|
||||||
|
"""
|
||||||
import re
|
import re
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
# Checking for the presence of GDAL
|
# Checking for the presence of GDAL (needed for the SpatialReference object)
|
||||||
try:
|
from django.contrib.gis.gdal import HAS_GDAL
|
||||||
|
if HAS_GDAL:
|
||||||
from django.contrib.gis.gdal import SpatialReference
|
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
|
# For pulling out the spheroid from the spatial reference string. This
|
||||||
# regular expression is used only if the user does not have GDAL installed.
|
# 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'
|
db_table = 'geometry_columns'
|
||||||
|
|
||||||
def __str__(self):
|
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.
|
# This is the global 'spatial_ref_sys' table from PostGIS.
|
||||||
# See PostGIS Documentation at Ch. 4.2.1
|
# See PostGIS Documentation at Ch. 4.2.1
|
||||||
@ -47,41 +46,44 @@ class SpatialRefSys(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
db_table = 'spatial_ref_sys'
|
db_table = 'spatial_ref_sys'
|
||||||
|
|
||||||
def _cache_osr(self):
|
@property
|
||||||
"Caches a GDAL OSR SpatialReference object for this SpatialRefSys model."
|
def srs(self):
|
||||||
if HAS_OSR:
|
"""
|
||||||
if not hasattr(self, '_srs'):
|
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
|
# Trying to get from WKT first
|
||||||
try:
|
try:
|
||||||
self._srs = SpatialReference(self.srtext, 'wkt')
|
self._srs = SpatialReference(self.srtext, 'wkt')
|
||||||
return
|
return self._srs.clone()
|
||||||
except Exception, msg:
|
except Exception, msg1:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
# Trying the proj4 text next
|
# Trying the proj4 text next
|
||||||
try:
|
try:
|
||||||
self._srs = SpatialReference(self.proj4text, 'proj4')
|
self._srs = SpatialReference(self.proj4text, 'proj4')
|
||||||
return
|
return self._srs.clone()
|
||||||
except Exception, msg:
|
except Exception, msg2:
|
||||||
pass
|
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:
|
else:
|
||||||
raise Exception, 'GDAL is not installed!'
|
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
|
@property
|
||||||
def ellipsoid(self):
|
def ellipsoid(self):
|
||||||
"""Returns a tuple of the ellipsoid parameters:
|
"""
|
||||||
(semimajor axis, semiminor axis, and inverse flattening)."""
|
Returns a tuple of the ellipsoid parameters:
|
||||||
if HAS_OSR:
|
(semimajor axis, semiminor axis, and inverse flattening).
|
||||||
self._cache_osr()
|
"""
|
||||||
return self._srs.ellipsoid
|
if HAS_GDAL:
|
||||||
|
return self.srs.ellipsoid
|
||||||
else:
|
else:
|
||||||
m = spheroid_regex.match(self.srtext)
|
m = spheroid_regex.match(self.srtext)
|
||||||
if m: return (float(m.group('major')), float(m.group('flattening')))
|
if m: return (float(m.group('major')), float(m.group('flattening')))
|
||||||
@ -90,66 +92,57 @@ class SpatialRefSys(models.Model):
|
|||||||
@property
|
@property
|
||||||
def name(self):
|
def name(self):
|
||||||
"Returns the projection name."
|
"Returns the projection name."
|
||||||
self._cache_osr()
|
return self.srs.name
|
||||||
return self._srs.name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def spheroid(self):
|
def spheroid(self):
|
||||||
"Returns the spheroid for this spatial reference."
|
"Returns the spheroid for this spatial reference."
|
||||||
self._cache_osr()
|
return self.srs['spheroid']
|
||||||
return self._srs['spheroid']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def datum(self):
|
def datum(self):
|
||||||
"Returns the datum for this spatial reference."
|
"Returns the datum for this spatial reference."
|
||||||
self._cache_osr()
|
return self.srs['datum']
|
||||||
return self._srs['datum']
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def projected(self):
|
def projected(self):
|
||||||
"Is this Spatial Reference projected?"
|
"Is this Spatial Reference projected?"
|
||||||
self._cache_osr()
|
return self.srs.projected
|
||||||
return self._srs.projected
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def local(self):
|
def local(self):
|
||||||
"Is this Spatial Reference local?"
|
"Is this Spatial Reference local?"
|
||||||
self._cache_osr()
|
return self.srs.local
|
||||||
return self._srs.local
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def geographic(self):
|
def geographic(self):
|
||||||
"Is this Spatial Reference geographic?"
|
"Is this Spatial Reference geographic?"
|
||||||
self._cache_osr()
|
return self.srs.geographic
|
||||||
return self._srs.geographic
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def linear_name(self):
|
def linear_name(self):
|
||||||
"Returns the linear units name."
|
"Returns the linear units name."
|
||||||
self._cache_osr()
|
return self.srs.linear_name
|
||||||
return self._srs.linear_name
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def linear_units(self):
|
def linear_units(self):
|
||||||
"Returns the linear units."
|
"Returns the linear units."
|
||||||
self._cache_osr()
|
return self.srs.linear_units
|
||||||
return self._srs.linear_units
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def angular_units(self):
|
def angular_units(self):
|
||||||
"Returns the angular units."
|
"Returns the angular units."
|
||||||
self._cache_osr()
|
return self.srs.angular_units
|
||||||
return self._srs.angular_units
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def angular_name(self):
|
def angular_name(self):
|
||||||
"Returns the name of the angular units."
|
"Returns the name of the angular units."
|
||||||
self._cache_osr()
|
return self.srs.angular_name
|
||||||
return self._srs.angular_name
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"Returns the string representation. If GDAL is installed, it will be 'pretty' OGC WKT."
|
"""
|
||||||
if HAS_OSR:
|
Returns the string representation. If GDAL is installed,
|
||||||
self._cache_osr()
|
it will be 'pretty' OGC WKT.
|
||||||
if hasattr(self, '_srs'): return str(self._srs)
|
"""
|
||||||
return "%d:%s " % (self.srid, self.auth_name)
|
if HAS_GDAL: return str(self.srs)
|
||||||
|
else: return "%d:%s " % (self.srid, self.auth_name)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user