the spatialrefsysmixin is a class used by the databasedependent spatialrefsys objects to reduce redundant code return a gdal spatialreference object todo is caching really necessary here is complexity worth it returning a clone of the cached spatialreference object attempting to cache a spatialreference object trying to get from wkt first return a tuple of the ellipsoid parameters semimajor axis semiminor axis and inverse flattening return a tuple of unitvalue unitname for the given wkt without using any of the database fields class method used by geometryfield on initialization to retrieve the spheroid parameters from the given wkt string parameter used to place in format acceptable by postgis return the string representation a pretty ogc wkt the spatialrefsysmixin is a class used by the database dependent spatialrefsys objects to reduce redundant code return a gdal spatialreference object todo is caching really necessary here is complexity worth it returning a clone of the cached spatialreference object attempting to cache a spatialreference object trying to get from wkt first return a tuple of the ellipsoid parameters semimajor axis semiminor axis and inverse flattening return a tuple of unit_value unit_name for the given wkt without using any of the database fields class method used by geometryfield on initialization to retrieve the spheroid parameters from the given wkt string parameter used to place in format acceptable by postgis return the string representation a pretty ogc wkt
from django.contrib.gis import gdal


class SpatialRefSysMixin:
    

    @property
    def srs(self):
        
        
        if hasattr(self, "_srs"):
            
            return self._srs.clone()
        else:
            

            
            try:
                self._srs = gdal.SpatialReference(self.wkt)
                return self.srs
            except Exception as e:
                msg = e

            try:
                self._srs = gdal.SpatialReference(self.proj4text)
                return self.srs
            except Exception as e:
                msg = e

            raise Exception(
                "Could not get OSR SpatialReference from WKT: %s\nError:\n%s"
                % (self.wkt, msg)
            )

    @property
    def ellipsoid(self):
        
        return self.srs.ellipsoid

    @property
    def name(self):
        "Return the projection name."
        return self.srs.name

    @property
    def spheroid(self):
        "Return the spheroid name for this spatial reference."
        return self.srs["spheroid"]

    @property
    def datum(self):
        "Return the datum for this spatial reference."
        return self.srs["datum"]

    @property
    def projected(self):
        "Is this Spatial Reference projected?"
        return self.srs.projected

    @property
    def local(self):
        "Is this Spatial Reference local?"
        return self.srs.local

    @property
    def geographic(self):
        "Is this Spatial Reference geographic?"
        return self.srs.geographic

    @property
    def linear_name(self):
        "Return the linear units name."
        return self.srs.linear_name

    @property
    def linear_units(self):
        "Return the linear units."
        return self.srs.linear_units

    @property
    def angular_name(self):
        "Return the name of the angular units."
        return self.srs.angular_name

    @property
    def angular_units(self):
        "Return the angular units."
        return self.srs.angular_units

    @property
    def units(self):
        "Return 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)

    @classmethod
    def get_units(cls, wkt):
        
        return gdal.SpatialReference(wkt).units

    @classmethod
    def get_spheroid(cls, wkt, string=True):
        
        srs = gdal.SpatialReference(wkt)
        sphere_params = srs.ellipsoid
        sphere_name = srs["spheroid"]

        if not string:
            return sphere_name, sphere_params
        else:
            
            if len(sphere_params) == 3:
                radius, flattening = sphere_params[0], sphere_params[2]
            else:
                radius, flattening = sphere_params
            return 'SPHEROID["%s",%s,%s]' % (sphere_name, radius, flattening)

    def __str__(self):
        
        return str(self.srs)