From e2bd2539b6d30a6f60dacdad9b8e15a2d4085484 Mon Sep 17 00:00:00 2001 From: Sergey Fedoseev Date: Sat, 1 Apr 2017 00:59:44 +0500 Subject: [PATCH] Made get_srid_info() cache use a namedtuple. --- django/contrib/gis/db/models/fields.py | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/django/contrib/gis/db/models/fields.py b/django/contrib/gis/db/models/fields.py index 737d757664..e27ff2dcf3 100644 --- a/django/contrib/gis/db/models/fields.py +++ b/django/contrib/gis/db/models/fields.py @@ -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): """