o
    uvXja*  ã                   @   s
  d dl 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-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$G d;d<„ d<eƒZ%ejG d=d>„ d>e%ƒƒZ&G d?d@„ d@e%ƒZ'ejG dAdB„ dBe'ƒƒZ(ejG dCdD„ dDe'ƒƒZ)ejG dEdF„ dFe'ƒƒZ*ejG dGdH„ dHe'ƒƒZ+dS )Ié    N)ÚBaseSpatialField)Ú
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   úa/var/www/html/myproject/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Úqueryr0   Úget_geom_placeholderr   Úoutput_field)r   r   r   r    r   Úplaceholderr!   r   r   r4   <   s   zGISLookup.process_rhsc                 C   s   |j j| j S r	   )r0   Úgis_operatorsr(   )r   r   r    r   r   r   Ú
get_rhs_opF   s   zGISLookup.get_rhs_opc           	      C   sX   |   ||¡\}}|  ||¡\}}| |¡ ||ddœ| j¥}|  ||¡}| || ||¡S )Nr/   )r   r    r2   )Úprocess_lhsr4   Úextendr   r;   r   )	r   r   r   Úlhs_sqlÚ
sql_paramsÚrhs_sqlr   r   Úrhs_opr   r   r   r   L   s   
zGISLookup.as_sql)F)r   r   r   Úsql_templateÚtransform_funcÚdistancer,   r.   r   r   r)   r3   r4   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   rG   Z   ó    rG   c                   @   rF   )ÚOverlapsRightLookupz}
    The 'overlaps_right' operator returns true if A's bounding box overlaps or is to the
    right of B's bounding box.
    Úoverlaps_rightNrI   r   r   r   r   rL   c   rK   rL   c                   @   rF   )ÚOverlapsBelowLookupzs
    The 'overlaps_below' operator returns true if A's bounding box overlaps or is below
    B's bounding box.
    Úoverlaps_belowNrI   r   r   r   r   rN   l   rK   rN   c                   @   rF   )ÚOverlapsAboveLookupzs
    The 'overlaps_above' operator returns true if A's bounding box overlaps or is above
    B's bounding box.
    Úoverlaps_aboveNrI   r   r   r   r   rP   u   rK   rP   c                   @   rF   )Ú
LeftLookupzo
    The 'left' operator returns true if A's bounding box is strictly to the left
    of B's bounding box.
    ÚleftNrI   r   r   r   r   rR   ~   rK   rR   c                   @   rF   )ÚRightLookupzq
    The 'right' operator returns true if A's bounding box is strictly to the right
    of B's bounding box.
    ÚrightNrI   r   r   r   r   rT   ‡   rK   rT   c                   @   rF   )ÚStrictlyBelowLookupzp
    The 'strictly_below' operator returns true if A's bounding box is strictly below B's
    bounding box.
    Ústrictly_belowNrI   r   r   r   r   rV   �   rK   rV   c                   @   rF   )ÚStrictlyAboveLookupzp
    The 'strictly_above' operator returns true if A's bounding box is strictly above B's
    bounding box.
    Ústrictly_aboveNrI   r   r   r   r   rX   ™   rK   rX   c                   @   rF   )Ú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_asNrI   r   r   r   r   rZ   ¢   s    rZ   Úexactc                   @   rF   )ÚBBContainsLookupzq
    The 'bbcontains' operator returns true if A's bounding box completely contains
    by B's bounding box.
    Ú
bbcontainsNrI   r   r   r   r   r]   ¯   rK   r]   c                   @   rF   )ÚBBOverlapsLookupz_
    The 'bboverlaps' operator returns true if A's bounding box overlaps B's bounding box.
    Ú
bboverlapsNrI   r   r   r   r   r_   ¸   s    r_   c                   @   rF   )ÚContainedLookupzt
    The 'contained' operator returns true if A's bounding box is completely contained
    by B's bounding box.
    Ú	containedNrI   r   r   r   r   ra   À   rK   ra   c                   @   ó   e Zd ZdZdS )ÚContainsLookupÚcontainsN©r   r   r   r(   r   r   r   r   rd   Í   ó    rd   c                   @   rc   )ÚContainsProperlyLookupÚcontains_properlyNrf   r   r   r   r   rh   Ò   rg   rh   c                   @   rc   )ÚCoveredByLookupÚ	coveredbyNrf   r   r   r   r   rj   ×   rg   rj   c                   @   rc   )ÚCoversLookupÚcoversNrf   r   r   r   r   rl   Ü   rg   rl   c                   @   rc   )ÚCrossesLookupÚcrossesNrf   r   r   r   r   rn   á   rg   rn   c                   @   rc   )ÚDisjointLookupÚdisjointNrf   r   r   r   r   rp   æ   rg   rp   c                   @   rc   )ÚEqualsLookupÚequalsNrf   r   r   r   r   rr   ë   rg   rr   c                   @   rc   )ÚIntersectsLookupÚ
intersectsNrf   r   r   r   r   rt   ð   rg   rt   c                   @   rc   )ÚOverlapsLookupÚoverlapsNrf   r   r   r   r   rv   õ   rg   rv   c                       s.   e Zd ZdZdZe d¡Z‡ fdd„Z‡  Z	S )ÚRelateLookupr#   ú%(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   r0   r:   r(   Úhasattrrz   r   ÚstrÚpattern_regexÚmatchr*   r   r4   )r   r   r   ÚpatternÚ
backend_opÚsqlÚparamsr!   r   r   r4      s   

zRelateLookup.process_rhs)
r   r   r   r(   rB   Úrer
   r}   r4   rE   r   r   r!   r   rx   ú   s
    
rx   c                   @   rc   )ÚTouchesLookupÚtouchesNrf   r   r   r   r   r„     rg   r„   c                   @   rc   )ÚWithinLookupÚwithinNrf   r   r   r   r   r†     rg   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   r5   r/   )
r   r{   r
   r5   r6   r0   Úget_distancer   r8   r(   )r   r   r   Ú
dist_paramr   r   r   Úprocess_distance$  s   
ÿÿýz#DistanceLookupBase.process_distanceN)r   r   r   rD   rB   r   r�   r   r   r   r   rˆ     s
    
rˆ   c                       s$   e Zd ZdZdZ‡ fdd„Z‡  ZS )ÚDWithinLookupÚdwithinry   c                    s8   |   ||¡\}}|| jd< tƒ  ||¡\}}||| fS )Nr2   )r�   r   r   r4   )r   r   r   Údist_sqlÚdist_paramsr@   r‚   r!   r   r   r4   2  s   
zDWithinLookup.process_rhs)r   r   r   r(   rB   r4   rE   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   r0   Údistance_expr_for_lookupr   r    r
   r5   r6   r�   r•   )	r   r   r   rŠ   Údistance_exprr�   r‚   r�   r‘   r   r   r   r   :  s    þz!DistanceLookupFromFunction.as_sqlNr   r   r   r   r   r’   9  r   r’   c                   @   rF   )ÚDistanceGTLookupÚdistance_gtú>N©r   r   r   r(   r•   r   r   r   r   r™   E  ó    r™   c                   @   rF   )ÚDistanceGTELookupÚdistance_gtez>=Nrœ   r   r   r   r   rž   K  r�   rž   c                   @   rF   )ÚDistanceLTLookupÚdistance_ltú<Nrœ   r   r   r   r   r    Q  r�   r    c                   @   rF   )ÚDistanceLTELookupÚdistance_ltez<=Nrœ   r   r   r   r   r£   W  r�   r£   ),rƒ   Ú#django.contrib.gis.db.models.fieldsr   Údjango.db.models.expressionsr   Údjango.db.models.lookupsr   r   Údjango.db.models.sql.queryr   r   r   Úregister_lookuprG   rL   rN   rP   rR   rT   rV   rX   rZ   r]   r_   ra   rd   rh   rj   rl   rn   rp   rr   rt   rv   rx   r„   r†   rˆ   rŽ   r’   r™   rž   r    r£   r   r   r   r   Ú<module>   sˆ    L	