o
    :Zc,                     @   s"  d dl 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 G dd	 d	eZG d
d de
ZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZejG dd deZeed ejG dd  d eZejG d!d" d"eZejG d#d$ d$eZejG d%d& d&eZejG d'd( d(eZejG d)d* d*eZejG d+d, d,eZ ejG d-d. d.eZ!ejG d/d0 d0eZ"ejG d1d2 d2eZ#ejG d3d4 d4eZ$ejG d5d6 d6eZ%ejG d7d8 d8eZ&ejG d9d: d:eZ'ejG d;d< d<eZ(G d=d> d>eZ)ejG d?d@ d@e)Z*G dAdB dBe)Z+ejG dCdD dDe+Z,ejG dEdF dFe+Z-ejG dGdH dHe+Z.ejG dIdJ dJe+Z/dS )K    N)BaseSpatialField)Distance)NotSupportedError)
Expression)Lookup	Transform)Queryc                   @      e Zd Zdd ZdS )RasterBandTransformc                 C   s   | | jS N)compilelhs)selfcompiler
connection r   Z/var/www/chikooza/env/lib/python3.10/site-packages/django/contrib/gis/db/models/lookups.pyas_sql   s   zRasterBandTransform.as_sqlN__name__
__module____qualname__r   r   r   r   r   r
          r
   c                       sf   e Zd ZdZdZdZdZdZ fddZdd Z	dddZ
d	d
 Z fddZdd Zdd Z  ZS )	GISLookupNFc                    s>   t |ttfr	|n|g^}| _t || i | _|   d S r   )
isinstancelisttuple
rhs_paramssuper__init__template_paramsprocess_rhs_params)r   r   rhs	__class__r   r   r      s   zGISLookup.__init__c                 C   sn   | j r't| j | jdkrdndkr|   d S t| j dkr%td| j d S t| jtr5| jdd d S d S )Nrelate      zTuple too long for lookup %s.T)only_lhs)r   lenlookup_nameprocess_band_indices
ValueErrorr   r   r
   r   r   r   r   r!      s   zGISLookup.process_rhs_paramsc                 C   sP   |rd| _ | jjd | _dS t| jtr| jjd | _nd| _| j^| _ | _dS )z
        Extract the lhs band index from the band transform class and the rhs
        band index from the input tuple.
        r'   N)band_rhsr   
band_indexband_lhsr   r
   r   )r   r(   r   r   r   r+   '   s   zGISLookup.process_band_indicesc                 C   s   d|j |gfS )N%s)opsAdapter)r   valuer   r   r   r   get_db_prep_lookup:   s   zGISLookup.get_db_prep_lookupc                    sj   t | jtrt ||S t | jtr| j|j| _t ||\}}|j	| j
j| j|}|| |fS r   )r   r"   r   r   process_rhsr   resolve_expressionqueryr2   get_geom_placeholderr   output_field)r   r   r   r"   r   placeholderr#   r   r   r6   >   s   zGISLookup.process_rhsc                 C   s   |j j| j S r   )r2   gis_operatorsr*   )r   r   r"   r   r   r   
get_rhs_opH   s   zGISLookup.get_rhs_opc           	      C   sX   |  ||\}}| ||\}}|| ||dd| j}| ||}||| ||S )Nr1   )r   r"   r4   )process_lhsr6   extendr    r=   r   )	r   r   r   lhs_sql
sql_paramsrhs_sqlr   r    rhs_opr   r   r   r   N   s   
zGISLookup.as_sql)F)r   r   r   sql_templatetransform_funcdistancer.   r0   r   r!   r+   r5   r6   r=   r   __classcell__r   r   r#   r   r      s    


r   c                   @      e Zd ZdZdZdS )OverlapsLeftLookupzy
    The overlaps_left operator returns true if A's bounding box overlaps or is to the
    left of B's bounding box.
    overlaps_leftNr   r   r   __doc__r*   r   r   r   r   rI   \       rI   c                   @   rH   )OverlapsRightLookupz}
    The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
    right of B's bounding box.
    overlaps_rightNrK   r   r   r   r   rN   e   rM   rN   c                   @   rH   )OverlapsBelowLookupzs
    The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
    B's bounding box.
    overlaps_belowNrK   r   r   r   r   rP   n   rM   rP   c                   @   rH   )OverlapsAboveLookupzs
    The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
    B's bounding box.
    overlaps_aboveNrK   r   r   r   r   rR   w   rM   rR   c                   @   rH   )
LeftLookupzo
    The 'left' operator returns true if A's bounding box is strictly to the left
    of B's bounding box.
    leftNrK   r   r   r   r   rT      rM   rT   c                   @   rH   )RightLookupzq
    The 'right' operator returns true if A's bounding box is strictly to the right
    of B's bounding box.
    rightNrK   r   r   r   r   rV      rM   rV   c                   @   rH   )StrictlyBelowLookupzp
    The 'strictly_below' operator returns true if A's bounding box is strictly below B's
    bounding box.
    strictly_belowNrK   r   r   r   r   rX      rM   rX   c                   @   rH   )StrictlyAboveLookupzp
    The 'strictly_above' operator returns true if A's bounding box is strictly above B's
    bounding box.
    strictly_aboveNrK   r   r   r   r   rZ      rM   rZ   c                   @   rH   )SameAsLookupz
    The "~=" operator is the "same as" operator. It tests actual geometric
    equality of two features. So if A and B are the same feature,
    vertex-by-vertex, the operator returns true.
    same_asNrK   r   r   r   r   r\      s    r\   exactc                   @   rH   )BBContainsLookupzq
    The 'bbcontains' operator returns true if A's bounding box completely contains
    by B's bounding box.
    
bbcontainsNrK   r   r   r   r   r_      rM   r_   c                   @   rH   )BBOverlapsLookupz_
    The 'bboverlaps' operator returns true if A's bounding box overlaps B's bounding box.
    
bboverlapsNrK   r   r   r   r   ra      s    ra   c                   @   rH   )ContainedLookupzt
    The 'contained' operator returns true if A's bounding box is completely contained
    by B's bounding box.
    	containedNrK   r   r   r   r   rc      rM   rc   c                   @      e Zd ZdZdS )ContainsLookupcontainsNr   r   r   r*   r   r   r   r   rf          rf   c                   @   re   )ContainsProperlyLookupcontains_properlyNrh   r   r   r   r   rj      ri   rj   c                   @   re   )CoveredByLookup	coveredbyNrh   r   r   r   r   rl      ri   rl   c                   @   re   )CoversLookupcoversNrh   r   r   r   r   rn      ri   rn   c                   @   re   )CrossesLookupcrossesNrh   r   r   r   r   rp      ri   rp   c                   @   re   )DisjointLookupdisjointNrh   r   r   r   r   rr      ri   rr   c                   @   re   )EqualsLookupequalsNrh   r   r   r   r   rt      ri   rt   c                   @   re   )IntersectsLookup
intersectsNrh   r   r   r   r   rv      ri   rv   c                   @   re   )OverlapsLookupoverlapsNrh   r   r   r   r   rx      ri   rx   c                       s.   e Zd ZdZdZedZ fddZ  Z	S )RelateLookupr%   z%(func)s(%(lhs)s, %(rhs)s, %%s)z^[012TF\*]{9}$c                    sp   | j d }|jj| j }t|dr|| nt|tr"| j	|s(t
d| t ||\}}|||g fS )Nr   check_relate_argumentz)Invalid intersection matrix pattern "%s".)r   r2   r<   r*   hasattrr{   r   strpattern_regexmatchr,   r   r6   )r   r   r   pattern
backend_opsqlparamsr#   r   r   r6     s   

zRelateLookup.process_rhs)
r   r   r   r*   rD   rer   r~   r6   rG   r   r   r#   r   rz      s
    
rz   c                   @   re   )TouchesLookuptouchesNrh   r   r   r   r   r     ri   r   c                   @   re   )WithinLookupwithinNrh   r   r   r   r   r     ri   r   c                   @   s$   e Zd ZdZdZdd Zdd ZdS )DistanceLookupBaseTz+%(func)s(%(lhs)s, %(rhs)s) %(op)s %(value)sc                 C   s~   dt | j  krdksn td| j t | jdkr'| jd dkr'tdt | jdkr;| jd dkr=|   d S d S d S )Nr'      z22, 3, or 4-element tuple required for '%s' lookup.r&   spheroidzHFor 4-element tuples the last argument must be the 'spheroid' directive.)r)   r   r,   r*   r+   r-   r   r   r   r!     s   z%DistanceLookupBase.process_rhs_paramsc                 C   sB   | j d }t|dr|||jS d|j| jj| j | j	fS )Nr   r7   r1   )
r   r|   r   r7   r8   r2   get_distancer   r:   r*   r   r   r   
dist_paramr   r   r   process_distance&  s   
z#DistanceLookupBase.process_distanceN)r   r   r   rF   rD   r!   r   r   r   r   r   r     s
    
r   c                       s0   e Zd ZdZdZ fddZ fddZ  ZS )DWithinLookupdwithinz%%(func)s(%(lhs)s, %(rhs)s, %(value)s)c                    s<   | j d }|jjst|drt|tstdt ||S )Nr   r7   zXThis backend does not support expressions for specifying distance in the dwithin lookup.)	r   featuressupports_dwithin_distance_exprr|   r   r   r   r   r   r   r#   r   r   r   4  s   
zDWithinLookup.process_distancec                    s8   |  ||\}}|| jd< t ||\}}||| fS )Nr4   )r   r    r   r6   )r   r   r   dist_sqldist_paramsrB   r   r#   r   r   r6   A  s   
zDWithinLookup.process_rhs)r   r   r   r*   rD   r   r6   rG   r   r   r#   r   r   /  s
    r   c                   @   r	   )DistanceLookupFromFunctionc           	      C   sv   t | jdkr| jd dkpd }|jj| j| j|d}|||j\}}| 	||\}}d|| j
|d || fS )Nr&   r   )r   z%(func)s %(op)s %(dist)s)funcopdist)r)   r   r2   distance_expr_for_lookupr   r"   r   r7   r8   r   r   )	r   r   r   r   distance_exprr   r   r   r   r   r   r   r   I  s    z!DistanceLookupFromFunction.as_sqlNr   r   r   r   r   r   H  r   r   c                   @   rH   )DistanceGTLookupdistance_gt>Nr   r   r   r*   r   r   r   r   r   r   T      r   c                   @   rH   )DistanceGTELookupdistance_gtez>=Nr   r   r   r   r   r   Z  r   r   c                   @   rH   )DistanceLTLookupdistance_lt<Nr   r   r   r   r   r   `  r   r   c                   @   rH   )DistanceLTELookupdistance_ltez<=Nr   r   r   r   r   r   f  r   r   )0r   #django.contrib.gis.db.models.fieldsr   django.contrib.gis.measurer   	django.dbr   django.db.models.expressionsr   django.db.models.lookupsr   r   django.db.models.sql.queryr   r
   r   register_lookuprI   rN   rP   rR   rT   rV   rX   rZ   r\   r_   ra   rc   rf   rj   rl   rn   rp   rr   rt   rv   rx   rz   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    L	