o
    <gH!                     @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
mZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ G dd deZG dd deeZdS )z\
SQL functions reference lists:
https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.3.0.html
    )models)BaseSpatialOperations)SpatiaLiteAdapter)SpatialOperator)GEOSGeometryGEOSGeometryBase)wkb_r)Distance)ImproperlyConfigured)DatabaseOperations)cached_property)get_version_tuplec                       s   e Zd Z fddZ  ZS )SpatialiteNullCheckOperatorc                    s"   t  ||||\}}d| |fS )Nz%s > 0)superas_sql)self
connectionlookuptemplate_params
sql_paramssqlparams	__class__ g/var/www/html/venv/lib/python3.10/site-packages/django/contrib/gis/db/backends/spatialite/operations.pyr      s   z"SpatialiteNullCheckOperator.as_sql)__name__
__module____qualname__r   __classcell__r   r   r   r   r      s    r   c                   @   s  e Zd ZdZdZeZdZdZdZ	dZ
dZi ded	d
dedd
dedd
dedd
dedd
dedd
dedd
dedd
dedd
dedd
dedd
ded d
d!ed"d
d#ed$d
d%ed	d
d&ed	d
d'ed(d
ZejfZd)Zd*d+d,d-d.d/d0d1d2d3d4d5d6Zed7d8 Zed9d: Zd;d< Zd=d> Zd?d@ ZdAdB ZdCdD ZdEdF ZdGdH ZdIdJ ZdKdL ZdMdN Z dOdP Z!dQdR Z"dSdT Z#dUdV Z$dWdX Z%dYS )ZSpatiaLiteOperations
spatialiteTCollectExtentMakeLineGUnionGeomFromTextequalsEquals)funcdisjointDisjointtouchesTouchescrossesCrosseswithinWithinoverlapsOverlapscontainsContains
intersects
IntersectsrelateRelate	coveredby	CoveredBycoversCovers	contained	MbrWithin
bbcontainsMbrContains
bboverlapsMbrOverlapssame_asexactdwithinPtDistWithinzCAST (AsEWKB(%s) AS BLOB)St_AsBinaryGEOSMinimumBoundingCircleST_ForceLHRST_GeomFromWKBST_GeomFromText	ST_LengthST_Line_Locate_Point
ST_NPoints
ST_ReverseScaleCoordsST_TranslateST_Union)AsWKBBoundingCircleForcePolygonCWFromWKBFromWKTLengthLineLocatePoint	NumPointsReverseScale	TranslateUnionc                 C   s4   h d}|   s|h dO }| jdk r|dhO }|S )N>   IsEmptyMemSizeGeometryDistance>   AzimuthGeoHash	MakeValid)      rU   )geom_lib_versionspatial_version)r   unsupportedr   r   r   unsupported_functionsS   s   

z*SpatiaLiteOperations.unsupported_functionsc              
   C   sZ   z
|   dd }W n ty" } ztd| jjd f |d}~ww |dk r+td|S )z0Determine the version of the SpatiaLite library.rg   Nz}Cannot determine the SpatiaLite version for the "%s" database. Was the SpatiaLite initialization SQL loaded on this database?NAME)      r   z.GeoDjango supports SpatiaLite 4.3.0 and above.)spatialite_version_tuple	Exceptionr
   r   settings_dict)r   versionexcr   r   r   ri   \   s   z$SpatiaLiteOperations.spatial_versionc                 C   sJ   |du rdS t |j}|d dd \}}|d dd \}}||||fS )zV
        Convert the polygon data received from SpatiaLite to min/max values.
        Nr      )r   shell)r   boxru   xminyminxmaxymaxr   r   r   convert_extentk   s   
z#SpatiaLiteOperations.convert_extentc                 C   s   dS )z
        Return None because geometry columns are added via the
        `AddGeometryColumn` stored procedure on SpatiaLite.
        Nr   )r   fr   r   r   geo_db_typev   s   z SpatiaLiteOperations.geo_db_typec                 C   sj   |sg S |d }t |tr0|| jr!|dkrtd|j}|gS t|t|| j}|gS |}|gS )zu
        Return the distance parameters for the given geometry field,
        lookup value, and lookup type.
        r   rF   zNOnly numeric values of degree units are allowed on geographic DWithin queries.)	
isinstancer	   geodeticr   
ValueErrormgetattrunit_attname
units_name)r   r|   valuelookup_type
dist_paramr   r   r   get_distance}   s"   
z!SpatiaLiteOperations.get_distancec                 C   s>   | j  }z|d|  | }W |  |d S |  w )z
        Helper routine for calling SpatiaLite functions and returning
        their result.
        Any error occurring in this method should be handled by the caller.
        z	SELECT %sr   )r   _cursorexecutefetchoneclose)r   r)   cursorrowr   r   r   _get_spatialite_func   s   


z)SpatiaLiteOperations._get_spatialite_funcc                 C   
   |  dS )z:Return the version of GEOS used by SpatiaLite as a string.zgeos_version()r   r   r   r   r   geos_version      
z!SpatiaLiteOperations.geos_versionc                 C   r   )z:Return the version of the PROJ library used by SpatiaLite.zproj4_version()r   r   r   r   r   proj_version   r   z!SpatiaLiteOperations.proj_versionc                 C   r   )z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version()r   r   r   r   r   lwgeom_version   r   z#SpatiaLiteOperations.lwgeom_versionc                 C   r   )z8Return the version of RTTOPO library used by SpatiaLite.zrttopo_version()r   r   r   r   r   rttopo_version   r   z#SpatiaLiteOperations.rttopo_versionc                 C   s   | j dkr	|  S |  S )zf
        Return the version of the version-dependant geom library used by
        SpatiaLite.
        )rf   )ri   r   r   r   r   r   r   rh      s   
z%SpatiaLiteOperations.geom_lib_versionc                 C   r   )z2Return the SpatiaLite library version as a string.zspatialite_version()r   r   r   r   r   spatialite_version   r   z'SpatiaLiteOperations.spatialite_versionc                 C   s   |   }|ft| S )zl
        Return the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        )r   r   )r   rr   r   r   r   ro      s   z-SpatiaLiteOperations.spatialite_version_tuplec                 C   s"   |  dkrdn|  }t| |S )zr
        Return the spatial aggregate SQL template and function for the
        given Aggregate instance.
        unionunionagg)lowerr   )r   agg_namer   r   r   spatial_aggregate_name   s   
z+SpatiaLiteOperations.spatial_aggregate_namec                 C      ddl m} |S )Nr   )SpatialiteGeometryColumns)0django.contrib.gis.db.backends.spatialite.modelsr   )r   r   r   r   r   geometry_columns      z%SpatiaLiteOperations.geometry_columnsc                 C   r   )Nr   )SpatialiteSpatialRefSys)r   r   )r   r   r   r   r   spatial_ref_sys   r   z$SpatiaLiteOperations.spatial_ref_sysc                    s"   |j j t j fdd}|S )Nc                    s   | d u rd S t |  S )N)r   )r   
expressionr   
geom_classreadr   r   	converter   s   z>SpatiaLiteOperations.get_geometry_converter.<locals>.converter)output_fieldr   r   r   )r   r   r   r   r   r   get_geometry_converter   s   z+SpatiaLiteOperations.get_geometry_converterN)&r   r   r   namer!   r   Adaptercollectextentmakeliner   	from_textr   r   gis_operatorsr   Extent3Ddisallowed_aggregatesselectfunction_namesr   rk   ri   r{   r}   r   r   r   r   r   r   rh   r   ro   r   r   r   r   r   r   r   r   r       s    







	












	r    N)__doc__django.contrib.gis.dbr   .django.contrib.gis.db.backends.base.operationsr   1django.contrib.gis.db.backends.spatialite.adapterr   $django.contrib.gis.db.backends.utilsr    django.contrib.gis.geos.geometryr   r   %django.contrib.gis.geos.prototypes.ior   django.contrib.gis.measurer	   django.core.exceptionsr
   %django.db.backends.sqlite3.operationsr   django.utils.functionalr   django.utils.versionr   r   r    r   r   r   r   <module>   s    