diff options
| author | Arnaldo Govenem <arnaldo.govene@outlook.com> | 2025-01-22 22:54:02 +0200 |
|---|---|---|
| committer | Sarah Boyce <42296566+sarahboyce@users.noreply.github.com> | 2025-02-04 07:40:02 +0000 |
| commit | 8ff1399f0656b4ff8181fe7872e426c331d1b1ee (patch) | |
| tree | 69cf7a4649522fdb9a4f07c97502ef594c9a3346 /django | |
| parent | 5f30fd2358fd60a514bdba31594bfc8122f30167 (diff) | |
Fixed #36058 -- Refactored SpatialRefSysMixin.srs to use cached_property.
Replaced manual caching complexity with cached_property for efficiency.
Enhanced error handling with distinct messages for WKT and PROJ.4.
Thanks to Sarah Boyce for the suggestions.
Diffstat (limited to 'django')
| -rw-r--r-- | django/contrib/gis/db/backends/base/models.py | 38 |
1 files changed, 15 insertions, 23 deletions
diff --git a/django/contrib/gis/db/backends/base/models.py b/django/contrib/gis/db/backends/base/models.py index 589c872da6..38309f0e5d 100644 --- a/django/contrib/gis/db/backends/base/models.py +++ b/django/contrib/gis/db/backends/base/models.py @@ -1,4 +1,5 @@ from django.contrib.gis import gdal +from django.utils.functional import cached_property class SpatialRefSysMixin: @@ -7,35 +8,26 @@ class SpatialRefSysMixin: SpatialRefSys objects to reduce redundant code. """ - @property + @cached_property def srs(self): """ Return a GDAL SpatialReference object. """ - # TODO: Is caching really necessary here? Is complexity worth it? - 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 = gdal.SpatialReference(self.wkt) - return self.srs - except Exception as e: - msg = e + try: + return gdal.SpatialReference(self.wkt) + except Exception as e: + wkt_error = e - try: - self._srs = gdal.SpatialReference(self.proj4text) - return self.srs - except Exception as e: - msg = e + try: + return gdal.SpatialReference(self.proj4text) + except Exception as e: + proj4_error = e - raise Exception( - "Could not get OSR SpatialReference from WKT: %s\nError:\n%s" - % (self.wkt, msg) - ) + raise Exception( + "Could not get OSR SpatialReference.\n" + f"Error for WKT '{self.wkt}': {wkt_error}\n" + f"Error for PROJ.4 '{self.proj4text}': {proj4_error}" + ) @property def ellipsoid(self): |
