2008-08-06 02:13:06 +08:00
|
|
|
"""
|
|
|
|
Imports the SpatialRefSys and GeometryColumns models dependent on the
|
|
|
|
spatial database backend.
|
|
|
|
"""
|
|
|
|
import re
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
# Checking for the presence of GDAL (needed for the SpatialReference object)
|
2008-08-07 09:14:10 +08:00
|
|
|
from django.contrib.gis.gdal import HAS_GDAL, PYTHON23
|
2008-08-06 02:13:06 +08:00
|
|
|
if HAS_GDAL:
|
|
|
|
from django.contrib.gis.gdal import SpatialReference
|
|
|
|
|
|
|
|
class SpatialRefSysMixin(object):
|
|
|
|
"""
|
|
|
|
The SpatialRefSysMixin is a class used by the database-dependent
|
|
|
|
SpatialRefSys objects to reduce redundnant code.
|
|
|
|
"""
|
|
|
|
# For pulling out the spheroid from the spatial reference string. This
|
|
|
|
# regular expression is used only if the user does not have GDAL installed.
|
2009-03-31 01:15:49 +08:00
|
|
|
# TODO: Flattening not used in all ellipsoids, could also be a minor axis,
|
|
|
|
# or 'b' parameter.
|
2008-08-06 02:13:06 +08:00
|
|
|
spheroid_regex = re.compile(r'.+SPHEROID\[\"(?P<name>.+)\",(?P<major>\d+(\.\d+)?),(?P<flattening>\d{3}\.\d+),')
|
|
|
|
|
|
|
|
# For pulling out the units on platforms w/o GDAL installed.
|
|
|
|
# TODO: Figure out how to pull out angular units of projected coordinate system and
|
2009-03-31 01:15:49 +08:00
|
|
|
# fix for LOCAL_CS types. GDAL should be highly recommended for performing
|
2008-08-06 02:13:06 +08:00
|
|
|
# distance queries.
|
|
|
|
units_regex = re.compile(r'.+UNIT ?\["(?P<unit_name>[\w \'\(\)]+)", ?(?P<unit>[\d\.]+)(,AUTHORITY\["(?P<unit_auth_name>[\w \'\(\)]+)","(?P<unit_auth_val>\d+)"\])?\]([\w ]+)?(,AUTHORITY\["(?P<auth_name>[\w \'\(\)]+)","(?P<auth_val>\d+)"\])?\]$')
|
2009-03-31 01:15:49 +08:00
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def srs(self):
|
|
|
|
"""
|
|
|
|
Returns a GDAL SpatialReference object, if GDAL is installed.
|
|
|
|
"""
|
|
|
|
if HAS_GDAL:
|
2009-03-31 01:15:49 +08:00
|
|
|
# TODO: Is caching really necessary here? Is complexity worth it?
|
2008-08-06 02:13:06 +08:00
|
|
|
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.wkt)
|
|
|
|
return self.srs
|
|
|
|
except Exception, msg:
|
|
|
|
pass
|
2009-03-31 01:15:49 +08:00
|
|
|
|
|
|
|
try:
|
|
|
|
self._srs = SpatialReference(self.proj4text)
|
|
|
|
return self.srs
|
|
|
|
except Exception, msg:
|
|
|
|
pass
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
raise Exception('Could not get OSR SpatialReference from WKT: %s\nError:\n%s' % (self.wkt, msg))
|
|
|
|
else:
|
|
|
|
raise Exception('GDAL is not installed.')
|
2008-08-07 09:14:10 +08:00
|
|
|
srs = property(srs)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def ellipsoid(self):
|
|
|
|
"""
|
|
|
|
Returns a tuple of the ellipsoid parameters:
|
|
|
|
(semimajor axis, semiminor axis, and inverse flattening).
|
|
|
|
"""
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.ellipsoid
|
|
|
|
else:
|
|
|
|
m = self.spheroid_regex.match(self.wkt)
|
|
|
|
if m: return (float(m.group('major')), float(m.group('flattening')))
|
|
|
|
else: return None
|
2008-08-07 09:14:10 +08:00
|
|
|
ellipsoid = property(ellipsoid)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def name(self):
|
|
|
|
"Returns the projection name."
|
|
|
|
return self.srs.name
|
2008-08-07 09:14:10 +08:00
|
|
|
name = property(name)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def spheroid(self):
|
|
|
|
"Returns the spheroid name for this spatial reference."
|
|
|
|
return self.srs['spheroid']
|
2008-08-07 09:14:10 +08:00
|
|
|
spheroid = property(spheroid)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def datum(self):
|
|
|
|
"Returns the datum for this spatial reference."
|
|
|
|
return self.srs['datum']
|
2008-08-07 09:14:10 +08:00
|
|
|
datum = property(datum)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def projected(self):
|
|
|
|
"Is this Spatial Reference projected?"
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.projected
|
|
|
|
else:
|
|
|
|
return self.wkt.startswith('PROJCS')
|
2008-08-07 09:14:10 +08:00
|
|
|
projected = property(projected)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def local(self):
|
|
|
|
"Is this Spatial Reference local?"
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.local
|
|
|
|
else:
|
|
|
|
return self.wkt.startswith('LOCAL_CS')
|
2008-08-07 09:14:10 +08:00
|
|
|
local = property(local)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def geographic(self):
|
|
|
|
"Is this Spatial Reference geographic?"
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.geographic
|
|
|
|
else:
|
|
|
|
return self.wkt.startswith('GEOGCS')
|
2008-08-07 09:14:10 +08:00
|
|
|
geographic = property(geographic)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def linear_name(self):
|
|
|
|
"Returns the linear units name."
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.linear_name
|
2009-03-31 01:15:49 +08:00
|
|
|
elif self.geographic:
|
2008-08-06 02:13:06 +08:00
|
|
|
return None
|
|
|
|
else:
|
|
|
|
m = self.units_regex.match(self.wkt)
|
|
|
|
return m.group('unit_name')
|
2008-08-07 09:14:10 +08:00
|
|
|
linear_name = property(linear_name)
|
|
|
|
|
2008-08-06 02:13:06 +08:00
|
|
|
def linear_units(self):
|
|
|
|
"Returns the linear units."
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.linear_units
|
|
|
|
elif self.geographic:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
m = self.units_regex.match(self.wkt)
|
|
|
|
return m.group('unit')
|
2008-08-07 09:14:10 +08:00
|
|
|
linear_units = property(linear_units)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def angular_name(self):
|
|
|
|
"Returns the name of the angular units."
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.angular_name
|
|
|
|
elif self.projected:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
m = self.units_regex.match(self.wkt)
|
|
|
|
return m.group('unit_name')
|
2008-08-07 09:14:10 +08:00
|
|
|
angular_name = property(angular_name)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def angular_units(self):
|
|
|
|
"Returns the angular units."
|
|
|
|
if HAS_GDAL:
|
|
|
|
return self.srs.angular_units
|
|
|
|
elif self.projected:
|
|
|
|
return None
|
|
|
|
else:
|
|
|
|
m = self.units_regex.match(self.wkt)
|
|
|
|
return m.group('unit')
|
2008-08-07 09:14:10 +08:00
|
|
|
angular_units = property(angular_units)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def units(self):
|
|
|
|
"Returns a tuple of the units and the name."
|
|
|
|
if self.projected or self.local:
|
|
|
|
return (self.linear_units, self.linear_name)
|
|
|
|
elif self.geographic:
|
|
|
|
return (self.angular_units, self.angular_name)
|
|
|
|
else:
|
|
|
|
return (None, None)
|
2008-08-07 09:14:10 +08:00
|
|
|
units = property(units)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def get_units(cls, wkt):
|
|
|
|
"""
|
|
|
|
Class method used by GeometryField on initialization to
|
|
|
|
retrive the units on the given WKT, without having to use
|
|
|
|
any of the database fields.
|
|
|
|
"""
|
|
|
|
if HAS_GDAL:
|
|
|
|
return SpatialReference(wkt).units
|
|
|
|
else:
|
|
|
|
m = cls.units_regex.match(wkt)
|
|
|
|
return m.group('unit'), m.group('unit_name')
|
2008-08-07 09:14:10 +08:00
|
|
|
get_units = classmethod(get_units)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def get_spheroid(cls, wkt, string=True):
|
|
|
|
"""
|
|
|
|
Class method used by GeometryField on initialization to
|
|
|
|
retrieve the `SPHEROID[..]` parameters from the given WKT.
|
|
|
|
"""
|
|
|
|
if HAS_GDAL:
|
|
|
|
srs = SpatialReference(wkt)
|
|
|
|
sphere_params = srs.ellipsoid
|
|
|
|
sphere_name = srs['spheroid']
|
|
|
|
else:
|
|
|
|
m = cls.spheroid_regex.match(wkt)
|
2009-03-31 01:15:49 +08:00
|
|
|
if m:
|
2008-08-06 02:13:06 +08:00
|
|
|
sphere_params = (float(m.group('major')), float(m.group('flattening')))
|
|
|
|
sphere_name = m.group('name')
|
2009-03-31 01:15:49 +08:00
|
|
|
else:
|
2008-08-06 02:13:06 +08:00
|
|
|
return None
|
2009-03-31 01:15:49 +08:00
|
|
|
|
|
|
|
if not string:
|
2008-08-06 02:13:06 +08:00
|
|
|
return sphere_name, sphere_params
|
|
|
|
else:
|
|
|
|
# `string` parameter used to place in format acceptable by PostGIS
|
|
|
|
if len(sphere_params) == 3:
|
|
|
|
radius, flattening = sphere_params[0], sphere_params[2]
|
|
|
|
else:
|
|
|
|
radius, flattening = sphere_params
|
2009-03-31 01:15:49 +08:00
|
|
|
return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)
|
2008-08-07 09:14:10 +08:00
|
|
|
get_spheroid = classmethod(get_spheroid)
|
2008-08-06 02:13:06 +08:00
|
|
|
|
|
|
|
def __unicode__(self):
|
|
|
|
"""
|
|
|
|
Returns the string representation. If GDAL is installed,
|
|
|
|
it will be 'pretty' OGC WKT.
|
|
|
|
"""
|
|
|
|
try:
|
|
|
|
return unicode(self.srs)
|
|
|
|
except:
|
|
|
|
return unicode(self.wkt)
|
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Django test suite on 2.3 platforms will choke on code inside this
|
|
|
|
# conditional.
|
|
|
|
if not PYTHON23:
|
2008-08-06 02:13:06 +08:00
|
|
|
try:
|
2009-03-31 01:15:49 +08:00
|
|
|
# try/except'ing the importation of SpatialBackend. Have to fail
|
|
|
|
# silently because this module may be inadvertently invoked by
|
|
|
|
# non-GeoDjango users (e.g., when the Django test suite executes
|
|
|
|
# the models.py of all contrib apps).
|
|
|
|
from django.contrib.gis.db.backend import SpatialBackend
|
|
|
|
if SpatialBackend.mysql: raise Exception
|
2008-08-06 02:13:06 +08:00
|
|
|
|
2009-03-31 01:15:49 +08:00
|
|
|
# Exposing the SpatialRefSys and GeometryColumns models.
|
|
|
|
class SpatialRefSys(SpatialBackend.SpatialRefSys, SpatialRefSysMixin):
|
|
|
|
pass
|
|
|
|
GeometryColumns = SpatialBackend.GeometryColumns
|
|
|
|
except:
|
|
|
|
pass
|