o
    <g.                     @   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   W/var/www/html/venv/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_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queryr3   get_geom_placeholderr   output_field)r   r   r   r#   r   placeholderr$   r   r   r7   <   s   zGISLookup.process_rhsc                 C   s   |j j| j S r   )r3   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 )Nr2   )r   r#   r5   )process_lhsr7   r!   r>   r   )
r   r   r   lhs_sql
lhs_paramsrhs_sqlr   
sql_paramsr!   rhs_opr   r   r   r   N   s   zGISLookup.as_sql)F)r   r   r   sql_templatetransform_funcdistancer/   r1   r    r"   r,   r6   r7   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   rJ   b       rJ   c                   @   rI   )OverlapsRightLookupz}
    The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
    right of B's bounding box.
    overlaps_rightNrL   r   r   r   r   rO   l   rN   rO   c                   @   rI   )OverlapsBelowLookupzs
    The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
    B's bounding box.
    overlaps_belowNrL   r   r   r   r   rQ   v   rN   rQ   c                   @   rI   )OverlapsAboveLookupzs
    The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
    B's bounding box.
    overlaps_aboveNrL   r   r   r   r   rS      rN   rS   c                   @   rI   )
LeftLookupzo
    The 'left' operator returns true if A's bounding box is strictly to the left
    of B's bounding box.
    leftNrL   r   r   r   r   rU      rN   rU   c                   @   rI   )RightLookupzq
    The 'right' operator returns true if A's bounding box is strictly to the right
    of B's bounding box.
    rightNrL   r   r   r   r   rW      rN   rW   c                   @   rI   )StrictlyBelowLookupzp
    The 'strictly_below' operator returns true if A's bounding box is strictly below B's
    bounding box.
    strictly_belowNrL   r   r   r   r   rY      rN   rY   c                   @   rI   )StrictlyAboveLookupzp
    The 'strictly_above' operator returns true if A's bounding box is strictly above B's
    bounding box.
    strictly_aboveNrL   r   r   r   r   r[      rN   r[   c                   @   rI   )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_asNrL   r   r   r   r   r]      s    r]   exactc                   @   rI   )BBContainsLookupzq
    The 'bbcontains' operator returns true if A's bounding box completely contains
    by B's bounding box.
    
bbcontainsNrL   r   r   r   r   r`      rN   r`   c                   @   rI   )BBOverlapsLookupzc
    The 'bboverlaps' operator returns true if A's bounding box overlaps B's
    bounding box.
    
bboverlapsNrL   r   r   r   r   rb      rN   rb   c                   @   rI   )ContainedLookupzt
    The 'contained' operator returns true if A's bounding box is completely contained
    by B's bounding box.
    	containedNrL   r   r   r   r   rd      rN   rd   c                   @      e Zd ZdZdS )ContainsLookupcontainsNr   r   r   r+   r   r   r   r   rg          rg   c                   @   rf   )ContainsProperlyLookupcontains_properlyNri   r   r   r   r   rk      rj   rk   c                   @   rf   )CoveredByLookup	coveredbyNri   r   r   r   r   rm      rj   rm   c                   @   rf   )CoversLookupcoversNri   r   r   r   r   ro      rj   ro   c                   @   rf   )CrossesLookupcrossesNri   r   r   r   r   rq      rj   rq   c                   @   rf   )DisjointLookupdisjointNri   r   r   r   r   rs      rj   rs   c                   @   rf   )EqualsLookupequalsNri   r   r   r   r   ru     rj   ru   c                   @   rf   )IntersectsLookup
intersectsNri   r   r   r   r   rw     rj   rw   c                   @   rf   )OverlapsLookupoverlapsNri   r   r   r   r   ry     rj   ry   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   r3   r=   r+   hasattrr|   r   strpattern_regexmatchr-   r   r7   )r   r   r   pattern
backend_opsqlparamsr$   r   r   r7     s   

zRelateLookup.process_rhs)	r   r   r   r+   rE   r	   r   r7   rH   r   r   r$   r   r{     s
    r{   c                   @   rf   )TouchesLookuptouchesNri   r   r   r   r   r   "  rj   r   c                   @   rf   )WithinLookupwithinNri   r   r   r   r   r   '  rj   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"   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   r8   r2   )
r   r}   r   r8   r9   r3   get_distancer   r;   r+   r   r   r   
dist_paramr   r   r   process_distance?  s   
z#DistanceLookupBase.process_distanceN)r   r   r   rG   rE   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   r8   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   R  s   
zDWithinLookup.process_distancec                    s8   |  ||\}}|| jd< t ||\}}||| fS )Nr5   )r   r!   r   r7   )r   r   r   dist_sqldist_paramsrB   r   r$   r   r   r7   _  s   
zDWithinLookup.process_rhs)r   r   r   r+   rE   r   r7   rH   r   r   r$   r   r   M  s
    r   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'   r   )r   z%(func)s %(op)s %(dist)s)funcopdist)r*   r   r3   distance_expr_for_lookupr   r#   r   r8   r9   r   r   )	r   r   r   r   distance_exprr   r   r   r   r   r   r   r   g  s   
z!DistanceLookupFromFunction.as_sqlNr   r   r   r   r   r   f  r   r   c                   @   rI   )DistanceGTLookupdistance_gt>Nr   r   r   r+   r   r   r   r   r   r   v      r   c                   @   rI   )DistanceGTELookupdistance_gtez>=Nr   r   r   r   r   r   |  r   r   c                   @   rI   )DistanceLTLookupdistance_lt<Nr   r   r   r   r   r     r   r   c                   @   rI   )DistanceLTELookupdistance_ltez<=Nr   r   r   r   r   r     r   r   N)0#django.contrib.gis.db.models.fieldsr   django.contrib.gis.measurer   	django.dbr   django.db.modelsr   r   r   django.db.models.sql.queryr   django.utils.regex_helperr	   r   r   register_lookuprJ   rO   rQ   rS   rU   rW   rY   r[   r]   r`   rb   rd   rg   rk   rm   ro   rq   rs   ru   rw   ry   r{   r   r   r   r   r   r   r   r   r   r   r   r   r   <module>   s    T								
		!