o
    `Zh.                     @   s  d dl mZ d dlmZ d dlmZ d dlmZmZm	Z	 d dl
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.d/ d/eZ"ejG d0d1 d1eZ#ejG d2d3 d3eZ$ejG d4d5 d5eZ%ejG d6d7 d7eZ&ejG d8d9 d9eZ'ejG d:d; d;eZ(G d<d= d=eZ)ejG d>d? d?e)Z*G d@dA dAe)Z+ejG dBdC dCe+Z,ejG dDdE dEe+Z-ejG dFdG dGe+Z.ejG dHdI dIe+Z/dJS )K    )BaseSpatialField)Distance)NotSupportedError)
ExpressionLookup	Transform)Query)_lazy_re_compilec                   @      e Zd Zdd ZdS )RasterBandTransformc                 C   s   | | jS N)compilelhs)selfcompiler
connection r   [/var/www/html/lang_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   Z
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)opsZAdapter)r   valuer   r   r   r   get_db_prep_lookup8   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   Zget_geom_placeholderr   output_field)r   r   r   r#   r   placeholderr$   r   r   r5   <   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   s\   |  ||\}}| ||\}}g ||R }||dd| j}| ||}	|	|| ||S )Nr1   )r   r#   r3   )Zprocess_lhsr5   r!   r;   r   )
r   r   r   Zlhs_sqlZ
lhs_paramsrhs_sqlr   Z
sql_paramsr!   Zrhs_opr   r   r   r   N   s   zGISLookup.as_sql)F)r   r   r   sql_templateZtransform_funcdistancer/   r0   r    r"   r,   r4   r5   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.
    Zoverlaps_leftNr   r   r   __doc__r+   r   r   r   r   rA   b       rA   c                   @   r@   )OverlapsRightLookupz}
    The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
    right of B's bounding box.
    Zoverlaps_rightNrB   r   r   r   r   rE   l   rD   rE   c                   @   r@   )OverlapsBelowLookupzs
    The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
    B's bounding box.
    Zoverlaps_belowNrB   r   r   r   r   rF   v   rD   rF   c                   @   r@   )OverlapsAboveLookupzs
    The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
    B's bounding box.
    Zoverlaps_aboveNrB   r   r   r   r   rG      rD   rG   c                   @   r@   )
LeftLookupzo
    The 'left' operator returns true if A's bounding box is strictly to the left
    of B's bounding box.
    leftNrB   r   r   r   r   rH      rD   rH   c                   @   r@   )RightLookupzq
    The 'right' operator returns true if A's bounding box is strictly to the right
    of B's bounding box.
    rightNrB   r   r   r   r   rJ      rD   rJ   c                   @   r@   )StrictlyBelowLookupzp
    The 'strictly_below' operator returns true if A's bounding box is strictly below B's
    bounding box.
    Zstrictly_belowNrB   r   r   r   r   rL      rD   rL   c                   @   r@   )StrictlyAboveLookupzp
    The 'strictly_above' operator returns true if A's bounding box is strictly above B's
    bounding box.
    Zstrictly_aboveNrB   r   r   r   r   rM      rD   rM   c                   @   r@   )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.
    Zsame_asNrB   r   r   r   r   rN      s    rN   exactc                   @   r@   )BBContainsLookupzq
    The 'bbcontains' operator returns true if A's bounding box completely contains
    by B's bounding box.
    Z
bbcontainsNrB   r   r   r   r   rP      rD   rP   c                   @   r@   )BBOverlapsLookupzc
    The 'bboverlaps' operator returns true if A's bounding box overlaps B's
    bounding box.
    Z
bboverlapsNrB   r   r   r   r   rQ      rD   rQ   c                   @   r@   )ContainedLookupzt
    The 'contained' operator returns true if A's bounding box is completely contained
    by B's bounding box.
    	containedNrB   r   r   r   r   rR      rD   rR   c                   @      e Zd ZdZdS )ContainsLookupcontainsNr   r   r   r+   r   r   r   r   rU          rU   c                   @   rT   )ContainsProperlyLookupZcontains_properlyNrW   r   r   r   r   rY      rX   rY   c                   @   rT   )CoveredByLookupZ	coveredbyNrW   r   r   r   r   rZ      rX   rZ   c                   @   rT   )CoversLookupZcoversNrW   r   r   r   r   r[      rX   r[   c                   @   rT   )CrossesLookupZcrossesNrW   r   r   r   r   r\      rX   r\   c                   @   rT   )DisjointLookupZdisjointNrW   r   r   r   r   r]      rX   r]   c                   @   rT   )EqualsLookupequalsNrW   r   r   r   r   r^     rX   r^   c                   @   rT   )IntersectsLookupZ
intersectsNrW   r   r   r   r   r`     rX   r`   c                   @   rT   )OverlapsLookupoverlapsNrW   r   r   r   r   ra     rX   ra   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+   hasattrrd   r   strpattern_regexmatchr-   r   r5   )r   r   r   patternZ
backend_opsqlparamsr$   r   r   r5     s   

zRelateLookup.process_rhs)	r   r   r   r+   r=   r	   rg   r5   r?   r   r   r$   r   rc     s
    rc   c                   @   rT   )TouchesLookupZtouchesNrW   r   r   r   r   rl   "  rX   rl   c                   @   rT   )WithinLookupZwithinNrW   r   r   r   r   rm   '  rX   rm   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"   0  s   z%DistanceLookupBase.process_rhs_paramsc                 C   sB   | j d }t|dr|||jS d|j| jj| j | j	fS )Nr   r6   r1   )
r   re   r   r6   r7   r2   Zget_distancer   r8   r+   r   r   r   Z
dist_paramr   r   r   process_distance?  s   
z#DistanceLookupBase.process_distanceN)r   r   r   r>   r=   r"   rr   r   r   r   r   rn   ,  s
    rn   c                       s0   e Zd ZdZdZ fddZ fddZ  ZS )DWithinLookupZ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   r6   zXThis backend does not support expressions for specifying distance in the dwithin lookup.)	r   featuresZsupports_dwithin_distance_exprre   r   r   r   r   rr   rq   r$   r   r   rr   R  s   
zDWithinLookup.process_distancec                    s8   |  ||\}}|| jd< t ||\}}||| fS )Nr3   )rr   r!   r   r5   )r   r   r   dist_sqldist_paramsr<   rk   r$   r   r   r5   _  s   
zDWithinLookup.process_rhs)r   r   r   r+   r=   rr   r5   r?   r   r   r$   r   rs   M  s
    rs   c                   @   r
   )DistanceLookupFromFunctionc           	      C   sv   t | jdko| jd dkpd }|jj| j| j|d}|||j\}}| 	||\}}d|| j
|d || fS )Nr'   rp   )rp   z%(func)s %(op)s %(dist)s)funcopdist)r*   r   r2   Zdistance_expr_for_lookupr   r#   r   r6   r7   rr   rz   )	r   r   r   rp   Zdistance_exprrj   rk   ru   rv   r   r   r   r   g  s   
z!DistanceLookupFromFunction.as_sqlNr   r   r   r   r   rw   f  r   rw   c                   @   r@   )DistanceGTLookupZdistance_gt>Nr   r   r   r+   rz   r   r   r   r   r|   v      r|   c                   @   r@   )DistanceGTELookupZdistance_gtez>=Nr~   r   r   r   r   r   |  r   r   c                   @   r@   )DistanceLTLookupZdistance_lt<Nr~   r   r   r   r   r     r   r   c                   @   r@   )DistanceLTELookupZdistance_ltez<=Nr~   r   r   r   r   r     r   r   N)0Z#django.contrib.gis.db.models.fieldsr   Zdjango.contrib.gis.measurer   Z	django.dbr   Zdjango.db.modelsr   r   r   Zdjango.db.models.sql.queryr   Zdjango.utils.regex_helperr	   r   r   Zregister_lookuprA   rE   rF   rG   rH   rJ   rL   rM   rN   rP   rQ   rR   rU   rY   rZ   r[   r\   r]   r^   r`   ra   rc   rl   rm   rn   rs   rw   r|   r   r   r   r   r   r   r   <module>   s    T								
		!