Made get_srid_info() cache use a namedtuple.

This commit is contained in:
Sergey Fedoseev 2017-04-01 00:59:44 +05:00 committed by Tim Graham
parent 24ae244a82
commit e2bd2539b6
1 changed files with 12 additions and 7 deletions

View File

@ -1,4 +1,4 @@
from collections import defaultdict
from collections import defaultdict, namedtuple
from django.contrib.gis import forms, gdal
from django.contrib.gis.db.models.proxy import SpatialProxy
@ -15,6 +15,9 @@ from django.utils.translation import gettext_lazy as _
_srid_cache = defaultdict(dict)
SRIDCacheEntry = namedtuple('SRIDCacheEntry', ['units', 'units_name', 'spheroid'])
def get_srid_info(srid, connection):
"""
Return the units, unit name, and spheroid WKT associated with the
@ -38,9 +41,11 @@ def get_srid_info(srid, connection):
if srid not in _srid_cache[alias]:
srs = get_srs(srid)
units, units_name = srs.units
sphere_name = srs['spheroid']
spheroid = 'SPHEROID["%s",%s,%s]' % (sphere_name, srs.semi_major, srs.inverse_flattening)
_srid_cache[alias][srid] = (units, units_name, spheroid)
_srid_cache[alias][srid] = SRIDCacheEntry(
units=units,
units_name=units_name,
spheroid='SPHEROID["%s",%s,%s]' % (srs['spheroid'], srs.semi_major, srs.inverse_flattening),
)
return _srid_cache[alias][srid]
@ -116,13 +121,13 @@ class BaseSpatialField(Field):
return connection.ops.geo_db_type(self)
def spheroid(self, connection):
return get_srid_info(self.srid, connection)[2]
return get_srid_info(self.srid, connection).spheroid
def units(self, connection):
return get_srid_info(self.srid, connection)[0]
return get_srid_info(self.srid, connection).units
def units_name(self, connection):
return get_srid_info(self.srid, connection)[1]
return get_srid_info(self.srid, connection).units_name
def geodetic(self, connection):
"""