U
    5øij   ã                   @   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_paramsZsqlÚparams©Ú	__class__© úX/tmp/pip-unpacked-wheel-gcyiijx6/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                   @   sn  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#œ	Zed$d%„ ƒZed&d'„ ƒZ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%dFS )GÚ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coversZ	containedZ
bbcontainsZ
bboverlapsZsame_asÚexactÚdwithinzCAST (AsEWKB(%s) AS BLOB)ZSt_AsBinaryZST_ForceLHRZ	ST_LengthZST_Line_Locate_PointZ
ST_NPointsZ
ST_ReverseZScaleCoordsZST_TranslateZST_Union)	ZAsWKBZForcePolygonCWZLengthZLineLocatePointZ	NumPointsZReverseZScaleZ	TranslateÚUnionc                 C   s$   dddh}|   ¡ s |dddhO }|S )NZBoundingCircleZGeometryDistanceZMemSizeZAzimuthZGeoHashZ	MakeValid)Úgeom_lib_version)r   Úunsupportedr   r   r   Úunsupported_functionsQ   s    
z*SpatiaLiteOperations.unsupported_functionsc              
   C   sd   z|   ¡ dd… }W n: tk
rN } ztd| jjd f ƒ|‚W 5 d}~X Y nX |dk r`t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k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   Zboxr3   ZxminZyminZxmaxZymaxr   r   r   Úconvert_extenth   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_types   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   r5   ÚvalueZlookup_typeZ
dist_paramr   r   r   Úget_distancez   s    
ÿz!SpatiaLiteOperations.get_distancec                 C   s8   | j  ¡ }z| d| ¡ | ¡ }W 5 | ¡  X |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ÚcloseÚexecuteZfetchone)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()©rA   ©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()rB   rC   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()rB   rC   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()rB   rC   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)r1   rG   rF   rC   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()rB   rC   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).
        )rI   r   )r   r/   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.modelsrN   )r   rN   r   r   r   Úgeometry_columnsÍ   s    z%SpatiaLiteOperations.geometry_columnsc                 C   s   ddl m} |S )Nr   )ÚSpatialiteSpatialRefSys)rO   rQ   )r   rQ   r   r   r   Úspatial_ref_sysÓ   s    z$SpatiaLiteOperations.spatial_ref_sysc                    s"   |j j‰ tƒ j‰‡ ‡fdd„}|S )Nc                    s   | d krd S t ˆ| ƒˆ ƒS )N)r   )r;   Ú
expressionr   ©Ú
geom_classÚreadr   r   Ú	converterÝ   s    z>SpatiaLiteOperations.get_geometry_converter.<locals>.converter)Zoutput_fieldrU   r   rV   )r   rS   rW   r   rT   r   Úget_geometry_converterÙ   s    z+SpatiaLiteOperations.get_geometry_converterN)&r   r   r   Únamer   r   ZAdapterZcollectZextentZmakelinerK   Z	from_textr   r   Zgis_operatorsr   ZExtent3DZdisallowed_aggregatesÚselectZfunction_namesr   r(   r1   r4   r6   r<   rA   rD   rE   rF   rG   r&   rI   r-   rM   rP   rR   rX   r   r   r   r   r      sr   é÷


	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   