3
g                 @   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  j||||\}}d| |fS )Nz%s > 0)superas_sql)self
connectionlookupZtemplate_paramsZ
sql_paramsZsqlparams)	__class__ r/var/www/tester-filtro-web/env/lib/python3.6/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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#dBS )C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}| j  s"|ddddhO }|S )NZBoundingCircleZGeometryDistanceZMemSizeZAzimuthZGeoHashZIsValidZ	MakeValid)lwgeom_version)r   unsupportedr   r   r   unsupported_functionsQ   s    
z*SpatiaLiteOperations.unsupported_functionsc             C   sd   y| j  dd }W n: tk
rN } ztd| jjd f |W Y dd}~X 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.)r*   r+   r   )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boxr2   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| jr>|dkr6td|j}q\t|tj|j| 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   r4   valueZlookup_typeZ
dist_paramr   r   r   get_distancez   s    
z!SpatiaLiteOperations.get_distancec          
   C   s8   | j j }z|jd|  |j }W d|j  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
   | j dS )z:Return the version of GEOS used by SpatiaLite as a string.zgeos_version())r@   )r   r   r   r   geos_version   s    z!SpatiaLiteOperations.geos_versionc             C   s
   | j dS )z:Return the version of the PROJ library used by SpatiaLite.zproj4_version())r@   )r   r   r   r   proj_version   s    z!SpatiaLiteOperations.proj_versionc             C   s
   | j dS )z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version())r@   )r   r   r   r   r%      s    z#SpatiaLiteOperations.lwgeom_versionc             C   s
   | j dS )z2Return the SpatiaLite library version as a string.zspatialite_version())r@   )r   r   r   r   spatialite_version   s    z'SpatiaLiteOperations.spatialite_versionc             C   s   | j  }|ft| S )zl
        Return the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        )rC   r   )r   r.   r   r   r   r,      s    z-SpatiaLiteOperations.spatialite_version_tuplec             C   s"   |j  dkrdn|j  }t| |S )zr
        Return the spatial aggregate SQL template and function for the
        given Aggregate instance.
        unionunionagg)lowerr9   )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.modelsrH   )r   rH   r   r   r   geometry_columns   s    z%SpatiaLiteOperations.geometry_columnsc             C   s   ddl m} |S )Nr   )SpatialiteSpatialRefSys)rI   rK   )r   rK   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_fieldrN   r   rO   )r   rM   rP   r   )rN   rO   r   get_geometry_converter   s    z+SpatiaLiteOperations.get_geometry_converterN)$r   r   r   namer   r   ZAdapterZcollectZextentZmakelinerE   Z	from_textr   r   Zgis_operatorsr   ZExtent3DZdisallowed_aggregatesselectZfunction_namesr   r'   r0   r3   r5   r;   r@   rA   rB   r%   rC   r,   rG   rJ   rL   rQ   r   r   r   r   r      sf   	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   