U
    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_paramsZsqlparams	__class__ X/tmp/pip-unpacked-wheel-_jot26k8/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coversZ	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&   ddddh}|   s"|dddhO }|S )NZBoundingCircleZGeometryDistanceZIsEmptyZ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_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 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      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   