o
    ˜¨Êht   ã                   @   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__© úf/var/www/html/env/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/d0d1d2d3œ	Zed4d5„ ƒZed6d7„ ƒZd8d9„ Zd:d;„ Zd<d=„ Zd>d?„ Zd@dA„ ZdBdC„ ZdDdE„ ZdFdG„ ZdHdI„ ZdJdK„ Z dLdM„ Z!dNdO„ Z"dPdQ„ Z#dRdS„ Z$dTdU„ Z%dVS )WÚ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ÚST_ForceLHRÚ	ST_LengthÚST_Line_Locate_PointÚ
ST_NPointsÚ
ST_ReverseÚScaleCoordsÚST_TranslateÚST_Union)	ÚAsWKBÚForcePolygonCWÚLengthÚLineLocatePointÚ	NumPointsÚReverseÚScaleÚ	TranslateÚUnionc                 C   s    h d£}|   ¡ s|h d£O }|S )N>   ÚMemSizeÚBoundingCircleÚGeometryDistance>   ÚAzimuthÚGeoHashÚ	MakeValid)Úgeom_lib_version)r   Úunsupportedr   r   r   Úunsupported_functionsO   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.é   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   Úspatial_versionV   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   Úboxrn   ÚxminÚyminÚxmaxÚymaxr   r   r   Úconvert_extente   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_typep   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   ru   ÚvalueÚlookup_typeÚ
dist_paramr   r   r   Úget_distancew   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.
        )é   )rl   r�   r�   r‹   r   r   r   r`   ­   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   rj   r   r   r   rg   »   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   rb   rl   rt   rv   r�   rˆ   rŒ   rŽ   r�   r�   r`   r’   rg   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    