a
    KcòfÊ   ã                   @   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ÚlookupZtemplate_paramsZ
sql_paramsÚsqlÚparams©Ú	__class__© úq/home/httpd/docs/test/DocsMgr/lib/python3.9/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                   @   sr  e Zd ZdZdZeZdZdZdZ	dZ
dZedd	�ed
d	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�edd	�dœZejfZdZddddddd d!d"d#d$d%œZed&d'„ ƒZed(d)„ ƒZd*d+„ Zd,d-„ Zd.d/„ Zd0d1„ Zd2d3„ Zd4d5„ Zd6d7„ Zd8d9„ Zd:d;„ Zd<d=„ Z d>d?„ Z!d@dA„ Z"dBdC„ Z#dDdE„ Z$dFdG„ Z%dHS )IÚSpatiaLiteOperationsÚ
spatialiteTZCollectZExtentZMakeLineZGUnionZGeomFromTextZEquals)ÚfuncZDisjointZTouchesZCrossesZWithinZOverlapsZContainsZ
IntersectsZRelateZ	CoveredByZCoversZ	MbrWithinZMbrContainsZMbrOverlapsZPtDistWithin)ÚequalsZdisjointZtouchesZcrossesZwithinÚoverlapsÚcontainsZ
intersectsZrelateZ	coveredbyZcoversÚ	containedZ
bbcontainsZ
bboverlapsZsame_asÚexactÚdwithinzCAST (AsEWKB(%s) AS BLOB)ZSt_AsBinaryZST_ForceLHRZST_GeomFromWKBZST_GeomFromTextZ	ST_LengthZST_Line_Locate_PointZ
ST_NPointsZ
ST_ReverseZScaleCoordsZST_TranslateZST_Union)ZAsWKBZForcePolygonCWZFromWKBZFromWKTZLengthZLineLocatePointZ	NumPointsZReverseZScaleZ	TranslateÚUnionc                 C   s    h d£}|   ¡ s|h d£O }|S )N>   ZIsEmptyZMemSizeZBoundingCircleZGeometryDistance>   Z	MakeValidZGeoHashZAzimuth)Úgeom_lib_version)r   Úunsupportedr   r   r   Úunsupported_functionsQ   s    z*SpatiaLiteOperations.unsupported_functionsc              
   C   sf   z|   ¡ dd… }W n< tyP } z$td| jjd f ƒ|‚W Y d}~n
d}~0 0 |dk rbtdƒ‚|S )z0Determine the version of the SpatiaLite library.é   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   Zsettings_dict)r   ÚversionÚexcr   r   r   Úspatial_versionX   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   Úboxr5   ZxminZyminZxmaxZymaxr   r   r   Úconvert_extentg   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_typer   s    z SpatiaLiteOperations.geo_db_typec                 C   sb   |sg S |d }t |tƒrX| | j¡r>|dkr6tdƒ‚|j}q\t|t | | j¡¡ƒ}n|}|gS )zu
        Return the distance parameters for the given geometry field,
        lookup value, and lookup type.
        r   r&   zNOnly numeric values of degree units are allowed on geographic DWithin queries.)	Ú
isinstancer	   Zgeodeticr   Ú
ValueErrorÚmÚgetattrZunit_attnameZ
units_name)r   r8   ÚvalueZlookup_typeZ
dist_paramr   r   r   Úget_distancey   s    
ÿÿz!SpatiaLiteOperations.get_distancec                 C   s@   | j  ¡ }z"| d| ¡ | ¡ }W | ¡  n
| ¡  0 |d S )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   Z_cursorÚexecuteZfetchoneÚclose)r   r    ÚcursorÚrowr   r   r   Ú_get_spatialite_func‘   s    

z)SpatiaLiteOperations._get_spatialite_funcc                 C   s
   |   d¡S )z:Return the version of GEOS used by SpatiaLite as a string.zgeos_version()©rD   ©r   r   r   r   Úgeos_versionŸ   s    z!SpatiaLiteOperations.geos_versionc                 C   s
   |   d¡S )z:Return the version of the PROJ library used by SpatiaLite.zproj4_version()rE   rF   r   r   r   Úproj_version£   s    z!SpatiaLiteOperations.proj_versionc                 C   s
   |   d¡S )z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version()rE   rF   r   r   r   Úlwgeom_version§   s    z#SpatiaLiteOperations.lwgeom_versionc                 C   s
   |   d¡S )z8Return the version of RTTOPO library used by SpatiaLite.zrttopo_version()rE   rF   r   r   r   Úrttopo_version«   s    z#SpatiaLiteOperations.rttopo_versionc                 C   s   | j dkr|  ¡ S |  ¡ S dS )zf
        Return the version of the version-dependant geom library used by
        SpatiaLite.
        )é   N)r3   rJ   rI   rF   r   r   r   r(   ¯   s    
z%SpatiaLiteOperations.geom_lib_versionc                 C   s
   |   d¡S )z2Return the SpatiaLite library version as a string.zspatialite_version()rE   rF   r   r   r   Úspatialite_version¹   s    z'SpatiaLiteOperations.spatialite_versionc                 C   s   |   ¡ }|ft|ƒ S )zl
        Return the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        )rL   r   )r   r1   r   r   r   r/   ½   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   Zagg_namer   r   r   Úspatial_aggregate_nameÅ   s    z+SpatiaLiteOperations.spatial_aggregate_namec                 C   s   ddl m} |S )Nr   )ÚSpatialiteGeometryColumns)Ú0django.contrib.gis.db.backends.spatialite.modelsrQ   )r   rQ   r   r   r   Úgeometry_columnsÎ   s    z%SpatiaLiteOperations.geometry_columnsc                 C   s   ddl m} |S )Nr   )ÚSpatialiteSpatialRefSys)rR   rT   )r   rT   r   r   r   Úspatial_ref_sysÕ   s    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)Zoutput_fieldrX   r   rY   )r   rV   rZ   r   rW   r   Úget_geometry_converterÜ   s    z+SpatiaLiteOperations.get_geometry_converterN)&r   r   r   Únamer   r   ZAdapterZcollectZextentZmakelinerN   Z	from_textr   r   Zgis_operatorsr   ZExtent3DZdisallowed_aggregatesÚselectZfunction_namesr   r*   r3   r7   r9   r?   rD   rG   rH   rI   rJ   r(   rL   r/   rP   rS   rU   r[   r   r   r   r   r      sv   éõ


	r   N)Ú__doc__Zdjango.contrib.gis.dbr   Z.django.contrib.gis.db.backends.base.operationsr   Z1django.contrib.gis.db.backends.spatialite.adapterr   Z$django.contrib.gis.db.backends.utilsr   Z django.contrib.gis.geos.geometryr   r   Z%django.contrib.gis.geos.prototypes.ior   Zdjango.contrib.gis.measurer	   Zdjango.core.exceptionsr
   Z%django.db.backends.sqlite3.operationsr   Zdjango.utils.functionalr   Zdjango.utils.versionr   r   r   r   r   r   r   Ú<module>   s   