o
    ÔèFh.  ã                   @   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/loop/nvenv/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								
		!