
    gH!                         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e      Z G d dee      Zy)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                        e Zd Z fdZ xZS )SpatialiteNullCheckOperatorc                 <    t         |   ||||      \  }}d|z  |fS )Nz%s > 0)superas_sql)self
connectionlookuptemplate_params
sql_paramssqlparams	__class__s          /var/www/python.vincentserveurtest.ovh/public_html/venv/lib/python3.12/site-packages/django/contrib/gis/db/backends/spatialite/operations.pyr   z"SpatialiteNullCheckOperator.as_sql   s*    gnZ*UV#~v%%    )__name__
__module____qualname__r   __classcell__)r   s   @r   r   r      s    & &r   r   c                   :   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 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(
      Zej                   fZd)Zd*d+d,d-d.d/d0d1d2d3d4d5d6Zed7        Zed8        Zd9 Zd: Zd; Zd< Zd= Zd> Zd? Zd@ ZdA ZdB Z dC Z!dD Z"dE Z#dF Z$dG Z%yH)I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GEOSMinimumBoundingCircleST_ForceLHRST_GeomFromWKBST_GeomFromText	ST_LengthST_Line_Locate_Point
ST_NPoints
ST_ReverseScaleCoordsST_TranslateST_Union)AsWKBBoundingCircleForcePolygonCWFromWKBFromWKTLengthLineLocatePoint	NumPointsReverseScale	TranslateUnionc                 f    h d}| j                         s|h dz  }| j                  dk  r|dhz  }|S )N>   IsEmptyMemSizeGeometryDistance>   AzimuthGeoHash	MakeValid)      rX   )geom_lib_versionspatial_version)r   unsupporteds     r   unsupported_functionsz*SpatiaLiteOperations.unsupported_functionsS   s@    @$$&>>K&(,--Kr   c                     	 | j                         dd }|dk  rt        d      |S # t        $ r,}t        d| j                  j                  d   d      |d}~ww xY w)z0Determine the version of the SpatiaLite library.rk   Nz1Cannot determine the SpatiaLite version for the "NAMEzJ" database. Was the SpatiaLite initialization SQL loaded on this database?)      r   z.GeoDjango supports SpatiaLite 4.3.0 and above.)spatialite_version_tuple	Exceptionr   r   settings_dict)r   versionexcs      r   rm   z$SpatiaLiteOperations.spatial_version\   st    	335ab9G Y&'WXX  	& ??008; 		s   ' 	A'AAc                 j    |y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   boxr{   xminyminxmaxymaxs          r   convert_extentz#SpatiaLiteOperations.convert_extentk   sQ     ;S!''1Xbq\
d1Xbq\
ddD$''r   c                      y)z
        Return None because geometry columns are added via the
        `AddGeometryColumn` stored procedure on SpatiaLite.
        N )r   fs     r   geo_db_typez SpatiaLiteOperations.geo_db_typev   s    
 r   c                 (   |sg S |d   }t        |t              ru|j                  | j                        r|dk(  rt	        d      |j
                  }|gS t        |t        j                  |j                  | j                                    }|gS |}|gS )zu
        Return the distance parameters for the given geometry field,
        lookup value, and lookup type.
        r   rI   zNOnly numeric values of degree units are allowed on geographic DWithin queries.)	
isinstancer
   geodeticr   
ValueErrormgetattrunit_attname
units_name)r   r   valuelookup_type
dist_params        r   get_distancez!SpatiaLiteOperations.get_distance}   s    
 IaeX&zz$//*)+$6  #WW
 | %800doo1NO

 | J|r   c                     | j                   j                         }	 |j                  d|z         |j                         }|j	                          |d   S # |j	                          w xY 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rows       r   _get_spatialite_funcz)SpatiaLiteOperations._get_spatialite_func   sW     ((*	NN;-.//#CLLN1v LLNs   $A A'c                 $    | j                  d      S )z:Return the version of GEOS used by SpatiaLite as a string.zgeos_version()r   r   s    r   geos_versionz!SpatiaLiteOperations.geos_version   s    (()9::r   c                 $    | j                  d      S )z:Return the version of the PROJ library used by SpatiaLite.zproj4_version()r   r   s    r   proj_versionz!SpatiaLiteOperations.proj_version   s    (():;;r   c                 $    | j                  d      S )z8Return the version of LWGEOM library used by SpatiaLite.zlwgeom_version()r   r   s    r   lwgeom_versionz#SpatiaLiteOperations.lwgeom_version       (();<<r   c                 $    | j                  d      S )z8Return the version of RTTOPO library used by SpatiaLite.zrttopo_version()r   r   s    r   rttopo_versionz#SpatiaLiteOperations.rttopo_version   r   r   c                 `    | j                   dk\  r| j                         S | j                         S )zf
        Return the version of the version-dependant geom library used by
        SpatiaLite.
        )rj   )rm   r   r   r   s    r   rl   z%SpatiaLiteOperations.geom_lib_version   s0    
 4'&&((&&((r   c                 $    | j                  d      S )z2Return the SpatiaLite library version as a string.zspatialite_version()r   r   s    r   spatialite_versionz'SpatiaLiteOperations.spatialite_version   s    (()?@@r   c                 @    | j                         }|ft        |      z   S )zl
        Return the SpatiaLite version as a tuple (version string, major,
        minor, subminor).
        )r   r   )r   rw   s     r   rt   z-SpatiaLiteOperations.spatialite_version_tuple   s%    
 ))+z-g666r   c                 d    |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)lowerr   )r   agg_names     r   spatial_aggregate_namez+SpatiaLiteOperations.spatial_aggregate_name   s-    
 "*!1W!<:(..BRtX&&r   c                     ddl m} |S )Nr   )SpatialiteGeometryColumns)0django.contrib.gis.db.backends.spatialite.modelsr   )r   r   s     r   geometry_columnsz%SpatiaLiteOperations.geometry_columns   s    	
 )(r   c                     ddl m} |S )Nr   )SpatialiteSpatialRefSys)r   r   )r   r   s     r   spatial_ref_sysz$SpatiaLiteOperations.spatial_ref_sys   s    	
 '&r   c                 j    |j                   j                  t               j                  fd}|S )Nc                 0    | d S t         |             S )N)r   )r   
expressionr   
geom_classreads      r   	converterz>SpatiaLiteOperations.get_geometry_converter.<locals>.converter   s     =4W.>tE{J.WWr   )output_fieldr   r	   r   )r   r   r   r   r   s      @@r   get_geometry_converterz+SpatiaLiteOperations.get_geometry_converter   s-    ,,77
w||	X r   N)&r   r   r    namer$   r   Adaptercollectextentmakeliner   	from_textr   r   gis_operatorsr   Extent3Ddisallowed_aggregatesselectfunction_namesr   ro   rm   r   r   r   r   r   r   r   r   rl   r   rt   r   r   r   r   r   r   r   r#   r#      s   DJGGFHHI-8< 	/Z@ 	.I>	
 	.I> 	-8< 	/Z@ 	/Z@ 	1|D 	-8< 	0kB 	-8< 	_+6  	o=9!$ 	o=9%( 	.H=)* 	,(;+. 	?7/M4 $__.(F 5'#$1!#N    	(0;<==)A7')'r   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   <module>r      sK   
 ) P O @ K 7 / 7 D 3 2&/ &N02D Nr   