mirror of
https://github.com/django/django.git
synced 2025-06-02 18:19:11 +00:00
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.
This commit is contained in:
parent
5f30fd2358
commit
8ff1399f06
@ -1,4 +1,5 @@
|
|||||||
from django.contrib.gis import gdal
|
from django.contrib.gis import gdal
|
||||||
|
from django.utils.functional import cached_property
|
||||||
|
|
||||||
|
|
||||||
class SpatialRefSysMixin:
|
class SpatialRefSysMixin:
|
||||||
@ -7,35 +8,26 @@ class SpatialRefSysMixin:
|
|||||||
SpatialRefSys objects to reduce redundant code.
|
SpatialRefSys objects to reduce redundant code.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
@property
|
@cached_property
|
||||||
def srs(self):
|
def srs(self):
|
||||||
"""
|
"""
|
||||||
Return a GDAL SpatialReference object.
|
Return a GDAL SpatialReference object.
|
||||||
"""
|
"""
|
||||||
# TODO: Is caching really necessary here? Is complexity worth it?
|
try:
|
||||||
if hasattr(self, "_srs"):
|
return gdal.SpatialReference(self.wkt)
|
||||||
# Returning a clone of the cached SpatialReference object.
|
except Exception as e:
|
||||||
return self._srs.clone()
|
wkt_error = e
|
||||||
else:
|
|
||||||
# Attempting to cache a SpatialReference object.
|
|
||||||
|
|
||||||
# Trying to get from WKT first.
|
try:
|
||||||
try:
|
return gdal.SpatialReference(self.proj4text)
|
||||||
self._srs = gdal.SpatialReference(self.wkt)
|
except Exception as e:
|
||||||
return self.srs
|
proj4_error = e
|
||||||
except Exception as e:
|
|
||||||
msg = e
|
|
||||||
|
|
||||||
try:
|
raise Exception(
|
||||||
self._srs = gdal.SpatialReference(self.proj4text)
|
"Could not get OSR SpatialReference.\n"
|
||||||
return self.srs
|
f"Error for WKT '{self.wkt}': {wkt_error}\n"
|
||||||
except Exception as e:
|
f"Error for PROJ.4 '{self.proj4text}': {proj4_error}"
|
||||||
msg = e
|
)
|
||||||
|
|
||||||
raise Exception(
|
|
||||||
"Could not get OSR SpatialReference from WKT: %s\nError:\n%s"
|
|
||||||
% (self.wkt, msg)
|
|
||||||
)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def ellipsoid(self):
|
def ellipsoid(self):
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import re
|
import re
|
||||||
|
|
||||||
|
from django.contrib.gis.db.backends.base.models import SpatialRefSysMixin
|
||||||
from django.db import connection
|
from django.db import connection
|
||||||
from django.test import TestCase, skipUnlessDBFeature
|
from django.test import TestCase, skipUnlessDBFeature
|
||||||
from django.utils.functional import cached_property
|
from django.utils.functional import cached_property
|
||||||
@ -147,3 +148,17 @@ class SpatialRefSysTest(TestCase):
|
|||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
self.SpatialRefSys.get_spheroid(srs.wkt).startswith("SPHEROID[")
|
self.SpatialRefSys.get_spheroid(srs.wkt).startswith("SPHEROID[")
|
||||||
)
|
)
|
||||||
|
|
||||||
|
def test_srs_with_invalid_wkt_and_proj4(self):
|
||||||
|
class MockSpatialRefSys(SpatialRefSysMixin):
|
||||||
|
def __init__(self, wkt=None, proj4text=None):
|
||||||
|
self.wkt = wkt
|
||||||
|
self.proj4text = proj4text
|
||||||
|
|
||||||
|
with self.assertRaisesMessage(
|
||||||
|
Exception,
|
||||||
|
"Could not get OSR SpatialReference.\n"
|
||||||
|
"Error for WKT 'INVALID_WKT': Corrupt data.\n"
|
||||||
|
"Error for PROJ.4 '+proj=invalid': Corrupt data.",
|
||||||
|
):
|
||||||
|
MockSpatialRefSys(wkt="INVALID_WKT", proj4text="+proj=invalid").srs
|
||||||
|
Loading…
x
Reference in New Issue
Block a user