local cache of the spatialrefsys table which holds srid data for each spatial database alias this cache exists so that the database isn t queried for srid info each time a distance query is constructed return the units unit name and spheroid wkt associated with the given srid from the spatialrefsys or equivalent spatial database table for the given database connection these results are cached the spatialrefsys model for the spatial backend the base gis field it s used as a base class for geometryfield and rasterfield defines properties that are common to all gis fields such as the characteristics of the spatial reference system of the field the initialization function for base spatial fields takes the following as keyword arguments srid the spatial reference system identifier an ogc standard defaults to 4326 wgs84 spatialindex indicates whether to create a spatial index defaults to true set this instead of dbindex for geographic fields since index creation is different for geometry columns setting the index flag with the value of the spatialindex keyword setting the srid and getting the units unit information must be easily available in the field instance for distance queries setting the verbosename keyword argument with the positional first parameter so this works like normal fields always include srid for less fragility include spatial index if it s not the default value return true if this field s srid corresponds with a coordinate system that uses nonprojected units e g latitudelongitude return the placeholder for the spatial column for the given value return the default srid for the given geometry or raster taking into account the srid set for the field for example if the input geometry or raster doesn t have an srid then the srid of the field will be returned return a gdalraster if conversion is successful otherwise return none when the input is not a geometry or raster attempt to construct one from the given string input check if input is a candidate for conversion to raster or geometry try to convert the input to raster assigning the srid value the base geometry field maps to the opengis specification geometry type the opengis geometry name the initialization function for geometry fields in addition to the parameters from basespatialfield it takes the following as keyword arguments dim the number of dimensions for this geometry defaults to 2 extent customize the extent in a 4tuple of wgs 84 coordinates for the geometry field entry in the usersdogeommetadata table defaults to 180 0 90 0 180 0 90 0 tolerance define the tolerance in meters to use for the geometry field entry in the usersdogeommetadata table defaults to 0 05 setting the dimension of the geometry field is this a geography rather than a geometry column oraclespecific private attributes for creating the entry in usersdogeommetadata include kwargs if they re not the default values setup for lazyinstantiated geometry object return the selection format string depending on the requirements of the spatial backend for example oracle and mysql require custom selection formats in order to retrieve geometries in ogc wkb the opengis geometry type fields raster field for geodjango evaluates into gdalraster objects make sure raster fields are used only on backends with raster support setup for lazyinstantiated raster object for large querysets the instantiation of all gdalrasters can potentially be expensive this delays the instantiation of the objects to the moment of evaluation of the raster attribute local cache of the spatial_ref_sys table which holds srid data for each spatial database alias this cache exists so that the database isn t queried for srid info each time a distance query is constructed return the units unit name and spheroid wkt associated with the given srid from the spatial_ref_sys or equivalent spatial database table for the given database connection these results are cached the spatialrefsys model for the spatial backend the base gis field it s used as a base class for geometryfield and rasterfield defines properties that are common to all gis fields such as the characteristics of the spatial reference system of the field the initialization function for base spatial fields takes the following as keyword arguments srid the spatial reference system identifier an ogc standard defaults to 4326 wgs84 spatial_index indicates whether to create a spatial index defaults to true set this instead of db_index for geographic fields since index creation is different for geometry columns setting the index flag with the value of the spatial_index keyword setting the srid and getting the units unit information must be easily available in the field instance for distance queries setting the verbose_name keyword argument with the positional first parameter so this works like normal fields always include srid for less fragility include spatial index if it s not the default value return true if this field s srid corresponds with a coordinate system that uses non projected units e g latitude longitude return the placeholder for the spatial column for the given value return the default srid for the given geometry or raster taking into account the srid set for the field for example if the input geometry or raster doesn t have an srid then the srid of the field will be returned srid of given geometry return a gdalraster if conversion is successful otherwise return none when the input is not a geometry or raster attempt to construct one from the given string input check if input is a candidate for conversion to raster or geometry try to convert the input to raster assigning the srid value the base geometry field maps to the opengis specification geometry type the opengis geometry name the initialization function for geometry fields in addition to the parameters from basespatialfield it takes the following as keyword arguments dim the number of dimensions for this geometry defaults to 2 extent customize the extent in a 4 tuple of wgs 84 coordinates for the geometry field entry in the user_sdo_geom_metadata table defaults to 180 0 90 0 180 0 90 0 tolerance define the tolerance in meters to use for the geometry field entry in the user_sdo_geom_metadata table defaults to 0 05 setting the dimension of the geometry field is this a geography rather than a geometry column oracle specific private attributes for creating the entry in user_sdo_geom_metadata include kwargs if they re not the default values setup for lazy instantiated geometry object return the selection format string depending on the requirements of the spatial backend for example oracle and mysql require custom selection formats in order to retrieve geometries in ogc wkb the opengis geometry type fields raster field for geodjango evaluates into gdalraster objects make sure raster fields are used only on backends with raster support setup for lazy instantiated raster object for large querysets the instantiation of all gdalrasters can potentially be expensive this delays the instantiation of the objects to the moment of evaluation of the raster attribute
from collections import defaultdict, namedtuple

from django.contrib.gis import forms, gdal
from django.contrib.gis.db.models.proxy import SpatialProxy
from django.contrib.gis.gdal.error import GDALException
from django.contrib.gis.geos import (
    GeometryCollection,
    GEOSException,
    GEOSGeometry,
    LineString,
    MultiLineString,
    MultiPoint,
    MultiPolygon,
    Point,
    Polygon,
)
from django.core.exceptions import ImproperlyConfigured
from django.db.models import Field
from django.utils.translation import gettext_lazy as _




_srid_cache = defaultdict(dict)


SRIDCacheEntry = namedtuple(
    "SRIDCacheEntry", ["units", "units_name", "spheroid", "geodetic"]
)


def get_srid_info(srid, connection):
    
    from django.contrib.gis.gdal import SpatialReference

    global _srid_cache

    try:
        
        SpatialRefSys = connection.ops.spatial_ref_sys()
    except NotImplementedError:
        SpatialRefSys = None

    alias, get_srs = (
        (
            connection.alias,
            lambda srid: SpatialRefSys.objects.using(connection.alias)
            .get(srid=srid)
            .srs,
        )
        if SpatialRefSys
        else (None, SpatialReference)
    )
    if srid not in _srid_cache[alias]:
        srs = get_srs(srid)
        units, units_name = srs.units
        _srid_cache[alias][srid] = SRIDCacheEntry(
            units=units,
            units_name=units_name,
            spheroid='SPHEROID["%s",%s,%s]'
            % (srs["spheroid"], srs.semi_major, srs.inverse_flattening),
            geodetic=srs.geographic,
        )

    return _srid_cache[alias][srid]


class BaseSpatialField(Field):
    

    description = _("The base GIS field.")
    empty_strings_allowed = False

    def __init__(self, verbose_name=None, srid=4326, spatial_index=True, **kwargs):
        

        
        self.spatial_index = spatial_index

        
        
        self.srid = srid

        
        
        kwargs["verbose_name"] = verbose_name

        super().__init__(**kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        
        
        kwargs["srid"] = self.srid
        if self.spatial_index is not True:
            kwargs["spatial_index"] = self.spatial_index
        return name, path, args, kwargs

    def db_type(self, connection):
        return connection.ops.geo_db_type(self)

    def spheroid(self, connection):
        return get_srid_info(self.srid, connection).spheroid

    def units(self, connection):
        return get_srid_info(self.srid, connection).units

    def units_name(self, connection):
        return get_srid_info(self.srid, connection).units_name

    def geodetic(self, connection):
        
        return get_srid_info(self.srid, connection).geodetic

    def get_placeholder(self, value, compiler, connection):
        
        return connection.ops.get_geom_placeholder(self, value, compiler)

    def get_srid(self, obj):
        
        srid = obj.srid  
        if srid is None or self.srid == -1 or (srid == -1 and self.srid != -1):
            return self.srid
        else:
            return srid

    def get_db_prep_value(self, value, connection, *args, **kwargs):
        if value is None:
            return None
        return connection.ops.Adapter(
            super().get_db_prep_value(value, connection, *args, **kwargs),
            **(
                {"geography": True}
                if self.geography and connection.features.supports_geography
                else {}
            ),
        )

    def get_raster_prep_value(self, value, is_candidate):
        
        if isinstance(value, gdal.GDALRaster):
            return value
        elif is_candidate:
            try:
                return gdal.GDALRaster(value)
            except GDALException:
                pass
        elif isinstance(value, dict):
            try:
                return gdal.GDALRaster(value)
            except GDALException:
                raise ValueError(
                    "Couldn't create spatial object from lookup value '%s'." % value
                )

    def get_prep_value(self, value):
        obj = super().get_prep_value(value)
        if obj is None:
            return None
        
        
        if isinstance(obj, GEOSGeometry):
            pass
        else:
            
            is_candidate = isinstance(obj, (bytes, str)) or hasattr(
                obj, "__geo_interface__"
            )
            
            raster = self.get_raster_prep_value(obj, is_candidate)

            if raster:
                obj = raster
            elif is_candidate:
                try:
                    obj = GEOSGeometry(obj)
                except (GEOSException, GDALException):
                    raise ValueError(
                        "Couldn't create spatial object from lookup value '%s'." % obj
                    )
            else:
                raise ValueError(
                    "Cannot use object with type %s for a spatial lookup parameter."
                    % type(obj).__name__
                )

        
        obj.srid = self.get_srid(obj)
        return obj


class GeometryField(BaseSpatialField):
    

    description = _(
        "The base Geometry field — maps to the OpenGIS Specification Geometry type."
    )
    form_class = forms.GeometryField
    
    geom_type = "GEOMETRY"
    geom_class = None

    def __init__(
        self,
        verbose_name=None,
        dim=2,
        geography=False,
        *,
        extent=(-180.0, -90.0, 180.0, 90.0),
        tolerance=0.05,
        **kwargs,
    ):
        
        
        self.dim = dim

        
        self.geography = geography

        
        
        self._extent = extent
        self._tolerance = tolerance

        super().__init__(verbose_name=verbose_name, **kwargs)

    def deconstruct(self):
        name, path, args, kwargs = super().deconstruct()
        
        if self.dim != 2:
            kwargs["dim"] = self.dim
        if self.geography is not False:
            kwargs["geography"] = self.geography
        if self._extent != (-180.0, -90.0, 180.0, 90.0):
            kwargs["extent"] = self._extent
        if self._tolerance != 0.05:
            kwargs["tolerance"] = self._tolerance
        return name, path, args, kwargs

    def contribute_to_class(self, cls, name, **kwargs):
        super().contribute_to_class(cls, name, **kwargs)

        
        setattr(
            cls,
            self.attname,
            SpatialProxy(self.geom_class or GEOSGeometry, self, load_func=GEOSGeometry),
        )

    def formfield(self, **kwargs):
        defaults = {
            "form_class": self.form_class,
            "geom_type": self.geom_type,
            "srid": self.srid,
            **kwargs,
        }
        if self.dim > 2 and not getattr(
            defaults["form_class"].widget, "supports_3d", False
        ):
            defaults.setdefault("widget", forms.Textarea)
        return super().formfield(**defaults)

    def select_format(self, compiler, sql, params):
        
        if not compiler.query.subquery:
            return compiler.connection.ops.select % sql, params
        return sql, params



class PointField(GeometryField):
    geom_type = "POINT"
    geom_class = Point
    form_class = forms.PointField
    description = _("Point")


class LineStringField(GeometryField):
    geom_type = "LINESTRING"
    geom_class = LineString
    form_class = forms.LineStringField
    description = _("Line string")


class PolygonField(GeometryField):
    geom_type = "POLYGON"
    geom_class = Polygon
    form_class = forms.PolygonField
    description = _("Polygon")


class MultiPointField(GeometryField):
    geom_type = "MULTIPOINT"
    geom_class = MultiPoint
    form_class = forms.MultiPointField
    description = _("Multi-point")


class MultiLineStringField(GeometryField):
    geom_type = "MULTILINESTRING"
    geom_class = MultiLineString
    form_class = forms.MultiLineStringField
    description = _("Multi-line string")


class MultiPolygonField(GeometryField):
    geom_type = "MULTIPOLYGON"
    geom_class = MultiPolygon
    form_class = forms.MultiPolygonField
    description = _("Multi polygon")


class GeometryCollectionField(GeometryField):
    geom_type = "GEOMETRYCOLLECTION"
    geom_class = GeometryCollection
    form_class = forms.GeometryCollectionField
    description = _("Geometry collection")


class ExtentField(Field):
    "Used as a return value from an extent aggregate"

    description = _("Extent Aggregate Field")

    def get_internal_type(self):
        return "ExtentField"

    def select_format(self, compiler, sql, params):
        select = compiler.connection.ops.select_extent
        return select % sql if select else sql, params


class RasterField(BaseSpatialField):
    

    description = _("Raster Field")
    geom_type = "RASTER"
    geography = False

    def _check_connection(self, connection):
        
        if (
            not connection.features.gis_enabled
            or not connection.features.supports_raster
        ):
            raise ImproperlyConfigured(
                "Raster fields require backends with raster support."
            )

    def db_type(self, connection):
        self._check_connection(connection)
        return super().db_type(connection)

    def from_db_value(self, value, expression, connection):
        return connection.ops.parse_raster(value)

    def contribute_to_class(self, cls, name, **kwargs):
        super().contribute_to_class(cls, name, **kwargs)
        
        
        
        
        setattr(cls, self.attname, SpatialProxy(gdal.GDALRaster, self))

    def get_transform(self, name):
        from django.contrib.gis.db.models.lookups import RasterBandTransform

        try:
            band_index = int(name)
            return type(
                "SpecificRasterBandTransform",
                (RasterBandTransform,),
                {"band_index": band_index},
            )
        except ValueError:
            pass
        return super().get_transform(name)