B
    ¥0H[u   ã               @   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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://web.archive.org/web/20130407175746/https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.0.0.html
https://www.gaia-gis.it/gaia-sins/spatialite-sql-4.2.1.html
é    )ÚBaseSpatialOperations)ÚSpatiaLiteAdapter)ÚSpatialOperator)Ú
aggregates)ÚGEOSGeometryÚGEOSGeometryBase)Úwkb_rÚwkt_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__© úxC:\Users\HIRONO~1\AppData\Local\Temp\pip-install-6bm3nxem\django\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	�dœZejfZedd„ ƒZ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d-d.„ Zd/d0„ Zd1d2„ Zd3d4„ Zd5d6„ Zd7d8„ Z d9d:„ Z!d;d<„ Z"d=d>„ Z#d?S )@ÚSpatiaLiteOperationsÚ
spatialiteTZCollectZExtentZMakeLineZGUnionZGeomFromTextZEquals)ÚfuncZDisjointZTouchesZCrossesZWithinZOverlapsZContainsZ
IntersectsZRelateZ	MbrWithinZMbrContainsZMbrOverlapsZPtDistWithin)ÚequalsZdisjointZtouchesZcrossesZwithinÚoverlapsÚcontainsZ
intersectsZrelateZ	containedZ
bbcontainsZ
bboverlapsZsame_asÚexactÚdwithinc             C   s   | j dkrdS dS )N)é   é   r   zCAST (AsEWKB(%s) AS BLOB)z
AsText(%s))Úspatial_version)r   r   r   r   ÚselectB   s    zSpatiaLiteOperations.selectZ	ST_LengthZST_Line_Locate_PointZ
ST_NPointsZ
ST_ReverseZScaleCoordsZST_TranslateZST_Union)ZLengthZLineLocatePointZ	NumPointsZReverseZScaleZ	TranslateÚUnionc             C   s&   dddh}|   ¡ s"|ddddhO }|S )NZBoundingCircleZForceRHRZMemSizeZAzimuthZGeoHashZIsValidZ	MakeValid)Úlwgeom_version)r   Zunsupportedr   r   r   Úunsupported_functionsP   s    
z*SpatiaLiteOperations.unsupported_functionsc          
   C   sd   y|   ¡ dd… }W n: tk
rN } ztd| jjd f ƒ|‚W d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%   r   r   z<GeoDjango only supports SpatiaLite versions 4.0.0 and above.)Úspatialite_version_tupleÚ	Exceptionr   r   Zsettings_dict)r   ÚversionÚexcr   r   r   r'   W   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_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   r5   ÚvalueZlookup_typeZ
dist_paramr   r   r   Úget_distancey   s    
z!SpatiaLiteOperations.get_distancec             C   s8   | j  ¡ }z| d| ¡ | ¡ }W d| ¡  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 %sNr   )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())rA   )r   r   r   r   Úgeos_version�   s    z!SpatiaLiteOperations.geos_versionc             C   s
   |   d¡S )z<Return the version of the PROJ.4 library used by SpatiaLite.zproj4_version())rA   )r   r   r   r   Úproj4_version¡   s    z"SpatiaLiteOperations.proj4_versionc             C   s
   |   d¡S )z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version())rA   )r   r   r   r   r*   ¥   s    z#SpatiaLiteOperations.lwgeom_versionc             C   s
   |   d¡S )z2Return the SpatiaLite library version as a string.zspatialite_version())rA   )r   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).
        )rD   r   )r   r0   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.modelsrI   )r   rI   r   r   r   Úgeometry_columns¾   s    z%SpatiaLiteOperations.geometry_columnsc             C   s   ddl m} |S )Nr   )ÚSpatialiteSpatialRefSys)rJ   rL   )r   rL   r   r   r   Úspatial_ref_sysÂ   s    z$SpatiaLiteOperations.spatial_ref_sysc                sZ   |j j‰ | jdkr*tƒ j‰‡ ‡fdd„}n,tƒ j‰|j j‰ˆdkrFd ‰‡ ‡‡fdd„}|S )N)r%   r&   r   c                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éÿÿÿÿc                s(   | d k	r$t ˆ| ƒˆ ƒ}ˆr ˆ|_|S d S )N)r   Úsrid)r;   rN   r   Zgeom)rO   rP   rS   r   r   rQ   Ó   s
    )Zoutput_fieldrO   r'   r   rP   r	   rS   )r   rN   rQ   r   )rO   rP   rS   r   Úget_geometry_converterÆ   s    
z+SpatiaLiteOperations.get_geometry_converterN)$r   r   r   Únamer   r   ZAdapterZcollectZextentZmakelinerF   Z	from_textr   r   Zgis_operatorsr   ZExtent3DZdisallowed_aggregatesr   r(   Zfunction_namesr+   r'   r4   r6   r<   rA   rB   rC   r*   rD   r.   rH   rK   rM   rT   r   r   r   r   r      s^   	r   N)Ú__doc__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.db.modelsr   Z django.contrib.gis.geos.geometryr   r   Z%django.contrib.gis.geos.prototypes.ior   r	   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   