
    DvgH!                         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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                        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          q/var/www/pixelcanvas.ch/venv/lib/python3.11/site-packages/django/contrib/gis/db/backends/spatialite/operations.pyr   z"SpatialiteNullCheckOperator.as_sql   s1    ggnnZ*UUV#~v%%    )__name__
__module____qualname__r   __classcell__)r   s   @r   r   r      s8        & & & & & & & & &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%dHS )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}|                                  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   sT    @@@$$&& 	?>>>>K&((,--Kr   c                     	 |                                  dd         }n6# t          $ r)}t          d| j        j        d         d          |d}~ww xY w|dk     rt          d          |S )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\   s    	3355abb9GG 	 	 	&& ?0888;  		 Y&'WXXXs    
A$AAc                     |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   boxr{   xminyminxmaxymaxs          r   convert_extentz#SpatiaLiteOperations.convert_extentk   sW     ;4S!!'1Xbqb\
d1Xbqb\
ddD$''r   c                     dS )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	    
 tr   c                 2   |sg S |d         }t          |t                    rr|                    | j                  r|dk    rt	          d          |j        }n=t          |t          j        |                    | j                                      }n|}|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   #W

$80do1N1NOO 

 J|r   c                     | j                                         }	 |                    d|z             |                                }|                                 n# |                                 w xY w|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   _cursorexecutefetchoneclose)r   r,   cursorrows       r   _get_spatialite_funcz)SpatiaLiteOperations._get_spatialite_func   sl     ((**	NN;-...//##CLLNNNNFLLNNNN1vs   ,A A2c                 ,    |                      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                 ,    |                      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                 ,    |                      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                 ,    |                      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                 h    | j         dk    r|                                 S |                                 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   s6    
 4''&&(((&&(((r   c                 ,    |                      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                 P    |                                  }|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66r   c                 ~    |                                 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_names     r   spatial_aggregate_namez+SpatiaLiteOperations.spatial_aggregate_name   s;    
 "*!1!1W!<!<::(..BRBR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                 T    |j         j        t                      j        fd}|S )Nc                 >    | d nt           |                     S )N)r   )r   
expressionr   
geom_classreads      r   	converterz>SpatiaLiteOperations.get_geometry_converter.<locals>.converter   s$     =44.>ttE{{J.W.WWr   )output_fieldr   r	   r   )r   r   r   r   r   s      @@r   get_geometry_converterz+SpatiaLiteOperations.get_geometry_converter   sF    ,7
ww|	X 	X 	X 	X 	X 	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#      sb       DJGGFHHI--8<<< 	//Z@@@ 	..I>>>	
 	..I>>> 	--8<<< 	//Z@@@ 	//Z@@@ 	11|DDD 	--8<<< 	00kBBB 	--8<<< 	__+666  	oo=999!$ 	oo=999%( 	..H===)* 	,,(;;;+. 	??777/M4 $_.(F 5'#$1!# N   _   _	( 	( 	(    0  ; ; ;< < <= = == = =) ) )A A A7 7 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      sI   
 ) ( ( ( ( ( P P P P P P O O O O O O @ @ @ @ @ @ K K K K K K K K 7 7 7 7 7 7 / / / / / / 7 7 7 7 7 7 D D D D D D 3 3 3 3 3 3 2 2 2 2 2 2& & & & &/ & & &N N N N N02D N N N N Nr   